playMaker

Author Topic: Sending a delayed event from script?  (Read 3598 times)

playmakerNewb

  • Playmaker Newbie
  • *
  • Posts: 15
Sending a delayed event from script?
« on: March 01, 2012, 02:09:15 PM »
All,

Currently I have a script (GUI) that takes in an FSM event and on button click, will fire said event, using the following code.

Code: [Select]
PlayMakerFSM.BroadcastEvent(sendEvent);
Where sendEvent is an FsmEvent. 

This works completely fine, however, I would like to be able to specify a delay before this event is fired.

I've tried calling Fsm.DelayedEvent, but since it's not static, I can't.  What other options do I have here?

Thanks.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sending a delayed event from script?
« Reply #1 on: March 02, 2012, 12:15:12 AM »
Hi,

 I Know how to send regular events but the delayed event signture doesn't work for me yet, Maybe Alex will shade some light on this. I'll give you the script snippet I have to send regular events.

 the key is to have a fsmComponent pointer at hand. Without this it's not possible. ANY Fsm component in your scene will do, so either you make a public PlaymakerFSM variable (fsmProxy in the script below)  and drop a fsm component in it, then in your script have this:

Code: [Select]
// set the target to be this gameObject. You can point to any GameObject in the scene of course.
FsmOwnerDefault goTarget = new FsmOwnerDefault();
goTarget.GameObject = new FsmGameObject();
goTarget.GameObject.Value = this.gameObject;
goTarget.OwnerOption = OwnerDefaultOption.SpecifyGameObject;

       // send the event to this gameObject and all its children. check the 'target' this to modify the behavior
FsmEventTarget eventTarget = new FsmEventTarget();
eventTarget.excludeSelf = false;
eventTarget.target = FsmEventTarget.EventTarget.GameObject;
eventTarget.gameObject = goTarget;
eventTarget.sendToChildren = true;

// create the event.
FsmEvent fsmEvent = new FsmEvent("Hellooo there!");

// send the event
fsmProxy.Fsm.Event(eventTarget,fsmEvent.Name); // works only because I declare the event as string here, not using the FsmEvent class itself.
// send the same event bu delayed
fsmProxy.Fsm.DelayedEvent(eventTarget,fsmEvent); // TOFIX: doesn't work because delayed event only have one signature and it's using the FsmEvent.




So hopefully this will give you a head start on what to do to fire events. I actually had to send a delayed event, and ended up implementing an invoke() call in my script to implement the delay in the script instead of with the event itself, the result is identical in the end.

There are more api goodies to make sure the event exists or actually get it somehow, but I haven't had time to investigate these api fully yet. Maybe Alex will be able to develop on these a bit more.

Bye,

 Jean

playmakerNewb

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Sending a delayed event from script?
« Reply #2 on: March 02, 2012, 08:13:42 AM »
Hi.

There is actually now a static method that allows you to easily send events from a script.  That's what I referenced in my first post.  What I really need is a way to introduce a delay into this static call.

In your code, you're referencing a fsmProxy variable.  What type of object is that?

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4002
  • Official Playmaker Support
    • LinkedIn
Re: Sending a delayed event from script?
« Reply #3 on: March 02, 2012, 04:05:47 PM »
There's no static method for delayed events yet. Actually the API for delayed events is not terribly user friendly right now...

However, there is a static list of all active PlayMakerFSMs: PlayMakerFSM.FsmList that you can loop through.

Note, you'll get an error if you change this list while iterating through it (e.g., if you send an event that destroys an FSM), so it's a good idea to copy it first.

A quick example:

Code: [Select]
var fsmList = new List<PlayMakerFSM>(PlayMakerFSM.FsmList);

foreach (var fsmComponent in fsmList)
{
    fsmComponent.Fsm.DelayedEvent(FsmEvent.GetFsmEvent(fsmEventName), delay);
}

Haven't actually tested it, but that should send a delayed event to all active PlayMakerFSMs.

I'll add a static method for this...

playmakerNewb

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Sending a delayed event from script?
« Reply #4 on: March 02, 2012, 04:13:01 PM »
Thanks!  I'll give this code a try on Monday.  An adjustment to the static method to broadcast methods would work well for me though.