playMaker

Author Topic: Playmaker global variables to C# script  (Read 22109 times)

akadow

  • Playmaker Newbie
  • *
  • Posts: 8
Playmaker global variables to C# script
« 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

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Playmaker global variables to C# script
« Reply #1 on: October 31, 2012, 01:37:34 AM »
Hi,

 Several errors here, let me correct them:

1:
Code: [Select]
using HutongGames.PlayMaker;

Use this instead of just HutongGames

2: to get to a variable value, you need to do the following:
Code: [Select]
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.

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

akadow

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Playmaker global variables to C# script
« Reply #2 on: October 31, 2012, 08:24:24 PM »
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!!!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Playmaker global variables to C# script
« Reply #3 on: November 01, 2012, 07:25:00 AM »
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

akadow

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Playmaker global variables to C# script
« Reply #4 on: November 01, 2012, 08:30:21 AM »
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

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Playmaker global variables to C# script
« Reply #5 on: November 01, 2012, 09:21:40 AM »
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

akadow

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Playmaker global variables to C# script
« Reply #6 on: November 02, 2012, 11:10:06 AM »
Sure! I sent you a link by PM. It's only a small project to understand Unity and Playmaker.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Playmaker global variables to C# script
« Reply #7 on: November 05, 2012, 02:00:31 AM »
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

akadow

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Playmaker global variables to C# script
« Reply #8 on: November 05, 2012, 05:55:20 PM »
Thanks!!

velketor

  • Junior Playmaker
  • **
  • Posts: 69
  • Multimedia Marathon Man
    • Shawn Kilian Portfolio
Re: Playmaker global variables to C# script
« Reply #9 on: August 24, 2017, 09:56:40 AM »
This post really helped me to understand how to tell my script to reach out to an FSM and grab a variable.  Thank you.

Gua

  • Beta Group
  • Sr. Member
  • *
  • Posts: 309
    • Andrii Vintsevych
Re: Playmaker global variables to C# script
« Reply #10 on: November 21, 2020, 05:48:20 AM »
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.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Playmaker global variables to C# script
« Reply #11 on: November 21, 2020, 06:07:04 AM »
Hi.
I believe you can finds samples on the Playmaker API