Forum rules - please read before posting.

Controlling Global/Local Variables via Script

edited August 2020 in Technical Q&A

In my game I have several mechanics and GameObjects throughout the entire game that I only want visible/active at certain points. I also want certain scripts to check if those things are active at points to decide whether or not it should do things. Rather than script it normally, I figure it might be more efficient to use AC's Global Variable System. That way, if I want to have a bunch of things that trigger at once or check if something's running, I can just hook them up to the same variable script-wise as an efficient way of doing things.

For example, I have a minigame normally looks for a bool in its script to decide if itself and subsequent functions active. I also have other scripts that need to check if that same bool on the other script is on or not since I want the minigame to freeze certain mechanics. I figure it's more efficient to futureproof this process by hooking it up to a Global Variable instead and control that via the script (button press for testing) and/or through ActionLists to easily control it.

However, I've never used AC Variables before and despite reading the documentation and an answer in my previous thread on a semi-separate issue, I'm having constant issues hooking up and controlling Global or Local AC Variables with Scripts.

As a test, I have a basic script that hooks up to a Boolean Script I made in the AC Menu called Minigame. It's the first Global Variable so it's ID is 0. I've set it to false. I've tried Link To None and Custom Script. While I've been fiddling with it a lot over the past few days with no success, here's a very basic form of it where I try to toggle the Global Variable on button press.

using AC;

    GVar MGActive2;

public void Awake()  {
        MGActive = AC.GlobalVariable.GetVariable(0);
 }

    void Update () {
        if (Input.GetKeyDown(KeyCode.U))
        {
                MGActive = true;
        }
    }

I've tried a number of other ways, but I'm just not getting anywhere. Unity also treats AC.GlobalVariable as a problem, saying the GlobalVariable namespace does not exist in AC. I'm very unfamiliar with manually scripting Adventure Creator stuff and I keep running into roadblocks, so I may need some specific syntax and explanations help to solve this.

Comments

  • There is no "GlobalVariable" class in AC - it needs an 's' on the end. Was there a typo in the docs somewhere?

    See the Manual's "Variable scripting" chapter for a guide to reading and writing to variables. The main issue with your code above is that you're trying to modify the reference to the variable - not the value that the variable stores. For a Bool variable, this is done by accessing the BooleanValue property:

    GVar MGActive2;
    
    public void Awake()
    {
        MGActive = AC.GlobalVariables.GetVariable(0);
    }
    
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.U))
        {
            MGActive.BooleanValue = true;
        }
    }
    

    This will work with the Link to value still set as None. Setting it to Custom Script allows you to automatically syncronise its value with a separate bool in a custom script, when both the script and AC read/set the variable. You can read more about this in the "Linking with custom scripts" chapter, but it's not necessary for the above example.

  • There we go. Now it works. (Though I just noticed the accidental 2 I put in the script and corrected that on my end). Thanks a lot.

    However, is there a reason why this doesn't work?

    public bool MGActive;
    
    public void Awake()
    {
            MGActive = AC.GlobalVariables.GetVariable(0).BooleanValue;
    }
    
    void Update ()
    {
     if (Input.GetKeyDown(KeyCode.U))
        {
            MGActive = true;
            }
    }   
    

    While your method works, because GVar Variables appear with all its parameters in the Inspector, I figure it's easier to manage and troubleshoot as a normal bool.

  • That's just how C# works. When storing the AC variable value in "MGActive", you're effectively making a copy - and modifying that copy won't update the original recursively.

  • Interestingly enough it only worked for me when not putting the calls inside the Awake() method - it only worked when put inside the Start() method.

  • If that's the case, it's likely due to both AC and the script both initialising in Awake, meaning there can be a bit of randomness in terms of the order things run in.

    Putting init code into Start, or giving the script a positive Script Execution Order value, should ensure it's run in the correct order.

  • edited February 26

    Hi Chris, I tried to adapt this code from the topic in my script to change the Boolean variable through AC, and errors occur.

    NullReferenceException: Object reference not set to an instance of an object
    MoverParaDireita.Start () (at Assets/PegaLadrao/Graficos/Sprites/Scripts/MoverParaDireita.cs:17)

    I created a variable in AC with ''link to custom script''

    https://uploaddeimagens.com.br/imagens/UClhUA0

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;

    public class MoverParaDireita : MonoBehaviour
    {
    public float velocidadeDoObjeto;

    GVar MGActive;
    
    
    // Start is called before the first frame update
    void Start()
    {
    
        MGActive.BooleanValue = true; //**the error indicates this line**
    
    }
    
    // Update is called once per frame
    void Update()
    {
    
    
    
    
        MovimentarObjeto();
    }
    
    private void MovimentarObjeto()
    
    {
        if (MGActive.BooleanValue == true)
        {
            transform.Translate(Vector3.right * velocidadeDoObjeto * Time.deltaTime);
        }
    }
    
    
    public void Awake()
    {
        MGActive = AC.GlobalVariables.GetVariable(8);
    }
    

    }

  • You don't need to set the Link to to Custom Script in this case.

    Try merging your Start and Awake functions, and making sure the script is only running in AC scenes:

    void Start()
    {
        MGActive = AC.GlobalVariables.GetVariable(8);
        MGActive.BooleanValue = true; 
    }
    
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.