playMaker

Author Topic: Set FSM Int Add?  (Read 5928 times)

Xtopher

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 71
    • Well Played Games
Set FSM Int Add?
« on: June 16, 2011, 02:25:28 PM »
I've run into a situation where I want to "add" to an int var on another FSM, but the only function is to set it.  However, there is an "add int" for local vars.  Any way to do this that I just haven't found, or would this require a new node?

FWIW, I am counting an active number of objects in a spawn manager FSM and want to lower that number when an object is destroyed.

Thanks for any help!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Set FSM Int Add?
« Reply #1 on: June 17, 2011, 06:21:56 AM »
Hi,

 Events can hold data, that's very convenient for your case: check the following pair actions:

https://hutonggames.fogbugz.com/default.asp?W531
https://hutonggames.fogbugz.com/default.asp?W530


----- CASE:

 FSM 1 : the fsm from where you want to increment the value
 FSM 2 : the fsm declaring the variable "my int" and another var "_add_" holding the amount to add

------

 You could do the following:

 in FSM 1 send an event "ADD TO MY INT " with that event int data set to the amount you want to add.
 in FSM 2 catch that event, get that int, store it in second temp variable like "_add_" and then you are all local and access "my int" easily by simply adding "_add_" to "my int" ( that's when I'd like playmaker to expose the ability to declare variables public or local... it totally make sense in such situation I feel)

The thing to be careful then is that you need to have FSM 2 ready to accept that "ADD TO MY INT" anytime and not have other potential process going on at the same time.

--------------

The other solution ( maybe less efficient cause always running) is to use the same fsms and variable config and set the "_add_" directly from FSM 1 and have FSM 2 checking constantly for "_add_" change and if change, add and reset "_add_" automatically.

If you have lots happening in FSM 2, all this is not really possible without refactoring. In this situation ( likely the case), I create a "meta data" fsm that simply holds variable and the events and actions required to manage them ( making sure they can not interfere with each other). I create as many and sometime one per variable so that it's totally isolated and can run constant checks and/or react to events without breaking any process. Very powerful, it's very much like implementing getter and setter function in conventional codes, you can react to variable changes by using events instead of directly change the variable from external places. This allow better encapsulation and provide a much higher level of flexibility and power all around.

--------------

 The last option is to write a custom action that add to ints from any fsm together.

 I did that for boolean tests and such. please find it attached with this post. I have also made a post on the right forum for easy referencing: http://hutonggames.com/playmakerforum/index.php?topic=353.0
 
 If you have any questions or needs clarifications, don't hesitate :)

 Bye,

 Jean

Xtopher

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 71
    • Well Played Games
Re: Set FSM Int Add?
« Reply #2 on: June 18, 2011, 07:18:46 PM »
Thanks for all that!  I worked with it yesterday and got some good stuff going.  And yeah, as you said, making sure the event is called when a state is paying attention is the key.  I ended up making a dedicated simple fsm that managed flipping some switches, and that way the larger spawn manager only needed to run when it was called to and do everything in it's stack that was relevant.  It's working great.

Thanks so much!

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: Set FSM Int Add?
« Reply #3 on: October 05, 2020, 04:11:04 PM »
Thank you Jean!!!
Have I said how much I love playmaker!!! This is one amazing tool

SamH

  • Playmaker Newbie
  • *
  • Posts: 41
Re: Set FSM Int Add?
« Reply #4 on: October 12, 2020, 07:26:02 PM »
Thanks Jean! 10 years later but that 'AddToFsmInt' action (which is now on the ecosystem for anyone looking at this) is very helpful.   So important when I need to instantly & reliably adjust an int on another FSM.
I was previously doing a 'get' int, doing the add locally and then sending back the int- but i was finding the math was unreliable if multiple sources did the same thing simultaneously.  Much appreciated
« Last Edit: October 12, 2020, 07:29:04 PM by SamH »