playMaker

Author Topic: Sending event or variable to FSM via Unity Script [solved]  (Read 20497 times)

amedux

  • Playmaker Newbie
  • *
  • Posts: 4
Sending event or variable to FSM via Unity Script [solved]
« on: March 20, 2013, 10:50:27 AM »
Hi there,
I've set up some FSMs on several objects and they all work fine.
Now I'd like to send and event (or variable value) to one of those via Unity script.

The idea is to create a script you can attach to ANY object, point it at one specific object, query its FSMs (so that we can choose which one to use, if more than one), and send the desired FSM an event (and or variable value).
It sounds ridicolously easy to me, but I can't get to the point of actually getting the FSM names.
With the following script I managed to list all the FSMs of one object.

The question is, how can I get the name of the FSM from each object in my FSMlist?

Code: [Select]

#pragma strict

//the object we want to scan for FSMs
public var obj: GameObject;

//the reference name of the desired FSM
public string desiredFSM;


function Start ()
{

var FSMlist = new Array();
FSMlist = obj.GetComponents.<PlayMakerFSM>();

print (FSMlist);   
}



If I then try and

Code: [Select]
print (FSMlist.FSMname);

For each of the elements in FSMlist, Unity tells me that 'FSMname' is not a member of 'Object'.
If I
Code: [Select]
print (FSMlist);
as I'm currently doing, the console reports a
Code: [Select]
nameofobject(PlaymakerFSM)
for each FSM attached to the desired object, where nameofobject is the name of the desired object, and PlaymakerFSM is written as it is.

I'm an ultra noob, currently learning unity script, and all the examples I could find are in C# and I'm not so used to the syntax porting.
Thank you for any help you can give me!
« Last Edit: March 21, 2013, 09:46:33 AM by amedux »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Sending event or variable to FSM via Unity Script
« Reply #1 on: March 20, 2013, 10:57:57 AM »
The idea is to create a script you can attach to ANY object, point it at one specific object, query its FSMs (so that we can choose which one to use, if more than one), and send the desired FSM an event (and or variable value).

I'm not sure about your scripting questions, but I'm curious why you couldn't use a broadcast event or ArrayMaker for this? Seems like you could accomplish the same thing without having to deal with any scripting.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

amedux

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Sending event or variable to FSM via Unity Script
« Reply #2 on: March 20, 2013, 02:53:33 PM »
I have several scripts that needs to run on their own without the need of any FSM, but a some points they have to send events or variable values to trigger FSMs. So the information always originates within the indipendent script and is then actively passed to a FSM. I would integrate the above posted script into my existing scripts.

That's the way I thought it might work in my head, but I haven't been coding for so long, so my solutions aren't necessarily wise.

Do you think that doesn't make sense?
 
I didn't think about Arraymaker as a solution. In which way would that spare me the scripting part?

When you mentioned "Broadcast Event" are you referring to Playmaker events or to Unity's "BroadcastMessage"?


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sending event or variable to FSM via Unity Script
« Reply #3 on: March 21, 2013, 01:11:47 AM »
Hi,

 From your description of what you want do to, I am not sure where you find a problem to implement this? is it a case where you don't want to use playmaker to do that? or is it a case where you don't know how to do that in playmaker to begin with?

 have a fsm, and use a "send event" action, it lets you select an gameObject, then it will list all fsm, and you can then select a global event to send it.

Maybe I do not understand the actual feature you want to build... Could you explain it again then? I am willing to understand and help.

bye,

 Jean

amedux

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Sending event or variable to FSM via Unity Script
« Reply #4 on: March 21, 2013, 09:39:03 AM »
Hi Jean,
I think I wasn't clear in my explanation, indeed. My FSMs are working like a charm.
Originally I just wanted to learn how to send events from a script to a FSM, because I had a turret which was instantiating projectiles. The projectile would be a gameobject with a FSM on it telling it to speed up and destroy itself on collision (+ all the damage data). Obviously that meant that the original bullet, from which others are instantiated, would speed up at the beginning of the game and would travel forever while the game was on. I'm sure it isn't an elegant implementation, I'm still learning.
So I thought of having the turret script sending a "thisBulletHasBeenShot" event to each instantiated bullet, so to trigger the whole add force and destroy on collision thing within the FSM. THis way the original bullet wouldn't zip around my scene at Game Start.
I just couldn't grasp the syntax of how to get a specific FSM and work on it, since all the examples I found are in C#.
As you see it is nothing fancy. I see in the end it is more a Unity question than a Playmaker one, and it could be dealt with with just a tiny bit more of research, so I have to thank you and Lane for your patience. :)

I see you're quite an authority around here. Would you think there's a better direction I should be pointing at?


I solved it now:

Code: [Select]
#pragma strict
//public var script: MonoBehaviour;

//The object I want to scan for FSMs
public var obj: GameObject;

//The reference name of the FSM I want to talk to
public var FSMreferenceName: String;

//The event I want to broadcast
public var eventText: String;

private var FSMs : PlayMakerFSM[];

function Start ()
{
FSMs = obj.GetComponents.<PlayMakerFSM>();
}


function Update ()
{

for (var fsm : PlayMakerFSM in FSMs)
{

if (fsm.FsmName == FSMreferenceName)
{
fsm.Fsm.Event(eventText);
}
}

}

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Sending event or variable to FSM via Unity Script [solved]
« Reply #5 on: March 21, 2013, 10:01:42 AM »
On the projectiles I've done, I simply make a prefab and tell it to create/instantiate that prefab. It includes a destroy after time and destroy on impact switch so it doesn't exist indefinitely. The first one doesnt exist in the scene and if it needed to then you could separate the start (idle) state and the fired (start moving) state that is activated by the firing weapons' state to keep the original frozen in place.

From what I understand its best to use a pooling solution for that kind of thing since creating new objects like I'm doing is not cheap.

I don't see a need for the script when all of the send event, broadcast and get/set property actions are at your disposal to reach between FSM's and run any state you want, or send any command to any variable for that matter. That's the confusing part.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sending event or variable to FSM via Unity Script [solved]
« Reply #6 on: March 22, 2013, 01:11:37 AM »
Hi,

 Yes, if you are willing to go crazy with projectiles and everything surrounding its management, I would encourage you check out TargetPro, and pool Manager, from path-o-logical. They have a full blown solution for your need I think, and I ported them to playmaker, so if double check it here and if you have questions let me know:

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


bye,

 Jean

amedux

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Sending event or variable to FSM via Unity Script [solved]
« Reply #7 on: March 22, 2013, 11:00:03 AM »
@Lane
thank you, now I see why I don't need to script for what I'm trying to do.

@Jean
thank you, too. TargetPro is very tempting!