playMaker

Author Topic: How exactly do state machines work?  (Read 3886 times)

TonkRogerio

  • Playmaker Newbie
  • *
  • Posts: 24
How exactly do state machines work?
« on: May 24, 2017, 07:49:41 PM »
I'm going to provide an example because I'm not really sure how to ask this question.

I have a spawner and that spawner creates named troops.
I have a manager game object that generates names based on certain specifics about the troops like race for example in an rpg game.
I have the "troop" prefab spawn with the ability to tag itself into a "self" variable and it is given the manager game object by it's spawner. So no problems there.
The troop passes it's "self" variable to the manager so it can start generating a name for itself, then the manager gets the name back to the troop.
The potential problem might come when two "troops" are both targeting the same manager. I'm not really sure if I need to create some sort of queue system in the manager or what. I'm not really sure how this is handled at all within the engine as I'm not really a programmer.

Any help getting me to understand this better would be very appreciated. At the moment I'm thinking of a queue system where the manager checks if the game object variable is null or if it has something. if it has something then it puts it into an array. it would be easier if I could just add to an array from another fsm but I can't as far as I know. Was easier with the downloadable arrays to be honest. but even if I decide to download them and use them instead of the ones that come with playmaker now, I'd still like to know how this mechanism works. if the state machine is a single item or if it creates a new manager whenever it gets called from another object.

I'm not really sure if any of this is making sense. Maybe programmers know exactly what I'm on about, maybe not lol. I don't know. any help is appreciated though.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: How exactly do state machines work?
« Reply #1 on: May 25, 2017, 01:31:21 AM »
Hi,
I would have a name generator right after creating the prefab and use a set fsm string on the "troop" to set it's name, then use a "send event" on the "troop" fsm to set its name or use a "string changed" on the "troop" fsm.

If you want to spawn many you should consider a pooling system, because creating many objects may give lag issues.


TonkRogerio

  • Playmaker Newbie
  • *
  • Posts: 24
Re: How exactly do state machines work?
« Reply #2 on: May 25, 2017, 09:41:07 AM »
//I would have a name generator right after creating the prefab and use a set fsm string on the "troop" to set it's name//

Right. I know this, but my question is about how I would go about doing this considering if two troops tried to access that manager at the same time.
I know I have to get the generator to set the fsm of the "troop" that's my goal. The problem is how to do it without glitches in the future.

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: How exactly do state machines work?
« Reply #3 on: May 25, 2017, 09:44:56 AM »
Not sure I completely understand your question.

But when your troop triggers the manager, have it set a bool to true "in use". When the manager is done, on the last state, it can change itself back to false "in use".

When each troop tries to use the manager, have them check the bool first. If its true, then have them wait .1 second and try again. (No idea how long your manager takes). Then two troops cannot go through the manager at the same time, and will queue themselves.

Does that answer your question???
« Last Edit: May 25, 2017, 09:46:50 AM by tcmeric »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: How exactly do state machines work?
« Reply #4 on: May 25, 2017, 12:02:59 PM »
Hi,
I mend in the same fsm that you are creating the objects.

How are you creating the "troops" ?

On a spawner object with an fsm or multiple spawners ?

Maybe you can give some more info with some pictures or video showing your setup?


TonkRogerio

  • Playmaker Newbie
  • *
  • Posts: 24
Re: How exactly do state machines work?
« Reply #5 on: May 25, 2017, 12:30:12 PM »
Not sure I completely understand your question.

But when your troop triggers the manager, have it set a bool to true "in use". When the manager is done, on the last state, it can change itself back to false "in use".

When each troop tries to use the manager, have them check the bool first. If its true, then have them wait .1 second and try again. (No idea how long your manager takes). Then two troops cannot go through the manager at the same time, and will queue themselves.

Does that answer your question???

OK it actually looks to me like you get what I'm saying even though you aren't sure. I can confirm you're on the right track though.
I've thought of doing it like that as a queue system, but when I think about it, the "troop" would have to get the bool state first from the name manager. then it has to check it. then it has to pass the information back.
What if between it doing all of that, another "troop" does the exact same thing at the same time? then two "troop" prefabs would be checking the bool and coming up with it being false. then both would transmit the data and it looks to me like it would mess it up when the name manager is doing it's thing.


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How exactly do state machines work?
« Reply #6 on: May 25, 2017, 03:09:13 PM »
Hi,

 for this sort of potential "race condition" I solve it using an array ( use FsmArray or ArrayMaker from the Ecosystem), and each trop register itself in that array by adding itself, and so there can not be any problem with this approach,

you can then generate unique Id using the index of a particular object inside this array.


Bye,

 Jean

TonkRogerio

  • Playmaker Newbie
  • *
  • Posts: 24
Re: How exactly do state machines work?
« Reply #7 on: May 25, 2017, 05:34:53 PM »
Hi,

 for this sort of potential "race condition" I solve it using an array ( use FsmArray or ArrayMaker from the Ecosystem), and each trop register itself in that array by adding itself, and so there can not be any problem with this approach,

you can then generate unique Id using the index of a particular object inside this array.


Bye,

 Jean

This is how I used to do it with array maker, but with fsm array it is required that I set the fsm array. there is no simply add to fsm that I can see like there was with array maker.
Maybe I just have to download array maker until this feature is implemented in fsm array.
Unless it  is there but I'm not seeing it.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How exactly do state machines work?
« Reply #8 on: May 26, 2017, 05:08:10 AM »
Hi,

 I am confused. you can simply create an FsmArray at edit time and use that inside your pool Manager, I don't think you need to create this array at runtime at all.

Or do you mean ArrayAdd action to add an item to an FsmArray, it is there indeed.

 Bye,

 Jean

TonkRogerio

  • Playmaker Newbie
  • *
  • Posts: 24
Re: How exactly do state machines work?
« Reply #9 on: May 26, 2017, 07:22:44 AM »
Well maybe I'm doing something wrong.

The way I've got it set up now is I have an object called spawner. I have the prefab called "troop". And I have a manager called "name generator"
The spawner spawns the troop and the troop decides if it's gender, race, class, etc.
Then it looks for managers that randomly generate those things. the manager in question at the moment is race and gender. once it figures out those it will pass itself as a gameobject variable to the name generator. then the generator can extract the information from the troop.

To pass it over from the troop to the name generator I can't use a "add array" on the troop or the spawner because it doesn't add to the array of another fsm. So the "troops array" in the name generator is a different object completely with a different fsm.

With array maker you could add to another object by name If I remember correctly.

Maybe I'm just not doing it right but as I said, I used to use this method in array array maker. I'm not a programmer so maybe there is a better way to do this. I don't know.

Champ

  • Playmaker Newbie
  • *
  • Posts: 8
Re: How exactly do state machines work?
« Reply #10 on: May 26, 2017, 10:33:17 PM »
Managers don't work that great when the spawned objects get it to ask it things whenever it feels like.

I think you're queuing system would work better. I'd make it simplex though.

I'd make the spawned game objects children of another game object.. say "Undefined troops" and then i'd have the manager periodically check the children of "Undefined troops".

Once a child shows up, the manager does all the stuff you want it to do ( i didn't get all the stuff you want to do.. but thats ok ), and then takes that child out and reparents it to another gameobject.. or to root, whatever.

I'd suggest using "get next child 2" with a reset bool so you can force the manager to always check the first child whenever there is one.

I wouldn't bother having the spawned game object talk to the manager for this stuff. Think new employee sitting down waiting for someone to show 'em around the office on their first day.

Hope that helps.

TonkRogerio

  • Playmaker Newbie
  • *
  • Posts: 24
Re: How exactly do state machines work?
« Reply #11 on: May 27, 2017, 02:06:02 PM »
That's not a bad idea but it's so much easier with array maker. I've decided to just download that and do it the easy way tbh :/

FSM arrays really can't compete at this point for me. Don't think they can be saved with easysave nether from what I've heard but could be wrong.

Anyway I wouldn't say this is solved because ultimately I'm having to use something else because of a missing feature of fsm arrays.

Thank you for all the replies though, guys. I appreciate the help.