playMaker

Author Topic: Is it possible to create new variables at runtime?  (Read 3409 times)

craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 234
    • Haven Made
Is it possible to create new variables at runtime?
« on: October 11, 2017, 02:18:10 AM »
Was curious whether it was possible to create new variables on an FSM at runtime :)

-craigz

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is it possible to create new variables at runtime?
« Reply #1 on: October 11, 2017, 02:31:42 AM »
Hi,

 yes, the api should allow for this, but it doesn't make sense since none of the fsm would be wired to actually use it, can you explain your use case?

 bye,

 Jean

craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 234
    • Haven Made
Re: Is it possible to create new variables at runtime?
« Reply #2 on: April 19, 2018, 03:41:06 PM »
Hi Jean!

I'm SO sorry for the crazy late response, notifications for this thread were off for me I guess? xD but this is still something that I would find super useful! :D

The main use case would primarily be for manager objects, where instead of interacting with a component to manage things (like using a hash table to keep track of how many units there are of a certain type and recording their stats) you could instead create the variables for those units on scene loading, and so long as you had a deliberate naming scheme, interacting with an FSM would be infinitely easier than a component :)

I know for me personally I usually end up having a lot of my data loaded in through hash tables, but it gets pretty messy to manage/interact with. Especially at run time, when you're getting and setting keys constantly.

For instance, say you have a 'FSM.UnitManager' and its whole goal is to keep track of unit stats in a level. Like how many kills, deaths, etc each type of unit has in a scene. You could create all the variables needed for each unit, and then since the game objects themselves have names, you can automate using a key system to interact with that data, so then all you'd have to do is use the regular actions like 'Subtract FSM Int', and then use the key (which would come from a build string) for the variable name.

This is my current setup though!

HashTableProxy
----------------
blue_archer_countStart
blue_archer_countEnd
blue_archer_countCurrent
blue_archer_kills
blue_archer_deaths
blue_crossbowman_countStart
blue_crossbowman_countEnd
blue_crossbowman_countCurrent
blue_crossbowman_kills
blue_crossbowman_deaths
red_bandit_countStart
red_bandit_countEnd
red_bandit_countCurrent
red_bandit_kills
red_bandit_deaths

And while my current setup *does* work, every time I need to set something on the hash table, I have to check if the key exists, and if it goes, get the Int, then add to the Int, then set the Int. It just seems like a lot of leg work for something that an FSM could handle easily if it could just create variables at runtime xD especially because I'm generating from a library of units created at runtime, not a bunch of prefabs (because they all share the same basic setup).

Let me know what you think, I know my example might sound niche, but I know you see the value of creating variables at runtime in general :P

-craigz

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Is it possible to create new variables at runtime?
« Reply #3 on: April 20, 2018, 01:35:38 AM »
Hi.
You might want to look into using xml or json.

and get datamaker to read/write the xml file.

Here are some video's that can help you get started with datamaker :



craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 234
    • Haven Made
Re: Is it possible to create new variables at runtime?
« Reply #4 on: April 24, 2018, 04:03:54 PM »
Hey djaydino!

I've looked into both xml and json and know the datamaker extension. If you're updating those constantly during a game (say for runtime stats etc) performance actually takes pretty big hit.

Thats why (I was hoping?) I guess past initializing a manager object and creating variables at the start of a scene, it would be more performant? :)

-craigz

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is it possible to create new variables at runtime?
« Reply #5 on: April 26, 2018, 02:05:28 AM »
Hi,

I think you are misunderstanding where performances suffers when using xml and json.

 it only affect performances when you save xml or json as strings for storage, otherwise, it is extremly fast, on par with any other methods for storing values.

 So, don't fear updating values constantly on an Xmlnode reference, but ONLY save in the player prefs at defined time like when the user pauses or in between level loading.

 
Creating Fsm variables at runtime doesn't work, because no fsm was created using these inexistant variable during editing, do you see the problem? what fsm would use your newly created fsm variable?

so, always use a database of some sort ( arrayMaker, XmlMaker, or a full blown nosql or sql database) for storing values that only exists at runtime and store them inside your Fsm using placeholders fsm variable.

 Bye,

 Jean
« Last Edit: April 27, 2018, 04:10:48 AM by jeanfabre »

craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 234
    • Haven Made
Re: Is it possible to create new variables at runtime?
« Reply #6 on: April 26, 2018, 10:34:54 AM »
Hi Jean,

I absolutely am misunderstanding where performance suffers xD

I'll definitely start porting my current systems over to it then and just save when I need to during level transitions (if ever for something like my current context).

Thanks for the clarification and for you and Djaydino for putting me on the right track :D

-craigz

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Is it possible to create new variables at runtime?
« Reply #7 on: April 26, 2018, 05:28:27 PM »
Hi.
No problem, we are here to help :)