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

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
KakimaAkuma
Posts: 29
Joined: Mon Sep 09, 2019 9:51 pm

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

Post 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 1469 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.
KakimaAkuma
Posts: 29
Joined: Mon Sep 09, 2019 9:51 pm

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

Post 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 1468 times
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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
KakimaAkuma
Posts: 29
Joined: Mon Sep 09, 2019 9:51 pm

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

Post 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
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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.
KakimaAkuma
Posts: 29
Joined: Mon Sep 09, 2019 9:51 pm

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

Post by KakimaAkuma »

Oh I created a race condition by ordering the Sequences wrong. Thank you so much for the clarification and the examples.
Post Reply