Forum rules - please read before posting.

How to read an AC variable from script?

edited April 2014 in Technical Q&A
Is there a way to read a variable set as bool in AC from a java or c# script of Unity?

Comments

  • edited April 2014
    There is. I'm not 100% certain but this should do it. Put this custom function in one of your scripts:

    bool GetBoolValue (int ID)
    {
     foreach (GVar _var in GameObject.FindWithTag (Tags.persistentEngine).GetComponent <RuntimeVariables>().localVars)
     {
      if (_var.id == ID)
      {
       return _var.val;
      }
     }
     return false;
    }

    Then wherever you need it you call GetBoolValue(ID); and replace ID with the number of the Global Variable you want to use for it (the ID of each variable can be found in the Variables Manager of AC).

    Chris said he was going to make this code a little easier in the next update.
  • edited April 2014
    Thank you so much for the help. This is what i did. And i get an error on line 13 which is yellow

    public class AlarmOn : MonoBehaviour {

        bool RoomLock;
        public Transform light;
        // bool light = true;

        // Use this for initialization
        void Start () {

            bool GetBoolValue (int ID);
            {
                foreach (GVar _var in GameObject.FindWithTag (Tags.persistentEngine).GetComponent <RuntimeVariables>().localVars)
                {
                    if (_var.id == ID)
                    {
                        return _var.val;
                    }
                }
                return false;
            }
       
        }
       
        // Update is called once per frame
        void Update () {

            RoomLock = GetBoolValue (2);
           
            if (RoomLock = false) {
               
                light.enabled = !light.enabled;
            }
            else {
               
                light.enabled = light.enabled;
                light.intensity=0.5;
            }
       
        }
    }

    I get error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
  • You'll have to take the GetBoolValue method out of the Start method.  And you'll have to remove the ; at the back of bool GetBoolValue (int ID);
  • just did and i get a bunch of new errors will check it out one by one
  • AC's bools share the same value field as ints: 0=false, 1=true.  There is a function that already does this for you, but returns a generic string, so try this instead:

    if (GameObject.FindWithTag (Tags.persistentEngine).GetComponent <RuntimeVariables>().GetVarValue (2)) == "1")
    {
      // TRUE
    }
    else
    {
      // FALSE
    }


  • So i must change the method to produce and integer value 0 or 1 ?
  • You don't have to write any code beside what I wrote above.  Did you try it?

    A more extensive tutorial is here.
  • edited April 2014
    yes i did try is and i was getting en error

    Engine.GameObject.GetComponent<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly
  • Ah.  You may have to replace RuntimeVariables with AC.RuntimeVariables.
  • edited April 2014
    The tutorial shows this differently but will try it thanks! :)

    GameObject.FindWithTag (Tags.persistentEngine).GetComponent ().GetVarValue (2);
  • Looks like the tutorial's HTML is removing the angle brackets!  I will correct their display.
  • Actually @ChrisIceBox I was getting an error using AC.RuntimeVariables , but the script seems to work with just RuntimeVariables.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.