playMaker

Author Topic: Playmaker FSM + own scripts, should they be handled via custom actions? [SOLVED]  (Read 3511 times)

FrozenTarzan

  • Playmaker Newbie
  • *
  • Posts: 9
Hi Playmaker community,

Let's say I have a component for a fighting-tower with a custom AI. The tower has a mesh that can "look at" a target position and has a component that handles the internal behaviors like "seek and destroy", "offline", "follow target peacefully", "turn around as fast as possible and shoot like there is no tomorrow".

Without Playmaker I would design a FSM with code. So When the Update() of the tower is called, I would call the FSM.Update() method, which would use polymorphism to call the correct Update() method of the current state.

Now how would I use Playmaker to represent such a FSM with the visual Playmaker window?

Possibility 1: Use the internal FSM for the tower as usual and just use the events from playmaker to handle the state-changes. The communication could happen by calling a method with the "Send Message" action. But this way I still have to use my own FSM implementation.

Possibility 2: Use the FSM from Playmaker and all the events and so forth. Then in the Update() of the tower, I have a lot of if/elseif/switches that check for different parameters and adapt the behavior. Like an enum with SeekAndDestroy, Offline,... This way I can set the parameters directly via actions from Playmaker (like Set Float Value,...) but the internal design is ugly.

Possibility 3: Use custom actions for each state I can be in. So I would pass the original GameObject (the tower) to the action and this action would encapsulate the behavior? It would be like passing the tower to a generic implementation of state design pattern. In this case I would have a custom action for each state the object can be in?

Possibility 4: ...insert your best solution here! ;-)

I'll try to reformulate my problem: When I have an object that behaves differently in different states and this object has to handle code (more complex than the predefined Playmaker actions) in an Update() routine, how do I design an appropriate FSM with playmaker?
« Last Edit: September 13, 2013, 03:46:31 AM by FrozenTarzan »

GarthSmith

  • Playmaker Newbie
  • *
  • Posts: 31
  • Games. Math. Code.
    • Garth Smith's Website
Hello!

I have recently dealt with similar issues with PlayMaker. My solution was pretty much your option #3.
  • Make an abstract base class that extends the FsmStateAction class. For example,
    public class TowerStateAction : FsmStateAction {...
  • This base class should have a static protected property with a reference to your Tower class.
  • Then for each state of your tower, you can extend the TowerStateAction class, use OnEnter, OnUpdate, OnExit functions and control your Tower from there.
Was that clear enough? Does anyone have any better ideas? It seems like this is how custom actions are supposed to work.

[Edit: Added the keyword "static".]
« Last Edit: September 13, 2013, 01:42:22 PM by GarthSmith »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Hi,

 Yes this is valid. Tho I tend to use this approach when I have several actions with common features, they implement such system to prevent code redundancy.

 I tend to simply compose the class I want to work with in an action. So I simply either instanciate that class in a private variable within the action and then work with it this way, or if it's a behavior or a static class, I accomodate the referencing.

bye,

 Jean

FrozenTarzan

  • Playmaker Newbie
  • *
  • Posts: 9
Hi jeanfabre, hi Garth Smith,

thank you for the quick reply! I will set this to [SOLVED], even though these design questions might be interesting for a longer discussion :-)

Great community by the way!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Hi,

 Thanks :)

 Look up the wiki section and the addons specificallly, if you own few of these assets, it's really a good thing to see how playmaker bridges where designed, so tyou can get a feel of what's possible.

https://hutonggames.fogbugz.com/default.asp?W714

bye,

 Jean

GarthSmith

  • Playmaker Newbie
  • *
  • Posts: 31
  • Games. Math. Code.
    • Garth Smith's Website
Thanks jeanfabre for catching that. In my previous post...
* This base class should have a protected property with a reference to your Tower class.

Should have been:
* This base class should have a static protected property with a reference to your Tower class.

And thanks for PlayMaker. I'm finding that PlayMaker with custom actions can be very powerful and allow super quick implementation all at once.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Hi,

 Yep, PlayMaker + Custom Action is raw power and a brease!


Not sure if you are asking questions here. Do you need more input on this?

bye,

 Jean