Forum rules - please read before posting.

Global var check via scripts

Hey there :)

In my game, i have serveral global vars (Bools) for checking which points on a map are accessible. Now i would like to do these checks via a script that's attached to the Unity UI.

What would i have to use to let the script check the Vars and how do i use them in an "if" and "else" situation?

Comments

  • This tutorial covers the manipulation of variables through script.
  • edited November 2016
    I have already studied that page, and it made a lot of things clearer, but i am a little puzzled how i would use "AC.GlobalVariables.GetBooleanValue (int _id);" inside of an if function.

    Basically i want to do this if check, and if it is true, i want a hotspot on a map to activate. If it returns false, the hotspot stays turned off.
  • Well, you can use that function like a regular value, the conditional will just check for the returned value, like:

    if (AC.GlobalVariables.GetBooleanValue(0)) // 0 is an example, get var id from the variables manager
    {
    //if true do this...
    }

    But, I can't recall how to turn on or off hotspots through scripting. I mean, you could enable or disable the component or object, but you can probably find the code on the API though, so you should probably take a look in there first.
  • Ahaa, this helped me a lot! :D
    I can then just do "if (AC.GlobalVariables.GetBooleanValue(0) == false)" and it works perfectly! Thanks a lot!

    There is one more thing i noticed, and it's that Unity always spits out a debug message, saying "Variable with ID=0 not found!", yet the script which references that variable works fine and gets/sets it just fine
  • edited November 2016
    There's no need to type:

    if (AC.GlobalVariables.GetBooleanValue(0) == false)

    you can shorten it as:

    if (!AC.GlobalVariables.GetBooleanValue(0)) // '!' is read as "not", here as in "if not true"

    As far as I've noticed you may get the "Variable with ID=0 not found!" error, if either the variable doesn't exist in the variables manager, OR if it's the wrong type... ie: if variable 0 were a float instead of a boolean. But to be honest I'm not sure if there are other situations which can spring that error.
  • I have checked everything, and the variable definitely exists and it's the right type..
    Do i need to initialize the variable anywhere the script?

    I tried putting "bool WeMetGoal = AC.GlobalVariables.GetBooleanValue (0);" right after the "public class", but that gives me an error:
    "Load is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead."

    However when i do call it in Start, i cannot use "WeMetGoal" in OnEnabled, which i need to let the UI check if a variable changed since the last time i opened the UI..
  • edited November 2016
    Ok, so I get the reason you were having this error message was because, as I mentioned in your other post, the code is probably being called before AC has finished initializing itself. 

    About how to re-initialize in a case were an object is disabled and re-enabled just check your other post. I put one method to do it in there for you.
  • See my reply to this post.  You can use custom events to run code when a Menu is turned on, among other things - you can therefore run this code to amend the Menu to suit.
  • edited November 2016
    Huh, i just cannot figure out why it doesn't like my code.. I initalize the GlobalVar first, and the script itself is ONLY executed when you press the Unity UI button..
    Here's the script, perhaps there really is something wrong with it.. http://paste.ofcode.org/qnDZ9bm6QzP82BinSNgTXn

    I still constantly get the error
    "Load is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'ShowDossierKeys' on game object 'BTN_LB1_UI_NB_Dossier_PortraitGrid_5'.
    See "Script Serialization" page in the Unity Manual for further details."
  • edited November 2016
    There are two vars which have the text Dossier_PortraitGrid_5: GridName and KeyGridsButton. You didn't specify which (but it's most likely the button I guess)... But anyway, try initializing the variable at the class level with the new keyword (to create a new holder/object of that type) and see if that makes a difference.

    Ummm, then, besides that, as far as I know you cannot use functions for variable initialization at the class level. If you are not getting an error directly for WeMetKeys you may get it later. 

    "AC.GlobalVariables.GetBooleanValue (1)" is a function (AC is the name space, GlobalVariables is a script name, and GetBooleanValue (1) is a function inside that script which takes one parameter).

    You'll need to initialize WeMetKeys in an Awake() or OnEnable() or Start() functions. Just remember that OnEnable can happen multiple times whereas Awake and Start only happen once during the life of a script. I guess you could also create a property that initializes itself inside the getter. There's a chance the error might be related to the WeMetKeys initialization too, so try fixing it too, just to be on the safe side.
  • But i cannot initialize it in Start, since then i cannot use it in the "ShowKeysDossier" void later on... And isn't "bool WeMetKeys = AC.GlobalVariables.GetBooleanValue (1);" initializing the var? If not, i cannot just tell the script to set it to "False" everytime i restart the game, since it will then set it to false even though it was set to true before the restart, right?
  • Move the whole of line 30 (bool weMetKeys) down to line 33 (above if (weMetKeys)).

    You need to initialise the var in a function - as the error is telling you - but you also need to initialise it at the time you need to check it, since the AC Global Variable will have changed since the game starts and whenever you run that function.
  • Oh my gosh, that fixed it!! Clearly i still have a lot to learn, hehe, but thanks so much for your help!!
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.