Page 1 of 1

SendMessage() and WaitForMessage() use on c# script

Posted: Wed Sep 25, 2019 4:08 am
by KakimaAkuma
Hi again!
In my current flow, I need to do some work on scripts and wait until it's finished.

I have this in my block sequencer
Sequence.PNG
Sequence.PNG (2.96 KiB) Viewed 1472 times
and in my script:

Code: Select all

using PixelCrushers.DialogueSystem;

....
public void DeductGems()
{
	//Do stuff
	Sequencer.Message("DeductGemsFinished");
}

I can't seem to get any message to my block and can't progress after this.

Re: SendMessage() and WaitForMessage() use on c# script

Posted: Wed Sep 25, 2019 4:30 am
by KakimaAkuma
Turns out I'm actually receiving something but I'm still not progressing to the next block.
MessageReceived.PNG
MessageReceived.PNG (3.82 KiB) Viewed 1471 times

Re: SendMessage() and WaitForMessage() use on c# script

Posted: Wed Sep 25, 2019 10:33 am
by Tony Li
Hi,

Is there perhaps a typo in the message that you're sending? Can you compare it to this example?

WaitForMessageExample_2019-09-25.unitypackage

Re: SendMessage() and WaitForMessage() use on c# script

Posted: Wed Sep 25, 2019 9:19 pm
by KakimaAkuma
It worked now.

Code: Select all

public void DeductGems()
    {
        StartCoroutine(Deductor());
    }

    IEnumerator Deductor()
    {
      //Do stuff
         yield return new WaitForSeconds(1);
        Sequencer.Message("DeductGemsFinished");
    }

Didn't knew that you needed a Coroutine for it to work

Re: SendMessage() and WaitForMessage() use on c# script

Posted: Wed Sep 25, 2019 9:28 pm
by Tony Li
You don't technically need a coroutine. The coroutine allows the WaitForMessage(DeductGemsFinished) command to start. Otherwise SendMessage(DeductGems,,MessageManager) will run DeductGems(), which will call Sequencer.Message("DeductGemsFinished") before sequence has even started the WaitForMessage(DeductGemsFinished) command. You probably could have reordered your sequence like this:

Code: Select all

WaitForMessage(DeductGemsFinished);
SendMessage(DeductGems,,MessageManager)
and removed the coroutine. Then WaitForMessage will have started (and will be waiting for "DeductGemsFinished") before SendMessage runs DeductGems().

But, in practice, many times you will use a coroutine when you're using WaitForMessage because it will probably be waiting for some amount of time. The coroutine allows Unity to keep running while your C# method is active.

Re: SendMessage() and WaitForMessage() use on c# script

Posted: Wed Sep 25, 2019 9:36 pm
by KakimaAkuma
Oh I created a race condition by ordering the Sequences wrong. Thank you so much for the clarification and the examples.