Jump to content

Time delay in a loop and playing a sound


Recommended Posts

Sorry for the noob questions, I'm still learning c#. How would I make the for loop wait 1 second before looping again? And how can I get it to play my sound file just before the loop?

[COLOR=#009695]using[/COLOR][COLOR=#333333] System[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#009695]using[/COLOR][COLOR=#333333] System[/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]Collections[/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]Generic[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#009695]using[/COLOR][COLOR=#333333] System[/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]Linq[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#009695]using[/COLOR][COLOR=#333333] System[/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]Text[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#009695]using[/COLOR][COLOR=#333333] UnityEngine[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#009695]namespace[/COLOR][COLOR=#333333] Airlock[/COLOR]
[COLOR=#333333]{[/COLOR]
[COLOR=#009695]public[/COLOR][COLOR=#009695]class[/COLOR][COLOR=#3364a4]AirlockPressure[/COLOR][COLOR=#333333]:[/COLOR][COLOR=#3364a4]PartModule[/COLOR]
[COLOR=#333333]{[/COLOR]


[COLOR=#333333][[/COLOR][COLOR=#333333]KSPField[/COLOR][COLOR=#333333]([/COLOR][COLOR=#333333]guiName [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]Pressure[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333],[/COLOR][COLOR=#333333] isPersistant [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]false[/COLOR][COLOR=#333333],[/COLOR][COLOR=#333333] guiActiveEditor [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]true[/COLOR][COLOR=#333333],[/COLOR][COLOR=#333333] guiActive [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]true[/COLOR][COLOR=#333333],[/COLOR][COLOR=#333333] guiUnits [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]kPsa[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333])][/COLOR]
[COLOR=#009695]private[/COLOR][COLOR=#009695]int[/COLOR][COLOR=#333333]Pressure[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#f57d00]100[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#009695]public[/COLOR][COLOR=#009695]string[/COLOR][COLOR=#333333]mySoundFile[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]Depressurise[/COLOR][COLOR=#f57d00]/[/COLOR][COLOR=#f57d00]PressureChange[/COLOR][COLOR=#f57d00].[/COLOR][COLOR=#f57d00]mp3[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333];[/COLOR]

[COLOR=#333333][[/COLOR][COLOR=#3364a4]KSPEvent[/COLOR][COLOR=#333333]([/COLOR][COLOR=#333333]guiActive [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]true[/COLOR][COLOR=#333333],[/COLOR][COLOR=#333333] guiName [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]Depressurise[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333])][/COLOR]
[COLOR=#009695]public[/COLOR][COLOR=#009695]void[/COLOR][COLOR=#333333]ActivateEvent[/COLOR][COLOR=#333333]()[/COLOR]
[COLOR=#333333]{[/COLOR]
[COLOR=#3364a4]ScreenMessages[/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]PostScreenMessage[/COLOR][COLOR=#333333]([/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]Depressurising[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333],[/COLOR][COLOR=#f57d00]5.0f[/COLOR][COLOR=#333333],[/COLOR][COLOR=#3364a4]ScreenMessageStyle[/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]UPPER_CENTER[/COLOR][COLOR=#333333])[/COLOR][COLOR=#333333];[/COLOR]

[COLOR=#333333]Events[/COLOR][COLOR=#333333][[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]ActivateEvent[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333]][/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]active[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]false[/COLOR][COLOR=#333333];[/COLOR]

//play
[COLOR=#009695]for[/COLOR][COLOR=#333333]([/COLOR][COLOR=#009695]int[/COLOR][COLOR=#333333]i[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#333333]Pressure[/COLOR][COLOR=#333333]-[/COLOR][COLOR=#f57d00]1[/COLOR][COLOR=#333333];[/COLOR][COLOR=#333333]i[/COLOR][COLOR=#333333]>[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#f57d00]0[/COLOR][COLOR=#333333];[/COLOR][COLOR=#333333]i[/COLOR][COLOR=#333333]--[/COLOR][COLOR=#333333])[/COLOR]
[COLOR=#333333]{[/COLOR]
[COLOR=#333333]Pressure[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#333333]i[/COLOR][COLOR=#333333];[/COLOR]


[COLOR=#888888][I]//[/I][/COLOR][COLOR=#888888][I]Wait[/I][/COLOR][COLOR=#888888][I]2[/I][/COLOR][COLOR=#888888][I]seconds[/I][/COLOR][COLOR=#888888][I]before[/I][/COLOR][COLOR=#888888][I]looping[/I][/COLOR][COLOR=#888888][I]?[/I][/COLOR]
[COLOR=#333333]}[/COLOR]

[COLOR=#333333]Events[/COLOR][COLOR=#333333][[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]DeactivateEvent[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333]][/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]active[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]true[/COLOR][COLOR=#333333];[/COLOR]

[COLOR=#333333]}[/COLOR]
[COLOR=#333333][[/COLOR][COLOR=#3364a4]KSPEvent[/COLOR][COLOR=#333333]([/COLOR][COLOR=#333333]guiActive [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]true[/COLOR][COLOR=#333333],[/COLOR][COLOR=#333333] guiName [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]Pressurise[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333],[/COLOR][COLOR=#333333] active [/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]false[/COLOR][COLOR=#333333])][/COLOR]
[COLOR=#009695]public[/COLOR][COLOR=#009695]void[/COLOR][COLOR=#333333]DeactivateEvent[/COLOR][COLOR=#333333]()[/COLOR]
[COLOR=#333333]{[/COLOR]
[COLOR=#3364a4]ScreenMessages[/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]PostScreenMessage[/COLOR][COLOR=#333333]([/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]Pressurising[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333],[/COLOR][COLOR=#f57d00]5.0f[/COLOR][COLOR=#333333],[/COLOR][COLOR=#3364a4]ScreenMessageStyle[/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]UPPER_CENTER[/COLOR][COLOR=#333333])[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#888888][I]//[/I][/COLOR][COLOR=#888888][I]This[/I][/COLOR][COLOR=#888888][I]will[/I][/COLOR][COLOR=#888888][I]hide[/I][/COLOR][COLOR=#888888][I]the[/I][/COLOR][COLOR=#888888][I]Deactivate[/I][/COLOR][COLOR=#888888][I]event[/I][/COLOR][COLOR=#888888][I],[/I][/COLOR][COLOR=#888888][I]and[/I][/COLOR][COLOR=#888888][I]show[/I][/COLOR][COLOR=#888888][I]the[/I][/COLOR][COLOR=#888888][I]Activate[/I][/COLOR][COLOR=#888888][I]event[/I][/COLOR][COLOR=#888888][I].[/I][/COLOR]
[COLOR=#333333]Events[/COLOR][COLOR=#333333][[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]ActivateEvent[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333]][/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]active[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]true[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#333333]Pressure[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#333333]Pressure[/COLOR][COLOR=#333333]+[/COLOR][COLOR=#f57d00]100[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#333333]Events[/COLOR][COLOR=#333333][[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]DeactivateEvent[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#333333]][/COLOR][COLOR=#333333].[/COLOR][COLOR=#333333]active[/COLOR][COLOR=#333333]=[/COLOR][COLOR=#009695]false[/COLOR][COLOR=#333333];[/COLOR]
[COLOR=#333333]}[/COLOR]



[COLOR=#333333]}[/COLOR]
[COLOR=#333333]}[/COLOR]

Any help is greatly appreciated

Link to comment
Share on other sites

Perfect application for a coroutine:


[RequireComponent(typeof(AudioSource))]
public class AirlockPressure : PartModule
{
[KSPField(guiName = "Pressure", isPersistant = false, guiActiveEditor = true, guiActive = true,
guiUnits = "kPsa")] private int Pressure = 100;

public string mySoundFile = "Depressurise/PressureChange"; // notice [b]no extension[/b]

void Start()
{
audio.clip = GameDatabase.Instance.GetAudioClip(mySoundFile);
}

[KSPEvent(guiActive = true, guiName = "Depressurise")]
public void ActivateEvent()
{
ScreenMessages.PostScreenMessage("Depressurising", 5.0f, ScreenMessageStyle.UPPER_CENTER);

Events["ActivateEvent"].active = false;

StartCoroutine(Depressurize());
}

private System.Collections.IEnumerator Depressurize()
{
audio.Play();
for (int i = Pressure - 1; i >= 0; i--)
{
Pressure = i;
yield return new WaitForSeconds(2f);
}
}
}

Edited by xEvilReeperx
forgot the sound part
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...