Author Topic: Global variables vs Statemachine actions  (Read 1851 times)


  • Playmaker Newbie
  • *
  • Posts: 38
    • View Profile
Global variables vs Statemachine actions
« on: March 04, 2013, 12:02:14 PM »
Is it a better practice to access variables directly from FSMs by using the Statemachine actions such as Get FSM Variable/Int/Float etc... than to create global variables? 


  • Administrator
  • Hero Member
  • *****
  • Posts: 15284
  • Official Playmaker Support
    • View Profile
Re: Global variables vs Statemachine actions
« Reply #1 on: May 14, 2013, 10:59:42 AM »

 It's really a question of taste and design pattern.

 I very very rarely use global variables, because it's a break to encapsulation. If global variables could be set to only be accessible within a hierarchy, that would be a different deal.

 what I tend to do, is create Meta Fsm, holding only variables, then within that hierarchy of gameObjects, each fsm that needs to look up or maintain these variables use get/set fsm var. I don't even bother checking if it is more or less efficient then using global var, it's purely because this is the way I feel confortable with it,

Enforcing encapsulation in playmaker is really something you have to decide at a design level, anything can accessed from anywhere, so you are free to do it the way you want.

 Global var are good for "global" variables...d'oh... :) I mean it actually. If your player has a property shared by several fsm dealing with that player, I tend to not use global var here, cause the day you will want two players... you are in trouble.

now, user prefs is a good place for global vars, cause they really can't be redundant.




  • Full Member
  • ***
  • Posts: 157
    • View Profile
    • Grinder Games
Re: Global variables vs Statemachine actions
« Reply #2 on: May 19, 2013, 09:48:57 AM »
Global vars are not reseted when entering a new scene or resetting a scene.
You have to do that manualy.

So i use globals only for stuff i need for all stages of the game (like the preferences) and use get/set FSM var for the communication between the FSMs.

I like the idea of Jean to create just one meta FSM with vars. I will try that on upcoming projects.