playMaker

Author Topic: Send Event from State  (Read 4366 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Send Event from State
« on: March 21, 2012, 03:41:05 AM »
Hi,

 I ran into a very fundamental concept that I totally overlooked so far.

if you send a delayed event. the event will be sent even if you exited the state before the delay. this is major! because all other actions ( as far as I know) do not to anything at all when not running.

 It turns out this cause major problems with what I wanted to achieve. I appreciate it can also be use as an advantage in other cases.

so I created a special action that WILL NOT SEND a delayed event if the state exited before the delay.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;
using System;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.StateMachine)]
[Tooltip("Sends an Event. If optional delay is set, WILL ONLY SEND THE DELAYED EVENT IF IT IS STILL IN THE STATE.\n" +
"NOTE: 'Send Event' will fire a delayed event even if the action is not running anymore as the state exited. \n" +
"NOTE: To send events between FSMs they must be marked as Global in the Events Browser.")]
public class SendEventFromState : FsmStateAction
{
[Tooltip("Where to send the event.")]
public FsmEventTarget eventTarget;

[RequiredField]
[Tooltip("The event to send. NOTE: Events must be marked Global to send between FSMs.")]
public FsmEvent sendEvent;

[HasFloatSlider(0, 10)]
[Tooltip("Optional delay in seconds. NOTE: The event will not be fired if the state has exited before the delay")]
public FsmFloat delay;


private float startTime;

public override void Reset()
{
eventTarget = null;
sendEvent = null;
delay = null;

}

public override void OnEnter()
{
if (delay.Value < 0.001f)
{
Fsm.Event(eventTarget, sendEvent);
Finish();
}
else
{
// start a timer to know when to send the delayed event.
startTime = Time.realtimeSinceStartup;

}
}


public override void OnUpdate()
{

// check the delta time against the delay
float deltaTime = Time.realtimeSinceStartup- startTime;
if (deltaTime >= delay.Value)
{
// we fire a normal event.
Fsm.Event(eventTarget, sendEvent);
Finish();
}
}
}
}

I think a possible way of this would be to either have a "Send transition" action that would only allow you to select a transition from that state, but I can't find anything like that in the api to implement such actions.

I am not sure about the implication of all of this, for example, I can not find a way to cancel a declared delayedEvent, so I had to run my own timer and send a regular event instead.

Bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Send Event from State
« Reply #1 on: June 13, 2014, 07:23:45 AM »
Hi,

 this action can be now downloaded directly from your project, using the Ecosystem online browser.

Also, follow news with the ecosystem rss feed to get the latest.

Bye,

 Jean