playMaker

Author Topic: Communicating between Prefabs and FSMs  (Read 1444 times)

PalmlessPrayer

  • Playmaker Newbie
  • *
  • Posts: 4
Communicating between Prefabs and FSMs
« on: February 09, 2021, 02:51:43 PM »
After reading the best practices section of the site I still have some questions about the best way to transfer variables betweeen fsms and prefabs for the following scenarios

Scenario 1 - FSMs within the same Prefab.
So I noticed variables within a FSM within the same Prefab can't call each others variables without using a "Get FSM Bool/Int/Float... ". Wondering what the best practice for this is because isn't it possible to store the variables within the prefab itself.

Scenario 2 - FSMs to FSMs in other Prefabs
Assuming this is where I would use Global Events and pass gameobjects. For instance for my characters in a turn based game I would make a FSM which listens for a command event, this event will store what the command is and who it is for. Almosting like networking in away.

Really I'm just super interested to know the ways others are doing their architecture and communication between FSMs, Sub-FSMs and Prefabs.

thoughts much appriecated  : ) really enjoying PlayMaker so far

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Communicating between Prefabs and FSMs
« Reply #1 on: February 09, 2021, 03:47:47 PM »
Hi.
Scenario 1 : this is in all cases, not only on prefabs.
if you want data from a different fsm you need to use (Get/Set) Fsm Actions.
I'm not sure what you mean by 'store the variables within the prefab itself.'

Tip : There are several other custom Fsm actions on the Ecosystem like Fsm Bool Test / Fsm Float Operator for example.

Scenario 2 :
Global events is something different than global variables.
if you mean global variables, then it depends on the prefab.

in our game we only have a hand full of game objects set as global variables.
for example our character root / Meta Data / Item Data / Enemy Data.

inside those game objects, for example Enemy data, we have arrays with spawned enemies (prefabs)
which we add/remove on spawn/despawn.

Here are some videos that might be useful :




Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Communicating between Prefabs and FSMs
« Reply #2 on: February 09, 2021, 04:13:09 PM »
Variables are stored in each FSM. There are a few strategies for FSMs to share variables:
  • Using Get FSM Bool/Int/Float...
  • Using Set Event Data then Send Event. The receiver can then use Get Event Info.
  • Turn the FSM into a Template and use Run FSM. The Run FSM editor will show the template's Input and Output variables. You can hook these up to local variables.
  • Global variables that can be accessed by any FSM.
Some considerations for each approach:
  • Get FSM XXX gives you the most freedom but is very unstructured. You're just getting and setting variables at will. This can backfire as your project grows if you don't organize your variables carefully.
  • Event data is a little more structured since you have to mark an event as global and have an informal understanding of the data associated with it. You should use a Comment or Tooltip to document the data you're expecting.
  • Templates are the most structured approach since you can only use variables explicitly set as Inputs or Outputs. The Input/Output variables should use Tooltips to communicate what they're used for. All other variables are considered private, part of the internal workings of the FSM, and probably best not to mess with.
  • Globals can be very convenient but should be used sparingly. Most of the time it's better to have a "manager" FSM that has some "global" variables that can be accessed by other FSMs. The manager FSM can do extra work to validate or manipulate its variables unlike globals that are just "raw" values.

Hope that makes some sense! They're all valid approaches, but it's good to understand their pros and cons.

There is also the related issue of referencing scene objects in Prefabs.
« Last Edit: February 09, 2021, 04:17:16 PM by Alex Chouls »

PalmlessPrayer

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Communicating between Prefabs and FSMs
« Reply #3 on: February 09, 2021, 04:23:09 PM »
Thank you so much both of you, very detailed and useful.

PalmlessPrayer

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Communicating between Prefabs and FSMs
« Reply #4 on: February 10, 2021, 05:52:53 AM »
I have some thoughts with communicating via events and I'm trying to implement the command pattern. Below is the first solution I've thought of so I bet theres probably a better way of doing it.

My current solution:
Prefab that runs commands has an FSM which manages received Command events.
Commands events recieved have already had their properties set elsewhere, for example a move command would have the command_name and move_pos.
The FSM that manages commands will store the command_name into a string array and move_pos into a vector 2 array within the FSM.
The prefab has a main FSM which has all the states such as move, attack and more. This main FSM listens for when the list of command_names array length is >0 then switches to a state based on the comman name, then grabs the relavent array data and deletes at index 0.

I have some concerns with this solution.
1. I'm using multiple arrays to store data for an event. Could I not store store an event in one array as a class?
2. How robust is listening for events? If my command listener FSM recieves an event while it is processing a command, won't it just disregard the current command and start over.

 


ch1ky3n

  • Full Member
  • ***
  • Posts: 208
Re: Communicating between Prefabs and FSMs
« Reply #5 on: February 10, 2021, 11:06:49 AM »

in our game we only have a hand full of game objects set as global variables.
for example our character root / Meta Data / Item Data / Enemy Data.


So make global of the object of the metadata that contains all the important variables?
gotta try this one!