playMaker

Author Topic: Advice re: too many global variables  (Read 2639 times)

ManicMinerUK

  • Junior Playmaker
  • **
  • Posts: 51
Advice re: too many global variables
« on: March 23, 2015, 12:59:01 PM »
Hi all,

In my game, the player can have various different kinds of object equipped (think of an RPG where the player might have a sword, an axe, a bow and arrow, etc) - At first I tracked all of this through a list of global bool variables (SwordEquipped?, AxeEquipped? etc etc). Effectively I've been using global variables almost as a save game.

As you can imagine, this has lead to me having a huge list of bools in the global variables list, and I'm starting to worry that a) just finding the relevant variable to check in various scripts is becoming a nightmare, and b) this is obviously bad practice as global variables should be used sparingly...

Is there a better way I could be tracking player loadouts that would reduce this spread of global variables? Ideally I would be saving this data into some kind of player config file or something, but I don't know if playmaker can work with external files like that?

TheBadFeeling

  • Junior Playmaker
  • **
  • Posts: 66
Re: Advice re: too many global variables
« Reply #1 on: March 24, 2015, 03:24:54 PM »
You can use both external xml and playerprefs for stuff like that with PlayMaker.
And instead of storing all those globals, you could let one "Inventory" object hold them as internal FSM variables. So instead of getting a global, you get the variable from your Inventory when needed.

I use EasySave2 to save player progress, and I pour all my inventory stuff into an ArrayMaker list. So when I need to check if my CrystalAxe is equipped, I just need to ask my inventory-admin object that holds the list.

At the moment I have 5 globals in my game -- and around 70 inventory items. w00t!  ;)

/Bad
The Force is with you, young Playmaker – but you are not a C# senpai yet.

ManicMinerUK

  • Junior Playmaker
  • **
  • Posts: 51
Re: Advice re: too many global variables
« Reply #2 on: March 25, 2015, 10:19:00 PM »
thanks man, that's really good advice (assuming I'm understanding you correctly).

I now have a single game object that is Don't Destroy on Load, and I'm using that to store all the booleans I am using to track the inventory and doing getFSMBool whenever I need to query them...

Only thing I'm not sure about is that this feels like it might actually just be global vars by another route - like they are still a bunch of variables, globally available etc... so is it actually any better to hold them here, or would I be better looking into array maker etc?

TheBadFeeling

  • Junior Playmaker
  • **
  • Posts: 66
Re: Advice re: too many global variables
« Reply #3 on: March 29, 2015, 04:31:19 PM »
Honestly, I'm definitely no hardcore coder, but it's my impression that because globals are everpresent to all parts of your game, they tend to make things more complex -- fx because every FSM can read and write to them at the same time.
When the variables are inside a particular FSM, that object is in control and decides when it's okay to ask or set them. At least, that's my way of looking at it.
Furthermore, you cannot change your pickaxe into a hatchet when it's a global, 'cause you're not allowed to change global names.  ;D

Whether using 200 variables on an FSM or attaching an ArrayMaker list with 100 entries is better, or which it more memory friendly, I honestly don't know. I just like the routine of querying items from the list I guess.  ::)

Good luck with your game!
/Bad
The Force is with you, young Playmaker – but you are not a C# senpai yet.