playMaker

Author Topic: How to set a Global Variable from C# script? [SOLVED]  (Read 12235 times)

kiko

  • Playmaker Newbie
  • *
  • Posts: 17
How to set a Global Variable from C# script? [SOLVED]
« on: February 10, 2014, 02:27:08 PM »
Pretty basic stuff, just looking for how to send a variable FROM a C# script TO a Playmaker Global Variable.

Sending a float value, if that's important.

Thank you!


UPDATE:
tried this with no luck:

FsmFloat myGlobalFloatVariable = LocalFloatVariable;
« Last Edit: February 10, 2014, 04:30:45 PM by kiko »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4002
  • Official Playmaker Support
    • LinkedIn
Re: How to set a Global Variable from C# script?
« Reply #1 on: February 10, 2014, 04:15:47 PM »
You can access GlobalVariables using a static property of FsmVariables:

Code: [Select]
FsmFloat globalVar = FsmVariables.GlobalVariables.FindFsmFloat("myFloat");
globalVar.Value = 100;

EDIT: Updated the wiki page here:
https://hutonggames.fogbugz.com/default.asp?W1103
« Last Edit: February 10, 2014, 04:29:16 PM by Alex Chouls »

kiko

  • Playmaker Newbie
  • *
  • Posts: 17
Re: How to set a Global Variable from C# script?
« Reply #2 on: February 10, 2014, 04:29:38 PM »
YES! Thank you Alex. Worked like a charm   :D

FXCarl

  • Playmaker Newbie
  • *
  • Posts: 4
Re: How to set a Global Variable from C# script? [SOLVED]
« Reply #3 on: February 11, 2014, 03:34:31 AM »
is there any way to add new globe variables dynamically?

I tried this in a update loop ... it not works

Code: [Select]
if(FsmVariables.GlobalVariables.FindFsmString("sth") == null){
Debug.Log("NotFound");
FsmVariables.GlobalVariables.GetFsmString("sth");
}
« Last Edit: February 11, 2014, 03:36:13 AM by FXCarl »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to set a Global Variable from C# script? [SOLVED]
« Reply #4 on: February 11, 2014, 01:10:09 PM »
Hi,

 PlayMaker is not editable at runtime, so adding a global variable at run time doesn't make sense cause nothing would reference it in your fsm logics because it did not existing when you edited it.

does that make sense?

 Can you explain why you want to create global variables at runtime?

 Maybe you need to use playerprefs or ArrayMaker actually?

bye,

 Jean

FXCarl

  • Playmaker Newbie
  • *
  • Posts: 4
Re: How to set a Global Variable from C# script? [SOLVED]
« Reply #5 on: February 11, 2014, 10:40:03 PM »
Hi,

 PlayMaker is not editable at runtime, so adding a global variable at run time doesn't make sense cause nothing would reference it in your fsm logics because it did not existing when you edited it.

does that make sense?

 Can you explain why you want to create global variables at runtime?

 Maybe you need to use playerprefs or ArrayMaker actually?

bye,

 Jean

our ui guy dont know C#, so our ui system are driven by playmaker

but,

1. Globe Variable editor isn't very handy for tens of vars, reference a var by a string maybe better —— select var form a popup list longer than screen is not a easy job ~ even we are using trick "group\item" to group them
2. for "binding", C# code can setup a var and don't need to open the globe variables editor.

I hope to work this way,

An NGUI label shows player xp.
In playmaker, use a custom action "set Property by globe var" and set the label text by var "player\xp"
In C# code, set it FsmVariables.GlobalVariables.GetFsmString("player\xp").Value = (100).ToString();
Playmaker user and programer don't need to know other's working detail.

maybe there are some better ideas ?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to set a Global Variable from C# script? [SOLVED]
« Reply #6 on: February 11, 2014, 11:41:13 PM »
Hi,

 Ok, but that still doesn't require having to create globals at runtime.

What you want to achieve is perfectly possible right now. I do this all the time with many addons.

Instead, would it not be better that you talk directly to the Fsm that is on your player gameobject? so instead of having a global variable "Player/xp" you only have a variable "Xp" on a fsm attached to your player gameobject. then from script you can still get to that variable.

Also, have you checked the ngui addons for PlayMaker, and the actions"get property" "set property" action. I suspect you can do all this without scripting to begin with.

https://hutonggames.fogbugz.com/default.asp?W1111


 Bye,

 Jean