playMaker

Author Topic: SEND EVENT DATA FROM C# SCRIPT  (Read 10248 times)

Palanysamy

  • Playmaker Newbie
  • *
  • Posts: 14
SEND EVENT DATA FROM C# SCRIPT
« on: August 01, 2014, 06:25:54 AM »
Dear all,
I have no problems to send events from script but I am having a hard time to send event data to a FSM. With Playmaker is very easy with actions set event data and get event data, but I am unable to set a float data to this fsm from script.

The state is triggered by the public event ¨DEAL DAMAGE¨, inside this state I have the action Get Event Info, with the Get Float Data set to variable ¨damage¨. How can I set this ¨damage¨ variable value from a C# Script? I have been searching the whole forum but nothing helped me.

Code: [Select]
behavior.Fsm.EventData.FloatData = damage;

with that code I get Static member `HutongGames.PlayMaker.Fsm.EventData' cannot be accessed with an instance reference, qualify it with a type name instead.

How is this done please, there is nothing about it in the API reference.

Best Regards,
« Last Edit: August 01, 2014, 08:50:22 AM by Palanysamy »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #1 on: August 04, 2014, 07:14:02 AM »
Hi,

 I would simply go another way and use "Get property" to store the damage in an fsm variable, then use "Set event data", inject that damage fsm variable and fire the event.

If you must do this from script, then you need to do the following:

create an instance of FsmEventData, populate it with your damage value, then apply it using this line
Code: [Select]
HutongGames.PlayMaker.Fsm.EventData = myFsmEventData
then you fire the event from script or else and it will be passed on.

you can find some code sample in ArrayMaker ( in file PlayMakerUtils_Events )

https://hutonggames.fogbugz.com/default.asp?W715

 Bye,

 Jean

Palanysamy

  • Playmaker Newbie
  • *
  • Posts: 14
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #2 on: August 05, 2014, 07:37:13 AM »
Thank you Jean, I´ll give it a try.
Cheers,
P.

Palanysamy

  • Playmaker Newbie
  • *
  • Posts: 14
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #3 on: August 10, 2014, 04:06:11 PM »
Hello Jean,
I tried your suggestion by creating a fsmeventdata instance and setting the damage value, and then typing HutongGames.PlayMaker.Fsm,EventData = myFsmData; right before the fsm.event(¨applyDamage¨) but I only get the following error which causes Unity to slow down :
NullReferenceException: Object reference not set to an instance of an object
HutongGames.PlayMaker.Fsm.SetEventDataSentByInfo ()
HutongGames.PlayMaker.Fsm.Event (HutongGames.PlayMaker.FsmEventTarget eventTarget, HutongGames.PlayMaker.FsmEvent fsmEvent)
HutongGames.PlayMaker.Fsm.Event (HutongGames.PlayMaker.FsmEvent fsmEvent)
HutongGames.PlayMaker.Actions.Wait.OnUpdate () (at Assets/PlayMaker/Actions/Wait.cs:57)
HutongGames.PlayMaker.FsmState.OnUpdate ()
HutongGames.PlayMaker.Fsm.UpdateState (HutongGames.PlayMaker.FsmState state)
HutongGames.PlayMaker.Fsm.Update ()

The file you suggested to check on doesnt help me much, could you provide a working example of how to send data with an event from script. In this particular issue I really need to do it by script.

Thanks in advance.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #4 on: August 14, 2014, 08:16:23 AM »
Hi,

 here we go:

Code: [Select]
using UnityEngine;
using System.Collections;

using HutongGames.PlayMaker;

public class SendData : MonoBehaviour {

public PlayMakerFSM fsm;

public string eventName = "MY EVENT";
public string myData = "hello";

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (Input.anyKeyDown)
{
SendEvent();
}
}

void SendEvent()
{
FsmEventData eventData= new FsmEventData();
eventData.StringData = myData;

HutongGames.PlayMaker.Fsm.EventData = eventData;


FsmEventTarget _eventTarget = new FsmEventTarget();
_eventTarget.excludeSelf = false;
FsmOwnerDefault owner = new FsmOwnerDefault();
owner.OwnerOption = OwnerDefaultOption.SpecifyGameObject;
owner.GameObject = new FsmGameObject();
owner.GameObject.Value = fsm.gameObject;
_eventTarget.gameObject = owner;
_eventTarget.fsmComponent = fsm;
_eventTarget.target = FsmEventTarget.EventTarget.GameObjectFSM;

_eventTarget.sendToChildren = false;

fsm.Fsm.Event(_eventTarget,eventName);
}
}


drop that on a gameobject, drag an fsm into the fsm field and everytime you press a key it will send that event with that string data.

Bye,

 Jean

Palanysamy

  • Playmaker Newbie
  • *
  • Posts: 14
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #5 on: August 14, 2014, 05:28:54 PM »
When I grow up I want to be like you. It works!

Thanks!

P.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #6 on: August 15, 2014, 09:05:43 AM »
Hi,

 :)

 How old are you?

Bye,

 Jean


Palanysamy

  • Playmaker Newbie
  • *
  • Posts: 14
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #7 on: August 15, 2014, 10:06:37 AM »
30+ hahahahahaha.

have a great weekend Jean.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #8 on: August 15, 2014, 01:35:54 PM »
 :P

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #9 on: November 06, 2018, 12:21:45 PM »
The wretched darkness spreads over the land.
It is a time of danger, of monsters and ancient forbidden magic.
Above all things, now is a time for thread necromancy!


And here we go. Something like fours years later. Yep. The thread is ripe.

So I've been reading this and I have a quest... ion.

In the code there's that bit in the SendEvent() method:

Code: [Select]
FsmEventTarget _eventTarget = new FsmEventTarget();
If I understand it right, this is going to produce an instance every time this method is called. But for something as intensive as pressing a key in a game, isn't it going to clutter the memory?
« Last Edit: November 06, 2018, 01:07:45 PM by Broken Stylus »

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #10 on: November 06, 2018, 02:39:28 PM »
Also, was is this part doing exactly:

Code: [Select]
HutongGames.PlayMaker.Fsm.EventData = eventData;
Fsm is a class so I tried to find a script with that name but maybe it's a nested one, I don't know. I wanted to do that to look into the script and understand what this "EventData" thing is all about so as to understand why there is this eventData value that's stored in it.

All of this is related to an attempt of mine to have a script glued to a game object, that waits for events sent by either a SomethingSomething.mm file for iOS or the equivalent in Android; a proxy or bridge of some kind, so it looks like this on iOS:

File.mm -> sends "normal" Unity c# event to c# script on a gameobject -> in that script, the received event activates a method (been stored with the += stuff on Awake) that launches the corresponding Playmaker-event to a FSM (likely a component on the same game object) -> said FSM receives the Playmaker-event (does the event need to be global? I don't think so) and does stuff in the FSM as usual.

This custom script I'm creating, put as a component on the game object, is meant to listen to several events coming from the same iOS file, and each Unity event has a corresponding Playmaker event to be forwarded.
« Last Edit: November 06, 2018, 02:49:53 PM by Broken Stylus »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #11 on: November 07, 2018, 01:08:48 AM »
Hi,

- yes, you can optimize this script by caching some of its classes.

- when you send an event, you can set event data by storing it in this HutongGames.PlayMaker.Fsm.EventData, it's the mechanism for it. you normally use GetEventInfos ( and setEventData) actions to access this.

 Bye,

 Jean

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #12 on: November 07, 2018, 08:59:04 AM »
Thanks Jean,

One general question: the code above is meant to send a FsmEvent to a FSM, right?

Hi,

- yes, you can optimize this script by caching some of its classes.

Let me guess. It means I need to push the instance creation into the Start() phase, right? I suppose there may be other things from that script I could move too, since I see other "new MoreStuff" lines below the one I quoted, although they are closely related to FSM functions, such as:

  • FsmEventTarget _eventTarget = new FsmEventTarget();
  • FsmOwnerDefault owner = new FsmOwnerDefault();
  • owner.GameObject = new FsmGameObject();

I would consider moving them too into the Start() phase, would that be fine?

Quote
- when you send an event, you can set event data by storing it in this HutongGames.PlayMaker.Fsm.EventData, it's the mechanism for it. you normally use GetEventInfos ( and setEventData) actions to access this.

But what is .EventData exactly?
In Playmaker you can send plenty of events at the same time, from different FSMs, each event with its own data set. But there I don't get the syntax as it seems to be pointing to one single element.
I mean, I'd have expected something more like MyFsmEvent.EventData with EventData being (I'm totally guessing) something like a dictionary considering the kind of stuff that goes in the whole data set of an event.

But with HutongGames.PlayMaker.Fsm.EventData, at no point I even see a reference to a unique event or an instance of something.
HutongGames? Nope. Playmaker? Nope. Fsm? Still nope. And suddenly... EventData!
If I have several events to store data into, how is that even going to work?

Or is there something in that "EventData" that parses and understands that depending on what it is fed with (in this case it's "= eventData") , it will generate a (temporary?) data set for that particular eventData, because eventData contains an ID for the event, plus a series of plenty of other values?

If I have several events, with their own data, would something like this work:

Code: [Select]
HutongGames.PlayMaker.Fsm.EventData = eventData1;
HutongGames.PlayMaker.Fsm.EventData = eventData2;
HutongGames.PlayMaker.Fsm.EventData = eventData3;

And produce discrete sets of data for each separate event?
To me it doesn't look like it would work. I don't see how the code would have a way to recall the data of event 1, event 2, etc. It looks like a placeholder for one single event, one at a time.
So is .EventData meant to be used for only one event and the coder MUST do everything that is necessary with that event (like sending it, for starters) before using .EventData for another event right after that?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: SEND EVENT DATA FROM C# SCRIPT
« Reply #13 on: November 08, 2018, 12:33:31 AM »
Hi,

On optimization, I think you are doing it the wrong way. If you are starting with c# coding, simply get ti to work first, then optimize, it will save you precious time.

So right now, just make it work, once you have a feature working, you can profile it and see if scripts is the problem and then address it if necessary, but only if it's necessary.

.EventData is a unique recipient indeed, you can't stack it, so yes you are right, when event data must be read before it's changed again and if you have two consecutive events where you set event data ( using the action SetEventData), all fsm that wants to get the event data must do that as early as possible, and in all cases earlier then the next time you'll send another event with some new event data.

Bye,

 Jean