playMaker

Author Topic: Global Variables Are Bad???  (Read 10339 times)

iEpic

  • Playmaker Newbie
  • *
  • Posts: 48
    • IndieGamerWorld
Global Variables Are Bad???
« on: January 29, 2015, 09:31:34 PM »
I read that using global variables are bad, but I'm always having problems with the game objects not being found without them so I've been using them.

Are global variables really bad like I've read?
 Thanks

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Global Variables Are Bad???
« Reply #1 on: January 29, 2015, 09:48:31 PM »
Its more of a bad habit than actually being bad.

Global variables are saved in a file, for one thing, and it happens to get deleted quite often on accident typically when a user tries to upgrade unity/playmaker or import stuff from a different project.

But aside from that there is nothing specifically bad in using globals other than the fact that using them prolifically is often a sign of less than ideal design being in place. The cases for global variables might be the Player character in a simple Singleplayer game, some Level manager object, a proxy that holds information in variables that other objects use, a database, a manager object that is needed by many other objects, or something to that effect.

Consider if you have a trigger which takes heath away that only the player will ever really set off so you just use the player global variable when its tripped. It's really not good design to do it that way. You can't scale up with the game at all, you have a trigger that takes health away from the player and only works if the global variable is set correctly (what if the scene changes? are you maintaining that global properly?).

On the other hand consider that same trigger, but you set up a generic "trap" template which - when triggered - collects the information it needs about what tripped it, finds the Health of that object and deducts a value that you expose from it... That template can be used for anything - player traps, enemy traps, etc... So its a much better design and not a band-aid solution to your Trap system. You probably can use it widely and you aren't constrained to the Player global variable.

Thats essentially the main reason (from what I've gathered) that globals are avoided for the most part. The circumstances where a global variable is really applicable are quite limited so you're better off designing more modular systems that operate on the fly based on realtime data instead of some fixed variables that may or may not be changing and would break things if they did.

Hope that helps.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

blackant

  • Hero Member
  • *****
  • Posts: 521
  • http://blackantmaster.com
    • blackantmaster.com
Re: Global Variables Are Bad???
« Reply #2 on: January 30, 2015, 01:56:50 AM »
it's mainly important to use global variables for objects that are used from a scene to another
like for exemple score /continue/Lifes....

if you don't call an external scene, you can do it using standard variables

wheretheidivides

  • Sr. Member
  • ****
  • Posts: 496
Re: Global Variables Are Bad???
« Reply #3 on: January 30, 2015, 11:55:53 AM »
If you need to you the variable in another FSM or scene use a global.  Like the score.  You need the score to stay from scene to scene. 

Local variables are just used on that FSM on that scene.  Like if you want to fade the scene in from black, you can use the animation graph to go from 0 to 1 and set the transparency.  You don't need this anywhere else except in that FSM.

Now let's say you have a door that opens when you get near it and decides to change color to red or blue.  It looks for a key that you may have.  The key would be a global variable as the key can open any door.  However, the variable to change the color of the door can be local as it only changes the color of itself.  If you copy and past the door in the scene may times, each one will have local variables that only they use.  The names will be the same, but local variables can have the same names (on different FSMs).

You will have a lot of global variables in your project and the more you have, the messier it gets. 


Lane:  it's funny  you mentioned the deleting the global variables info.  I did that the other day and just recovered finally.  So, if you have a FSM on a object, the local variables will be on that FSM.  If you delete the global variables info, the local varibales and FSMs will remain.  This is not something you should worry about as it's user error for not knowing about this when reinstalling playmaker.
« Last Edit: January 30, 2015, 11:59:56 AM by wheretheidivides »

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Global Variables Are Bad???
« Reply #4 on: January 30, 2015, 12:08:53 PM »
I've often used them to point to important scene objects in my pre-fabs, so my VariableManager game object gets loaded as an object on a Global Game Object, and then all my prefabs reference that game object.

iEpic

  • Playmaker Newbie
  • *
  • Posts: 48
    • IndieGamerWorld
Re: Global Variables Are Bad???
« Reply #5 on: January 30, 2015, 12:11:13 PM »
It seems like when I use the set property, I always run into problems later on where it can't find the game object it used to be getting or setting from.
The only way I've been able to keep all the connections and not worry have to worry about losing them, is to set all the game objects as global gameobject variables.
Am I just doing it wrong, or should I be doing that?

iEpic

  • Playmaker Newbie
  • *
  • Posts: 48
    • IndieGamerWorld
Re: Global Variables Are Bad???
« Reply #6 on: January 30, 2015, 12:37:31 PM »
How could I set a gameobject to a global gameobject variable if it is disabled?
I normally just use get owner, but when the gameobject isn't active it doesn't work.
As you can tell I am a noob. lol

clandestine

  • Sr. Member
  • ****
  • Posts: 255
    • ЯKD.ZONE
Re: Global Variables Are Bad???
« Reply #7 on: January 30, 2015, 02:36:14 PM »
I'm pretty much a noob, so I wont pretend i understood anything Lane wrote, but i use globals to communicate completed levels, lives, collected continues, etc. Is there a way to send variables between scenes without using globals?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Global Variables Are Bad???
« Reply #8 on: January 30, 2015, 02:44:06 PM »
You can use persistent GameObjects that survive through scene loads by making use of the Dont Destroy On Load action (or Do Not Destroy On Load, i forget the exact title).
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

clandestine

  • Sr. Member
  • ****
  • Posts: 255
    • ЯKD.ZONE
Re: Global Variables Are Bad???
« Reply #9 on: January 31, 2015, 04:00:39 AM »
You can use persistent GameObjects that survive through scene loads by making use of the Dont Destroy On Load action (or Do Not Destroy On Load, i forget the exact title).

True, forgot about that... but are they really better than globals (more reliable)?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Global Variables Are Bad???
« Reply #10 on: January 31, 2015, 09:41:16 AM »
I haven't used globals in that manner so I'm not really certain.

Score is a good example of global use. I think at the bottom line though, it's the over use of them in regular logic design out of pure convenience when it could be done better another way that makes them 'bad'.

YMMV, just keep an open mind when designing systems that might use them.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

blackant

  • Hero Member
  • *****
  • Posts: 521
  • http://blackantmaster.com
    • blackantmaster.com
Re: Global Variables Are Bad???
« Reply #11 on: February 11, 2015, 06:03:13 AM »
i know by experience, that having used undreds of global variables can cause sometimes a strange bug wich will cause you more time to correct it,
because the variables suddenly disappeared, causing trouble in the script.
and after reorganised all of them, the strange variable reappeared...

so, use it only for important things for global values throught the game.