Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: zhuxianzhi on August 04, 2017, 03:08:29 AM

Title: How do I send custom data to PlayMakerFSM?
Post by: zhuxianzhi on August 04, 2017, 03:08:29 AM
I would like to send some custom data when two game objects collide.


Code: [Select]
        private void TryDoAttack(Collider2D collider2D)
        {
            var targetFsm = collider2D.gameObject.GetComponentInParent<PlayMakerFSM>();

            if (targetFsm != null)
            {
                //    targetFsm.Fsm.GetFsmObject("");   //Send custom data
                targetFsm.SendEvent("Beaten");
            }
        }
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: jeanfabre on August 04, 2017, 03:34:12 AM
Hi,

 For this you have two options ready made.

1:

 before you send the FsmEvent, setup HutongGames.PlayMaker.Fsm.EventData

create a new EventData, fill data ( string, int, etc etc), and assign it to HutongGames.PlayMaker.Fsm.EventData, it's a static property so it affects everything ( that's why you need to set it one line before sending the event.

with this you can them use GetEventInfo action as usual.

2: If your data is complex, typically with several ints or floats, you can use PlayMaker Utils ( from the Ecosystem, you may already have it).

this time you set SetEventProperties.properties, and fill it with keyValue pairs like:

SetEventProperties.properties["My Value"] = something;

then you use GetEventProperties action to retrieve "My Value" and save it into an Fsm variable

Let me know if you have more questions.


 Bye,

 Jean
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: zhuxianzhi on August 06, 2017, 10:20:09 PM
Hi,

 For this you have two options ready made.

1:

 before you send the FsmEvent, setup HutongGames.PlayMaker.Fsm.EventData

create a new EventData, fill data ( string, int, etc etc), and assign it to HutongGames.PlayMaker.Fsm.EventData, it's a static property so it affects everything ( that's why you need to set it one line before sending the event.

with this you can them use GetEventInfo action as usual.

2: If your data is complex, typically with several ints or floats, you can use PlayMaker Utils ( from the Ecosystem, you may already have it).

this time you set SetEventProperties.properties, and fill it with keyValue pairs like:

SetEventProperties.properties["My Value"] = something;

then you use GetEventProperties action to retrieve "My Value" and save it into an Fsm variable

Let me know if you have more questions.


 Bye,

 Jean

感谢回复,第二种方式我不是很明白,请问可以可以写一点代码实例吗
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: jeanfabre on August 07, 2017, 10:23:30 AM
Hi,

 Ok, I'll write a quick sample on this and actually do a wiki page on this, this will be better.

Bye,

 Jean
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Hayato on March 27, 2020, 08:59:01 AM
Hi,

Is there a sample code about SetEventProperties.properties now ?
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: djaydino on March 27, 2020, 04:07:26 PM
Hi

Maybe the Playmaker API (https://hutonggames.fogbugz.com/default.asp?W127) can Help
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: neteeyore@comcast.net on March 27, 2020, 11:00:36 PM
I would also be interested in a better understanding of the second option that Jean was describing.
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: jeanfabre on March 30, 2020, 02:25:16 AM
Hi,

 What is it that doesn't work on your end based on what I wrote?

Bye,

 Jean
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Hayato on March 30, 2020, 07:18:51 AM
Hi,

 What is it that doesn't work on your end based on what I wrote?

Bye,

 Jean

In the https://hutonggames.fogbugz.com/default.asp?W127 (https://hutonggames.fogbugz.com/default.asp?W127) section I don't found any example using :
Code: [Select]
SetEventProperties.properties["My Value"] = something;Please help me to give a hint if there is a example for it there.

Or can you just write a short example code for example how to send a int value with a key ? (And we can recive the value using GetEventProperties action)

Thank you !
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: jeanfabre on April 03, 2020, 02:35:26 AM
Hi,

 you need to fire a playmaker event, are you doing this? meaning inside the playmaker fsm, you check for the event and use GetEventProperties then?

Bye,

 Jean
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Hayato on April 03, 2020, 03:11:10 AM
Hi

I know it need to fire a event , but I'm not fimilar with C# so I can not figure out how to writre the script correctly .
So it will help if there is a sample code , or can you help me correct the code below to send a int ?

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;
using HutongGames.Utility

public class MyBehaviour : MonoBehaviour
{
    public PlayMakerFSM fsm;
    public int AAA;

    void Update()
    {
        // i don't know how to use it
        SetEventProperties.properties["DATA1"] = AAA;

        // sending an event
        fsm.SendEvent("FireEvent");
    }
}


Title: Re: How do I send custom data to PlayMakerFSM?
Post by: jeanfabre on April 03, 2020, 03:44:26 AM
Hi,

 yes, that's right, that's all you need.

 in fsm, you use GetEventProperties and get the value for key "DATA1"

but if you want to send a value every update, then you are better off setting an FsmString on that fsm directly, since you already have a pointer to it.

Bye,

 Jean
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Hayato on April 03, 2020, 01:21:12 PM
Hi,

 yes, that's right, that's all you need.

 in fsm, you use GetEventProperties and get the value for key "DATA1"

but if you want to send a value every update, then you are better off setting an FsmString on that fsm directly, since you already have a pointer to it.

Bye,

 Jean

The code what I write have error in console , it says:

Code: [Select]
Assets\SendToFSM.cs(13,9): error CS0103: The name 'SetEventProperties' does not exist in the current context
So I think it's not correct , but don't know how to correct it .
Can you give more hint about it ?
Thank you !
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: jeanfabre on April 08, 2020, 02:22:20 AM
Hi,

 Do you have the SetEventProperties action in your project?

Bye,

 Jean
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Hayato on April 09, 2020, 04:41:30 AM
Hi,

 Do you have the SetEventProperties action in your project?

Bye,

 Jean

Hi jean,

Yes, I install PlayMakerUtils package via ecosystem and both SetEventProperties and GetEventProperties can see in actions broswer .

---
PlayMaker: 1.9.0.p20
Unity: 2019.3.7f1
Build Target: StandaloneWindows
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Hayato on April 09, 2020, 05:06:54 AM
Okay finally I got the answer,  it do not need :
Code: [Select]
//using HutongGames.PlayMaker;
//using HutongGames.Utility;

but need to add  :
Code: [Select]
using HutongGames.PlayMaker.Actions;
The full sample code send a int with key that work to me is:

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker.Actions;

public class SendToFSM : MonoBehaviour
{
    public PlayMakerFSM fsm;
    public int AAA;
   
    void Update()
    {
        // Set variables with key
        SetEventProperties.properties["DATA1"] = AAA;

        // sending an event
        fsm.SendEvent("myEvent");
    }
}

The function is really powerful ! : )
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Broken Stylus on April 10, 2020, 06:11:28 AM
SetEventProperties.properties["My Value"] = something;

I might be wrong here but this sounds a lot like a Hash Table pair, name:value.
Which makes me wonder, out of curiosity, if I missed something here, like being able to directly input a key and its value into an event instead of having to save the name as a string on one side, and its associated var on the other.
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Hayato on April 10, 2020, 06:32:23 AM
SetEventProperties.properties["My Value"] = something;

I might be wrong here but this sounds a lot like a Hash Table pair, name:value.
Which makes me wonder, out of curiosity, if I missed something here, like being able to directly input a key and its value into an event instead of having to save the name as a string on one side, and its associated var on the other.

Do you mean ways like the image attached below ?
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Broken Stylus on April 13, 2020, 07:11:58 AM
Something like that, yes.
It would have to combine this code above that creates custom properties to be set into an event, with a basic action that gets a key + value at a given index from a Hash Table.
Title: Re: How do I send custom data to PlayMakerFSM?
Post by: jeanfabre on April 14, 2020, 01:14:39 AM
Hi,

 it's then likely because you are not including the namespace for it at the top of your script:

using HutongGames.PlayMaker.Actions;


normally your ide where you edit scripts should give you some hints that it is necessary and even do it for you. I use Rider myself and did not look back.

Bye,

 Jean

Title: Re: How do I send custom data to PlayMakerFSM?
Post by: Hayato on April 14, 2020, 07:00:52 AM
Hi,

 it's then likely because you are not including the namespace for it at the top of your script:

using HutongGames.PlayMaker.Actions;


normally your ide where you edit scripts should give you some hints that it is necessary and even do it for you. I use Rider myself and did not look back.

Bye,

 Jean

Like some of PlayMaker users ,I don't fimiliar with both C# and IDE , even don't know clearly about namespace meaning before the problem ,that's the reason why I want to get a full sample code to learn how to use the function .

Thank you for make sure that my code is correct , or in my own knowledge it's impossiable to go to the next step that IDE adventitiously help me.  :)

Title: Re: How do I send custom data to PlayMakerFSM?
Post by: jeanfabre on April 15, 2020, 01:56:37 AM
Hi,

 ok, I have made a new sample on the Ecosystem called "SetFsmFloatVariableValue", get that and check the script "SetFsmFloatVariableValue" in the scene "SetFsmFloatVariableValue", it will show you how to properly set the value of an fsm variable.

here's the code:

Code: [Select]
using HutongGames.PlayMaker;
using UnityEngine;

public class SetFsmFloatVariableValue : MonoBehaviour
{

public PlayMakerFSM Fsm;

public string FsmFloatVariableName = "My Fsm Float";
public float SetFsmFloatValue;

private FsmFloat _fsmFloatVariable;

void Start () {
if (Fsm == null)
{
Debug.LogError("Missing Fsm reference");
return;
}

_fsmFloatVariable = Fsm.Fsm.GetFsmFloat(FsmFloatVariableName);

if (_fsmFloatVariable == null)
{
Debug.LogError("<"+FsmFloatVariableName+"> could not be found in Fsm");
return;
}

SetValue();
}

// Update is called once per frame
void Update ()
{
SetValue();
}

void SetValue()
{
if (_fsmFloatVariable != null)
{
_fsmFloatVariable.Value = SetFsmFloatValue;
}
}
}


Bye,

 Jean