playMaker

Author Topic: Add "Behaviours" to states?  (Read 2368 times)

elxen

  • Playmaker Newbie
  • *
  • Posts: 6
Add "Behaviours" to states?
« on: June 17, 2014, 08:10:55 PM »
Hello,

previously I was using a custom-made FSM where I have the following rules:
1- States are actual gameObjects.
2- The FSM has a list of FSMState, each FSMState has a list of FSMTransition, and each transition has a FSMState (the state to transit to)
3- Only one state (the current state) is active at a time.
4- Transitions are made from the "MakeTransition" method from FSMTransition which 'indirectly' tells the FSM to set the 'toState' (the state that the transition goes to) as the current state.
5- Interacting with the FSM = Interacting with its current state gameObject.

This allowed me to easily create "Trigger FSMs" - basically, triggers that changes states. How? what do I mean by that?

For ex, I was working on creating some classical Resident Evil mechanics, take the tiger statue puzzle as an example (see this video @2:40
If you interact with it the first time, it displays text, when you use the red gem on it, it rotates to the right (or left, can't remember) and reveals a crest item, interact with it now, and you'll pickup the crest! The statue goes back to its start state, interact now and you'll get the same text you got before. (Same thing happens when you use the blue gem)

How is this done? - The tiger statue has 3 states:
1- Start
2- On RedGem Use
3- On BlueGem Use

1- In Start: there's a "TextTrigger" - Interact with it (press action key) and it will display some text. There's also two "ItemUseTrigger"s on it, one references the BlueGem, the other the RedGem. ItemUseTrigger has an OnUse delegate so that when I use the BlueGem, I can tell the FSM to make transition and move to the "On BlueGem Use" state.

2- In either of the two remaining states: there's an "ItemPickup" script that when the player interacts with, picks up the revealed item and tells the FSM to go back to its initial state - this is done via a delegate too "OnPickup" that gets fired when an item is picked up.

You can take this concept further into AI, say you had an AI with: Idle, Patrol, Chase and Attack. So you have 4 states = 4 gameObjects. You put whatever behaviours you want on each of those states, so in the Attack state, you attach components that will give you the attacking behaviour you want. Since one state is active at a time, it means only the behaviours of the current state is active (so Attack doesn't conflict with Patrol, etc)

My question now is: How can I achieve that with Playmaker? How can I create a state, that contains behaviour? (components) - And if Playmaker doesn't support this, how can I achieve the wanted results? How can I create a trigger that changes states (interact with it once, does this, interact with it twice, does that, etc - Like the tiger statue for ex) - I would prefer a delegate-friendly solution if possible.

Attached is a simple FSM diagram for the tiger statue puzzle.

Thank you!

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4003
  • Official Playmaker Support
    • LinkedIn
Re: Add "Behaviours" to states?
« Reply #1 on: June 17, 2014, 08:45:41 PM »
You can use the Enable Behaviour action to turn Behaviours off and on. The "Reset on Exit" setting makes it very easy to coordinate a Behaviour with a state - the Behaviour is active while the state is active, and disabled when the state is exited.

To trigger different transitions based on game state you can use Logic Actions like Bool Test, Int Compare, Int Switch (associate different events with 0, 1, 2...) etc. Remember actions are executed in order so you can stack tests, and they behave like if, else, else blocks, either sending an event or falling through to the next action.

We don't expose any delegates yet, but you can use Send Message, Invoke Method, or Call Method to call Behaviour methods when changing state.
« Last Edit: June 17, 2014, 08:49:11 PM by Alex Chouls »

elxen

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Add "Behaviours" to states?
« Reply #2 on: June 17, 2014, 09:07:56 PM »
Thanks for your reply. Disabling/Enabling the behaviours selectively one by one is kind of tedius but OK to work around (I could maybe write a "ComponentsCategory" that would let me put certain components under one and then enable/disable that one component instead)

However, I saw Enable Behaviour but no Enable Component out of the box which is strange. Enable Behaviour does have a Component reference but i'm not sure for what... since dropping a component (say box collider) to it doesn't seem to affect it, the doc doesn't say much "Optionally drag a Component directly into this field"

I was kinda hoping from Playmaker for delegates more than SendMessages all over the place, hope you work on improving that.

Thank you.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4003
  • Official Playmaker Support
    • LinkedIn
Re: Add "Behaviours" to states?
« Reply #3 on: June 17, 2014, 09:10:41 PM »
If you have a whole bunch of Behaviours on a GameObject you can use Activate GameObject instead, then just organize them how you described before... It also has a "Reset on Exit" setting.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4003
  • Official Playmaker Support
    • LinkedIn
Re: Add "Behaviours" to states?
« Reply #4 on: June 17, 2014, 09:13:30 PM »
Can you describe your use case for delegates in more detail? Thanks!

elxen

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Add "Behaviours" to states?
« Reply #5 on: June 17, 2014, 09:37:18 PM »
Most certainly!

I use delegates extensively! I believe they provide true power and flexibility. (That's why I made uFAction http://forum.unity3d.com/threads/ufaction-a-complete-set-of-serializable-and-inspectable-delegates-for-unity3d.245248/)

I asked another question about delegates with an ItemPickup example here: http://hutonggames.com/playmakerforum/index.php?topic=7653.0

More examples:
1- As previously mentioned in this post, I have an ItemUseTrigger which is basically a trigger that references "Usable" item(s) when the item(s) is/are used, an OnUse delegate (Action) fires. So say I wanted to create a "Manhole Opener" (sounds funny I know...) - I would use it on the "Manhole" - so first I add "Usable" to the opener - then I go to Manhole and give it a ItemUseTrigger and add the opener. So now I can use the opener on the hole. When I do that, OnUse fires which, let's say plays a cut-scene for Leon opening a Manhole to go down hanka-hanka with Aida Wong.

2- An AI sensory system, take Vision for example, say your AI has Vision, Hearing and Touch. When you construct your AI, you let him subscribe to his senses so when his eyes see a player, they notify him so that he takes whatever necessary measures (say transit from patrol/idle to chase/attack or whatever...)
Code: [Select]
         private void OnEnable()
{
// Sub to eyes
eyes.OnSeen += OnSeen;

// Sub to touch
touch.OnTouch.Add(OnTouch);

// Sub to ears
ears.OnHeard += OnHeard;
}

private void OnSeen(GameObject current, List<GameObject> all)
{
if (current.tag == Tags.player)
{
playerInSight = true;
}
else if (playerInSight)
{
playerInSight = all.Select(hit => hit.tag).Contains("Player");
}
}

private void OnHeard(ISoundEmitter source, GameObject noiseMaker)
{
if (noiseMaker.tag == Tags.player)
Debug.Log("Heard player moving");
}

public void OnTouch(Collider other)
{
if (other.tag == Tags.player)
{
Debug.Log("Touching player");
onPlayerTouch.Invoke();
}
}

3- In a Text/Dialogue system (much like in the video ^) - especially when getting interactive text: "There's a switch, will you push it? Yes | No" - I have an OnDecision foreach interactive decision so when the player says yes, a certain chain of event fires based on what's subbed to the yes's OnDecision, and when says No another chain fires... Very flexible.

Hope I made it clearer.

Lastly, where can I post suggestions/enhancements for some stuff in Playmaker? (Like for example, seeing there's a "Newbie" besides my name really irritates me - It would be better if it was "Playmaker Newbie")

Thank you.
« Last Edit: June 17, 2014, 09:41:18 PM by elxen »