playMaker

Author Topic: Send Event  (Read 2430 times)

gregmax17

  • Playmaker Newbie
  • *
  • Posts: 11
Send Event
« on: September 26, 2012, 09:29:34 AM »
Is there a way to get the Send Event action to use the Delay when the Time.timeScale is set to 0?

If time scale is 0, and the delay is 1, the Send Event will never be called. Any ideas on how to solve this?

gregmax17

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Send Event
« Reply #1 on: September 26, 2012, 12:04:37 PM »
Never mind... I looked into it more and rewrote the Send Event script, here is what I have now:

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

using System;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.StateMachine)]
[Tooltip("Sends an Event after an optional delay. NOTE: To send events between FSMs they must be marked as Global in the Events Browser.")]
public class SendEvent : 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.")]
public FsmFloat delay;

[Tooltip("Repeat every frame. Rarely needed.")]
public bool everyFrame;

public bool realtime;

private DelayedEvent delayedEvent;
private float startTime;
private float currentTime;

public override void Reset()
{
eventTarget = null;
sendEvent = null;
delay = null;
everyFrame = false;
realtime = false;
}

public override void OnEnter()
{
startTime = FsmTime.RealtimeSinceStartup;
currentTime = 0f;

if (delay.Value < 0.001f)
{
Fsm.Event(eventTarget, sendEvent);
Finish();
}
else if ( !realtime )
{
delayedEvent = Fsm.DelayedEvent(eventTarget, sendEvent, delay.Value);
}
}

public override void OnUpdate()
{
if (!everyFrame)
{
if ( realtime )
{
currentTime = FsmTime.RealtimeSinceStartup - startTime;

if ( currentTime >= delay.Value )
{
Fsm.Event(eventTarget, sendEvent);
Finish();
}
}
else
{
if ( DelayedEvent.WasSent(delayedEvent) )
Finish();
}
}
}
}
}

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4002
  • Official Playmaker Support
    • LinkedIn
Re: Send Event
« Reply #2 on: September 26, 2012, 07:05:52 PM »
Cool, thanks for sharing :)

The DelayedEvent class stores a copy of the current FsmEventData (set by the Set Event Data action) to avoid bugs where a delayed event has the wrong event data. It's a fairly uncommon use case, but you should probably do something similar in your implementation...

Something along these lines:
var storeEventData = new FsmEventData(Fsm.EventData);

Then temporarily set Fsm.EventData when sending the event. A bit messy, but it should work.

I'll try to add a Realtime option to the DelayedEvent class to handle this internally...