Page 1 of 1

Is there a way to pass parameters into Scene Events?

Posted: Mon Aug 14, 2023 1:50 pm
by Basel
Currently I have a function to UpdateGameState(parameter).

Wasn't sure how to get this to show up is Scene Event. I'm just write specific functions that return void to get to work (e.g. UpdateCombatState(){UpdateGameState(CombatState);}

Is there another recommended way of doing this?

Thanks for your assistance.

Re: Is there a way to pass parameters into Scene Events?

Posted: Mon Aug 14, 2023 2:38 pm
by Tony Li
Hi,

Unity restricts UnityEvents such as the OnExecute() scene event to only accept primitive types or UnityEngine object types such as GameObject or Transform.

You could make those wrapper methods, or you could use a custom sequencer command instead of scene events. Example:

Code: Select all

// Syntax: UpdateGameState(state, subject)
// Example: UpdateGameState(combat, OrcBoss)
public class SequencerCommandUpdateGameState : SequencerCommand
{
    void Awake()
    {
        string state = GetParameter(0);
        AI ai = GetSubject(1).GetComponent<AI>();
        switch (GetParameter(0))
        {
            case "combat":
                ai.UpdateGameState(CombatState);
                break;
            case "idle":
                ai.UpdateGameState(IdleStatE);
                break;
        }
        Stop();
    }
}
There are many things you could do to enhance this script, such as checking if GetSubject(1) returns null, or -- if your game states are an enum -- use something like Enum.GetNames(typeof(GameState)) to get the game state names instead of hard-coding them here. The above is just a simple example.

Re: Is there a way to pass parameters into Scene Events?

Posted: Tue Aug 15, 2023 11:20 am
by Basel
I really appreciate the breakdown, thank you.
It's one thing to make a great product, it's another thing to add great support to a great product. Many thanks for your time once again.

Re: Is there a way to pass parameters into Scene Events?

Posted: Tue Aug 15, 2023 12:46 pm
by Tony Li
Glad to help!