playMaker

Author Topic: Global Variable best practices?  (Read 2978 times)

Krileon

  • Full Member
  • ***
  • Posts: 107
Global Variable best practices?
« on: April 07, 2013, 08:47:17 PM »
I'd like some suggestions on what people think the best practices is or should be for Global Variable usage?

Is it ok to have 100+ Global Variables?

Should a Global Variable always be used when a Variable is used multiple times by multiple FSMs?

Should you set these Global Variables using Prefabs or have a empty GameObject with an FSM that establishes their values?

Is there risk of using too many Global Variables causing issues later?

Am curious to hear everyones thoughts on the matter. So far I'm around 12ish Global Variables and counting. I find that I gain much better performance establishing the Global instead of having multiple GameObjects establish a variable on Start.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Global Variable best practices?
« Reply #1 on: April 08, 2013, 07:47:36 AM »
Quote
Is it ok to have 100+ Global Variables?
Yup. The editor may start responding slowly while editing them, but otherwise it isnt an issue.

Quote
Should a Global Variable always be used when a Variable is used multiple times by multiple FSMs?
That's the point of globals. Otherwise you need to get the variable every time.

Quote
Should you set these Global Variables using Prefabs or have a empty GameObject with an FSM that establishes their values?
Set them in the Global Variable editor, the same place you create them.

Quote
Is there risk of using too many Global Variables causing issues later?
Not as far as I have seen. I don't see why it would. The editor may have some slowdown but it should be fine. They aren't rocket science, its just a different variable type.

Quote
Am curious to hear everyones thoughts on the matter. So far I'm around 12ish Global Variables and counting. I find that I gain much better performance establishing the Global instead of having multiple GameObjects establish a variable on Start.

12 globals is definitely nothing to worry about. I have like 15+ on my character controller alone.

Maybe some scripters can elaborate on this..

Globals: Good for working between scripts, single objects, sharing data between things easily. Can't have duplicate names in the project.
Non-globals: Good for keeping the data all local, mass produced items, generic items, duplicate names all day long across the project.

So if you had an AI on a monster and told it to change a bool when you came in range so it would look at you, and you used a global, then ALL of those monsters would do that. If it were a local variable then it would only be for that one monster that you actually came in range of. If you wanted them to act individually then you would clearly use local variables for that action.
« Last Edit: April 08, 2013, 07:50:11 AM by Lane »
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Krileon

  • Full Member
  • ***
  • Posts: 107
Re: Global Variable best practices?
« Reply #2 on: April 08, 2013, 09:56:08 AM »
Quote
Set them in the Global Variable editor, the same place you create them.
My only issue with this is if you create an Object Global Variable and set it to a Component of a Prefab GameObject it doesn't actually work or at least for the asset component I was using it was not. Same for a few GameObjects and found I had to establish them using an FSM instead of using the Prefab (the MainCamera for example).

Anyway, this is good information to know; thank you!