playMaker

Author Topic: Call Object function and store result[SOLVED]  (Read 4382 times)

pedroathumanspot

  • Playmaker Newbie
  • *
  • Posts: 11
Call Object function and store result[SOLVED]
« 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.
« Last Edit: January 23, 2014, 02:54:01 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Call Object function and store result
« Reply #1 on: January 14, 2013, 01:40:27 PM »
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

pedroathumanspot

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Call Object function and store result
« Reply #2 on: January 14, 2013, 01:51:49 PM »
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. Like this:

Code: [Select]
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.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Call Object function and store result
« Reply #3 on: January 14, 2013, 02:20:12 PM »
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.


Code: [Select]
// (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

wickedw

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Call Object function and store result
« Reply #4 on: January 08, 2014, 04:17:36 AM »
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


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Call Object function and store result
« Reply #5 on: January 10, 2014, 08:46:18 AM »
Hi,

 You have to set the .Value of the FsmVector3 Variable, that's how it works.

Code: [Select]
public void NormalizeVector3(FsmVector3 vector )
{
vector.Value = vector.Value.normalized;
}

I have attached a working scene.

Bye,

 Jean

 

wickedw

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Call Object function and store result
« Reply #6 on: January 15, 2014, 08:56:37 AM »
Thanks Jean, Got it :)