playMaker

Author Topic: Passing variables to Script Methods  (Read 12362 times)

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Passing variables to Script Methods
« on: August 17, 2011, 01:28:36 AM »
I have a script that controls my camera with a basic mouse orbit and scroll zoom with some keyboard keys in there as well for fall backs.

In some cases when i am changing the focus of the camera i want to control the zoom. So i figured i would need to use the Invoke Method Action.. I think i know how to modify my JS script to include a method for changing the zoom. But i have to pass a variable to it so that i can set it to what ever i want and  not something hard coded... how do i do that?

Maybe i am supposed to call them arguments that i want to pass instead of variables.. not sure.


Q

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Passing variables to Script Methods
« Reply #1 on: August 17, 2011, 10:18:19 AM »
Hi,

 I had to do something like that and the trick is the following:

in your script implement a function with a fsm param, not a regular var, an actual fsmXXX var, for this you need to include the

Code: [Select]
using HutongGames.PlayMaker;
with a function like that:


Code: [Select]
void ReadWriteFsmVector3(FsmVector3 outVector3){

// read
Debug.Log(outVector3.Value);

// write
outVector3.Value = **some Vector3 ***;


}// ReadWriteFsmVector3

 so from playmaker you use the sendMessage action that target this component method, and fill a vector3 parameter.

 The beauty of this is that you can read write on both side ( because of the nature of fsmxxx objects), that is you can send say a the current score of the user, and have your script updating the score, the next action can already work with the update fsm variable.

 Does that make sense?

 Bye,

 Jean

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #2 on: August 17, 2011, 10:58:18 AM »
Thanks Jean!!!


It almost makes sense... ::)

How can i get the script to write back to my FSM? Not sure i need that at this moment but i am sure i will in the future..

I was actually toying with the idea of just writing an action to do my camera controls instead of using an external script.. which might be a good idea anyway as the script is not super complicated and i will be reusing it anyway.. But i also need to know how to talk to scrips as this will come up over and over i am sure of it.

Q


qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #3 on: August 17, 2011, 11:07:13 AM »
My original script is in Java.... Do i need a C# version to make this work?

I have just learned by hacking existing scripts so i am still missing some basic knowledge. But i do like to think that is getting smaller bit by bit.

Q

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #4 on: August 17, 2011, 11:51:59 AM »
Ok i found my original script in C# so i will edit it to match what i had and then implement.

Has anyone used Python with Unity?

My buddy i work with sometimes uses Python for everything and says i should really convert to it. And i have seen that there is a Python extension for Unity i think.

Q

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #5 on: August 17, 2011, 12:49:43 PM »
Ok.... I got this to work but not with a FsmFloat.... I got it to work with a normal float though.... If i tried the FsmFloat then it gave me an error message that there were bad parameters so i guessed that when i set the type of float in the SendMessage Action it was a normal float it was sending instead of a FsmFloat...

So maybe i have to edit the Send Message Action to include Fsm variables??

if it works with just a normal float why do i need the FsmFloat?

Is that enough questions?

Q

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #6 on: August 17, 2011, 12:53:43 PM »
Yep that was it... if i modify the SendMessage Action then it will send a FsmFloat instead of just the .Value of it.. Then it works.

But i still dont know why i would want the Fsm version.

Q

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #7 on: August 17, 2011, 02:03:52 PM »
Ok so i modified the SendMessage Action to allow for Fsm variables via a bool switch. I also added EveryFrame bool to it as well... but i cant figure out how to get a value from my Script... I am modifying a value over time with EaseFloat but if i dont know where i am starting from then i cant make it work...

Q

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #8 on: August 18, 2011, 12:13:00 AM »
Ok this is as far as i can get on this i think... I can get a value back from a function but i have to hard code the script name and the function name... I would love to be able to use a variable for the Script Name but i have no idea how?

Anyone?

Code: [Select]

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("Retrieves a Float variable from a function.")]
public class GetFunctionVariable : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

public FsmString functionName;

public FsmFloat back;

public FsmBool everyFrame;

private cameraMouseOrbit script;


public override void Reset()
{
gameObject = null;
back = 0;
everyFrame = false;
functionName = null;
}

public override void OnEnter()
{
DoGetVariable();

if(!everyFrame.Value) Finish();
}

public override void OnUpdate()
{
DoGetVariable();
}

void DoGetVariable()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);

if (go == null)
{
return;
}

script = go.GetComponent("cameraMouseOrbit") as cameraMouseOrbit;

back.Value = script.getDist();

}
}
}

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #9 on: August 18, 2011, 12:47:38 AM »
Well should we just delete all this junk....

I found this post and followed Jeans example and it worked awesome!

http://hutonggames.com/playmakerforum/index.php?topic=95.0

Wish i had found it earlier... oh well.

Sorry for all the chatter.

Q

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Passing variables to Script Methods
« Reply #10 on: August 18, 2011, 02:29:40 AM »
Hi,


 you want to use  fsmXXX params during this kind of communication because they allow both sides to read and write to it, using a normal float or vairable is not that easy plus playmaker will just not be able to conveniently read that.


 I know I had this thread done some time ago, but just could not find it...

 ALSO: be very careful if you plan on using sendmessage in your game loop. This is not a good idea, it's a not a fast and efficient methods of communication for intense and repeating calls. Instead host a reference of your script and talk to it directly, that will be a lot more efficient in many ways if you need to constantly update variables.

Bye,

 Jean

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #11 on: August 18, 2011, 09:55:52 AM »
yea wish i had found that post earlier but i did learn some stuff.. the hard way of course. What other way is there....

Good to know that the SendMessage is slow... but i am not doing a game and it is not used a lot just for short transitions now and then. My project is a commercial product explorer kind of thing...

My first project.... too big to start on really and wayyyyyyyyyyyyyyyy over due.. Throw in a huge life crisis and a new baby and never ending regular work and you get.... LIFE    take it or leave it.  ;D

Thanks Jean

Q

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Passing variables to Script Methods
« Reply #12 on: August 18, 2011, 03:09:07 PM »
Hi,

 By "game loop" I mean the core of your system that runs. when a button is pressed, you can afford a slow send message, or when events happens occasionally and once . But for example if your camera is following something even for a short period of time, don't use sendmessage to communicate between the camera and that thing, that would be a very bad flow. Try with and try without, you'll the performance hit clearly in the stats.

 You need to start somewhere, so take it :) My first project with Unity was the one project with the most rock solid deadline I have ever had to deal with in like ten years of freelancing... I delivered, but that was hard indeed from a life point of view I give you that. I guess you can't really move forward without such compromise. I mean working in a post office, you don't really get that kind of rush or pressure ( I think...) but then you get bored of it ( of that I am sure...).

 Bye,

 Jean

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Passing variables to Script Methods
« Reply #13 on: August 18, 2011, 03:15:27 PM »
Yes i get what you mean by a main game loop.. Thanks.. I dont call it on a regular basis.. I guess if i need to interact with it more then i should maybe just build the script into an Action then...

Or access it using something like the script that i had up above here where i return a value from getting a component.. but i still dont understand how i can make that Action more generic and useful... probably a way using some internal PlayMaker functions or variables that i do not know about..

Being a freelancer is exciting... sometimes terrifying too..  ;D

Q

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Passing variables to Script Methods
« Reply #14 on: August 19, 2011, 01:51:57 AM »
Hi,

 Well, if you don't mind sharing this, we can discuss it and find ways to make it generic or define some design pattern, I think it's very important with playmaker actually. It's not necessarily the generic nature but most posts are really about understanding and learning design patterns ( the case for any framework actually).

Bye,

 Jean