Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: pedroathumanspot on January 14, 2013, 01:20:49 PM
-
Hi there!
Is there any way (action) to call an Object's function and store the result in a FSM variable?
Cheers.
-
Hi,
There is a way, but it's not an obvious, nor direct way.
to call a method on a Behavior, you can use two actions:
-- Send Message
-- Invoke
but none will let you "reply" back, so to reply back, you can, either set a public property on the behavior you are calling, and after you have called the method, you use "get property" on the same behavior and get your result like that.
does that make sense?
bye,
Jean
-
Hey Jean, thanks for the reply.
does that make sense?
Yes and no. :D It does solve the problem, yes, but it doesn't feel quite right - it was better to have some action to do that.
In that sense, and if you wan't to have some sort of protection/verification on that public property, I remembered another way to do that through C# assessors (http://msdn.microsoft.com/en-us/library/aa287786(v=vs.71).aspx). Like this:
private int alwaysPositive;
public int AlwaysPositive{
get{
return alwaysPositive;
}
set{
if(value<0) alwaysPositive = 0;
else alwaysPositive = value;
}
}
Even so, I find it more comfortable to have an action that does this. (I guess I'll go with this assessor solution by now. If I code a custom action to do this, I'll post.)
Cheers.
-
Hi,
yes, by all means, if you can code a custom action, this is the best way, I do that always on such cases. PlayMaker general actions can not be that specific, because each behavior is unique.
I did create a while ago a sendmessage that would allow a vector3 to be passed back. The good thing about FsmVariables is that it's quite easy actually, BUT it means that the method you call implements the proper signature.
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("Sends a Message to a Game Object with a specific variable holding reference to a fsmVector3. See Unity SendMessage docs.")]
public class SendMessageVector3Out : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
public SendMessageOptions options;
[RequiredField]
public string functionName;
[UIHint(UIHint.Variable)]
public FsmVector3 outVector3;
public override void Reset()
{
gameObject = null;
options = SendMessageOptions.DontRequireReceiver;
functionName = "";
}
public override void OnEnter()
{
if (gameObject.OwnerOption == OwnerDefaultOption.UseOwner)
DoSendMessage(Owner);
else
DoSendMessage(gameObject.GameObject.Value);
Finish();
}
void DoSendMessage(GameObject go)
{
go.SendMessage(functionName,outVector3,options);
return;
}
}
}
bye,
Jean
-
Hi Jean,
Could you please show me what the receiving function will look like when its is called by your SendMessageVector3Out action.
I am trying to test this and cannot seem to set the Vector 3 variable.
Thanks
Matt
-
Hi,
You have to set the .Value of the FsmVector3 Variable, that's how it works.
public void NormalizeVector3(FsmVector3 vector )
{
vector.Value = vector.Value.normalized;
}
I have attached a working scene.
Bye,
Jean
-
Thanks Jean, Got it :)