Forum rules - please read before posting.

EventManager.On*Variable and Component variables

edited June 2019 in Technical Q&A

I'm using EventManager.OnUploadVariable events to detect when my variables change, which is working for the most part.

However, with the new Component variables there doesn't seem to be a way of detecting if a variable is a Global or Component one? This problem gets aggravated because both types of variables start at 0, so there is no workaround that I can see (except maybe checking if a variable is global with var.IsGlobalVariable(), but that iterates over every global variable on every call, which looks very inefficient).

Also, the location of the variable is in GVar.Upload(), so I guess one possible solution will be passing it down to the subscribed listeners (which I'm not sure is possible in a backwards compatible way, maybe optional parameters?)

Comments

  • Besides this, it seems my script will receive all component variables that have a CustomScript link, so even if I differentiate between global and component ones, how can I check if I'm dealing with one or another component variable? (besides FindObjectsOfType<Variables> and doing a manual check, which seems like a big hassle, or something like checking its label).

    I'm probably missing something, hence my asking.

  • You should just need to store a reference to the variable itself from the Variables component, i.e.:

    myVar = myVariables.GetVariable (myID);
    

    You can then check if this is the same variable being uploaded:

    void My_OnUploadVariable (GVar gVar)
    {
        if (gVar == myVar)
        {
            //
        }
    }
    
  • Thanks Chris. This will of course solve the problem when I'm dealing with component variables from a script that's attached to the same GameObject, although I'll still have problems if I need to deal with them from a more central location, but I guess I can workaround it by storing the components the variables come from alongside their IDs.

    Thanks again.

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.