Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: akadow on October 30, 2012, 03:57:45 PM
-
Hello! I tried to find the answer in the forums but I could not make it work
I have a global variable (float) named clamp_cam in playmaker that is updated every frame by a FSM.
How to access this variable in my C# script? I'm having this error: Assets/Scripts/clampedXY.cs(28,39): error CS0103: The name `FsmVariables' does not exist in the current context
The script:
using UnityEngine;
using System.Collections;
using HutongGames;
public class clampedXY : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var globalVariables = FsmVariables.GlobalVariables;
var clamp_cam = globalVariables.GetFsmInt("clamp_cam");
float x_clamped = Mathf.Clamp(transform.position.x, (clamp_cam -1f), (clamp_cam +1f));
float y_clamped = Mathf.Clamp(transform.position.y, -0.83f, 0.83f);
transform.position = new Vector3(x_clamped, y_clamped, 0);
}
}
PS: sorry for my english, I'm from Brazil
-
Hi,
Several errors here, let me correct them:
1:
using HutongGames.PlayMaker;
Use this instead of just HutongGames
2: to get to a variable value, you need to do the following:
string _value = FsmVariables.GlobalVariables.GetFsmString("globalString").Value;
notice the .Value call. A Fsm variable is a wrapper, not the actual value itself, so always use .Value to get to it.
3: you can optimize this behavior by only getting the FsmInt once on start and simply query for its value every update. This is slightly more efficient this way.
using UnityEngine;
using System.Collections;
using HutongGames.PlayMaker;
public class clampedXY : MonoBehaviour
{
private FsmInt _clamp_camFsmInt;
// Use this for initialization
void Start ()
{
// we get the FsmInt here, once for all. No need to query for it every update.
_clamp_camFsmInt = FsmVariables.GlobalVariables.GetFsmInt ("clamp_cam");
}
// Update is called once per frame
void Update ()
{
// but we do get the value of that Fsm Int on every update.
int clamp_cam = _clamp_camFsmInt.Value;
float x_clamped = Mathf.Clamp (transform.position.x, (clamp_cam - 1f), (clamp_cam + 1f));
float y_clamped = Mathf.Clamp (transform.position.y, -0.83f, 0.83f);
transform.position = new Vector3 (x_clamped, y_clamped, 0);
}
}
bye,
Jean
-
I got no errors but it does not work the way I expected. The variable clamp_cam is being updated every frame in FSM but in my script it only takes the first update. I want my variable that generates the "clamp" update everyframe in accordance with the lateral movement of the screen (not works even when I put _clamp_camFsmInt = FsmVariables.GlobalVariables.GetFsmInt ("clamp_cam"); on void Update)
Thanks!!!
-
hi,
It works perfectly well here. Not sure what you expect form this, but it does the job. And If I edit the clamp_cam global var, it does react accordingly and update as I change the value in the globals inspector.
bye,
Jean
-
1. I have a camera moving to the right eternally
2. This camera refreshes a global variable on FSM (clamp_cam) every frame. FSM / get position / every frame
3. In the Playmaker panel I see the variable being updated at every moment (0,1,2,etc)
4. The variable clamp_cam im my C# script get only the first update. I see only the first update using print (clamp_cam) n consoloe
Sorry if I'm doing something wrong but I'm pretty newbie with Unity/Playmaker
-
hi,
yes, it seems you are bitting your tail. is it possible to send the project or the scene or share screen shots of you fsm?
bye,
jean
-
Sure! I sent you a link by PM. It's only a small project to understand Unity and Playmaker.
-
Hi,
I see the problem now. Ok allow me few days, I will modify the jump platform game to scroll automatically to the right and clamp the user ( not the camera) within the current visible area of the camera.
bye,
Jean
-
Thanks!!
-
This post really helped me to understand how to tell my script to reach out to an FSM and grab a variable. Thank you.
-
This post really helped me to understand how to tell my script to reach out to an FSM and grab a variable. Thank you.
Same. This info should probably bee added somewhere on a website.
-
Hi.
I believe you can finds samples on the Playmaker API (https://hutonggames.fogbugz.com/default.asp?W127)