playMaker

Author Topic: Incorrect event source when sending events to sub-FSM  (Read 1197 times)

kingstone426

  • Playmaker Newbie
  • *
  • Posts: 2
Incorrect event source when sending events to sub-FSM
« on: December 13, 2018, 08:38:44 AM »
Not sure if this shuld be posted here or under Playmaker Bugs, nut let's see if we can figure this out.

I have a goblin that will chase a chicken when it comes within range. The goblin fires off a SendEvent action that is received by the chicken who uses the GetEventInfo action to find out which goblin it should run away from.

This all works ok when the chicken listens for the event in a single FSM, but it doesn't work when the chicken listens for the event inside a sub-FSM. The sub-FSM still receives the event, but the event source is now the chicken itself.



What's really weird is that the sub-FSM "sometimes" gets the correct goblin event source when I enable a breakpoint on the chicken state receiving the event. I haven't been able to figure out why or when this occurs.

FYI:
I use a custom event called "GoblinChase" that is marked as global in all FSMs.
The goblin's SendEvent action has EventTarget=GameObjectFSM with GameObject=Chicken (FSM variable in goblin) Delay=0 EveryFrame=false

When debug-logging Fsm.EventData.SentByState inside GetEventInfo, I saw that the sub-FSM got its event from the host-FSM instead of the goblin. Strangely, Fsm.EventData.SentByGameObject seemed to be null.

Is this the intended behaviour? If so, how can I get the "true" source of the event when using nested FSMs?

Thanks!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Incorrect event source when sending events to sub-FSM
« Reply #1 on: December 13, 2018, 02:27:42 PM »
Hi.
It might have to do indeed with the subfsm/template

Can you try using 'send event by name' instead as a template is like a prefab
When you use a send event on an object and make it a prefab it will also break.

The reason for that is that you can not communicate from the scene to objects in the project folder (prefabs) (unity limitation)

But i would do this differently, instead of using get event info. :

when the chicken gets in range you can use the 'Set Fsm Gameobject' to set the goblin that is in range.

Then you can send event...
OR
You can set the action 'Game Object Changed' on the chicken which will trigger an event if the variable was set by the goblin.

kingstone426

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Incorrect event source when sending events to sub-FSM
« Reply #2 on: December 14, 2018, 05:15:07 AM »
Thanks for replying djdino!

I tried using SendEventByName but it yielded the same result.

The goblin has a Food variable that is used as the event target. That variable is set in runtime (when the chicken gets within range) so no problems with the prefab/template thing.

I'd rather not use SetFsmGameObject because I fear that accessing Fsm variables directly from other objects will cause unexpected and weird behaviour as my project grows and gets more complex. Also, I want to avoid GameObjectChanged for performance reasons, as it polls the state of the variable every frame.



For now, my workaround is a SetEventGameObjectToSelf action that simply sets: 
Code: [Select]
Fsm.EventData.GameObjectData = Fsm.Owner.gameObject;
Then the chicken checks GetEventInfo's GetGameObjectData instead of SentByGameObject to find the event source. Not very elegant though.

Is there a better way?