playMaker

Author Topic: Get Global Variable By Name  (Read 483 times)

mecauz

  • Playmaker Newbie
  • *
  • Posts: 10
Get Global Variable By Name
« on: June 28, 2023, 07:24:03 AM »
Hi,

Is there any way to get a global variable value with name? :( i create different game features and i use a lot of templates. when FSM Template start, build strings like "FeatureName"_"Status" etc. i name "FeatureName" and other inputs in editor. i can get and set player prefs with no problem. i can get and set global variable object's variables. but i can get or set directly a global variable with the name. how can i do that, am i missing something? ::)

Thanks

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7621
    • jinxtergames
Re: Get Global Variable By Name
« Reply #1 on: June 28, 2023, 09:01:13 AM »
Hi.
What you can do is Create a empty game object and make sure is stays alive when changing scenes.
There is a action called Dont Destroy On Load to do this.
To make sure there is only 1 of this object you can use the action Singleton Manager (Ecosystem)

Set that gameobject as a global and call it for example 'Data' then add a fsm (call it 'Data' as well for example.

now you can get/set variables in there that you need.
then use for example Get/Set Fsm Float to get/set a variable, and you can use strings now for variable names.

on the Ecosystem you can find more fsm actions (for example Fsm Bool Test)

mecauz

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Get Global Variable By Name
« Reply #2 on: June 28, 2023, 09:42:26 AM »
Hi djaydino,

Thank you for your reply. I have already a game object i named "GlobalManager". I hold my all global variables in this object (around 40 variables). I was using that method since i started to this project. but i can't see or change variables on play mode. i created a manager that i can see all values on play mode.

Your recommendation solve this "can't see and change" situtation but every time a scene start, this don't destroy on load object have to set it self to the global. But i have a lot of FSMs reaching and get the data right after start the scene. I have to carefully design the FSMs who need the data after scene loaded. That's why i wasn't go that way.

I tried to move all global variables (in GlobalManager) to Global Variables, but i can't get with builded string names.

Is there any way to get variables from Global Variables with builded string inside from the FSM. I tried to create custom action with ChatGPT (i create like 50 custom actions with ChatGPT  ;D), but it wouldn't work.

Thank you

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7621
    • jinxtergames
Re: Get Global Variable By Name
« Reply #3 on: June 29, 2023, 06:14:17 AM »
Hi.
If you have a little knowledge (or maybe with chat gpt) you can use scriptable objects.
In our project I use scriptable objects a lot (after I knew about them)
for example :
Enemies Data :  (damage per attack / total health / resistance settings / and so on)
Weapons :  base / elemental Damage per level (upgrades) , UI data (Image, buy/sell price/etc)

Some advantages when using scriptable objects:

You can edit during play mode and changed stay after play mode (this can also be a downside, if you forgot the setting it had before you changed)

Easy to access, Just drag/drop the scriptable object in a variable.

if you don't want to use or can't.
You could load this global manager for example in a splash screen.
you can also set a check on other fsm's to see if the global is null with the action : Game Object Is Null (every frame) and if not null you can continue what you have to do in that fsm.

mecauz

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Get Global Variable By Name
« Reply #4 on: June 30, 2023, 04:17:33 AM »
Hi djaydino,

Yes, I also use a lot of scriptable objects, but only for reading. I specifically use them for decoration metadata, where they hold the decoration data for each concept. Using scriptable objects is a good option, but I avoid writing to scriptable objects during play mode due to performance issues (although I'm not sure how significant those issues are). I'm developing a mobile puzzle casual game, such as match 3 and solitaire. Performance is the most important aspect for me.

Could you please compare the performance of using global variables and scriptable objects, considering both writing and reading during play mode?

Thank you.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7621
    • jinxtergames
Re: Get Global Variable By Name
« Reply #5 on: June 30, 2023, 10:50:05 AM »
Hi.
There should be no performance difference.
I have all our data : player inventory / shards / currency / options / enemy attack damage's / etc (a lot more, event localisation text etc) in scriptable's

I do have dedicated actions for bool test when regular used, for example on my move fsm to check if a 'Always run' Setting is enabled/disabled

else i would need to get the bool and then do a bool test.
so i made 1 action 'Check Always run'