playMaker

Author Topic: Global Variables disappearing in combination with C#  (Read 2222 times)

janys

  • Playmaker Newbie
  • *
  • Posts: 3
Global Variables disappearing in combination with C#
« on: March 12, 2015, 08:53:19 AM »
Hi guys!

I'm currently working on a tiny game just to get a hang of Unity/PlayMaker/C#. However, I found an interesting issue while trying to load Playmaker's Global Variables into C# code.

Here's what I have:

I load a bunch of strings from XML using DataMakerProxy. I'm using the demo script for searching books (from the bundled example). In the end, the FSM stores several strings from the XML into Global Variables. I.e. Global\EnemiesPerLvl (string). So far so good, it all works well.

Then, I add a new gameobject with attached C# script. The script is supposed to load the global variable EnemiesPerLvl and put it as a text to UI object (using Unity 5 and the new UI).

Here's the (very untidy) code:

Code: [Select]
using UnityEngine;
using System.Collections;
using HutongGames.PlayMaker;
using UnityEngine.UI;



public class GlobalVariables : MonoBehaviour {

public static string EnemiesPerLvl_Global = FsmVariables.GlobalVariables.GetFsmString ("EnemiesPerLvl").Value;




Text _enemiesperlvl;
GameObject RemainingEnemiesText;

// Use this for initialization
void Start () {

int EnemiesPerLvl_Global_Int = 0;

int.TryParse (EnemiesPerLvl_Global, out EnemiesPerLvl_Global_Int);


RemainingEnemiesText = GameObject.Find("RemainingEnemiesText");
_enemiesperlvl = RemainingEnemiesText.GetComponent<Text> ();
_enemiesperlvl.text = EnemiesPerLvl_Global;
Debug.Log ("test");
Debug.Log (EnemiesPerLvl_Global_Int);


}

// Update is called once per frame
void Update () {



The problem is that once this code appears anywhere in Unity and I run the game, all the Global Variables are suddenly gone. The FSM pulling strings out of the XML suddenly has "none" set as the variable and the list of Global Values is empty. Once I stop the game, Global Variables are suddenly back with the correct numbers they were supposed to get from the XML.

I tried two different lines of code:

Code: [Select]
public static string EnemiesPerLvl_Global = FsmVariables.GlobalVariables.GetFsmString ("EnemiesPerLvl").Value;
and

Code: [Select]
public static string EnemiesPerLvl_Global = PlayMakerGlobals.Instance.Variables.GetFsmString ("EnemiesPerLvl").Value;
But both have the same effect - their presence causes the globalvars to disappear.

I tried googling, but I only found some info about an old version of Playmaker/Unity and it wasn't 100% the same case.

Any help would be greatly appreciated!

janys

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Global Variables disappearing in combination with C#
« Reply #1 on: March 12, 2015, 09:52:17 AM »
Update - it seems something's wrong with the file where PlayMaker stores Global Variables. When I deleted a few globalvars from the list and launched the game, the deleted vars were back again... doesn't seem to be connected with C# after all

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4003
  • Official Playmaker Support
    • LinkedIn
Re: Global Variables disappearing in combination with C#
« Reply #2 on: March 12, 2015, 09:59:19 AM »
Try moving the first time you access global variables to Start. I suspect accessing it in a static initializer is creating a new Globals instance before unity has had a chance to load the one from resources...that new instance prevents the loading of the real one. Just a theory - not at my computer to test...