playMaker

Author Topic: [SOLVED] Global variables order of initialization in different FSMs  (Read 2229 times)

LucVdB

  • Playmaker Newbie
  • *
  • Posts: 2
Hi,

I am watching the tutorials at http://www.hutonggames.com/tutorials_game_design_with_playmaker.php .

In tutorial 7, the owner of an FSM (a game object, in this case go_HealthManager) is assigned to a global variable in the setup of the FSM. This way the FSM can be referenced by other FSMs in the game.

Now, how can we be sure that this variable gets assigned before another FSM tries to reference it (e.g. in its own setup script - which doesn't happen in the tutorial itself)? The variable might be null if the order is 'wrong'.

Thanks,

Luc
« Last Edit: May 15, 2012, 01:34:09 PM by Alex Chouls »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Global variables order of initialization in different FSMs
« Reply #1 on: May 15, 2012, 12:53:01 AM »
Good question! Unity makes no guarantees about the order that monobehaviors are updated, so you have to consider it basically random...

Intializing global variable values in FSMs was a workaround for the PlayMakerGlobals asset losing its default values in older versions of Playmaker. This should now be fixed, so the workaround should no longer be necessary.

However, in other situations where you need to guarantee update order, there are various strategies you could use:

- Do stuff that needs to be done first, then load the level.
- Use the Next Frame Event to wait till the next frame to do something.
- Use states and events between FSMs to explicitly define an execution order.

Hope this helps some! Most of the time this shouldn't be an issue - it's a general problem with any monobehaviour in Unity, but rarely causes actual problems...

LucVdB

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Global variables order of initialization in different FSMs
« Reply #2 on: May 15, 2012, 02:12:00 AM »
That is a great answer. I learned a few things, got a few new tricks in my toolbag.

Thanks very much!