Articy Forum


https://www.articy.com:443/forum/

Global variables changed event

https://www.articy.com:443/forum/viewtopic.php?f=46&t=18634

Page 1 of 1

Global variables changed event

Posted: Sun 18. Aug 2019, 08:48
by thepunkoff
Hi again! Another question from me =) I guess there isn't a built in way to check if a variable (or any of the varables) was changed? I'm sorry, but I can't find anything like this. If there's not, what is the best way to achieve this? Or could you tell exactly where and when it happens?

UPD. I guess a variable changes If there's some instructions in the pin

Re: Global variables changed event

Posted: Wed 21. Aug 2019, 08:48
by [Articy] Nico Probst
Hi thepunkoff,

There actually is a way to do exactly that!
Global Variables are stored inside an instance of BaseGlobalVariables. You can have multiple of those
to keep track of different sets of your variables, but most of the time you only need one. This one is automatically created for you and accessible via

Code: Select all
ArticyDatabase.DefaultGlobalVariables


or if you want C# access to your global variables; via the generated class(Both ways point to the exact same instance, just with different types)

Code: Select all
ArticyGlobalVariables.Default


Either way you can access now the Notification Manager. Here you can register a method to be called everytime a global variables is modified.

Code: Select all
public class MyNotificationScript : MonoBehaviour
{
    void Start()
    {
        // we get the default variable storage from our database and add a listener to the notification manager
        // to call our supplied method every time any Variable inside the namespace "GameState" is changed.
        ArticyDatabase.DefaultGlobalVariables.Notifications.AddListener("GameState.*", MyGameStateVariablesChanged);
    }
    // here we handle every variable change inside the "GameState" namespace
    void MyGameStateVariablesChanged(string aVariableName, object aValue)
    {
        // for example:
        // When our exit variable was set to true, we quit the game.
        if(aVariableName == "GameState.exit" && ((bool)aValue) == true)
             Application.Quit();

        // in this example we play a beep when the variable changes
        // its worth noting that we won't hear a beep now every frame once the variable has been set to true. Because our listener method will only
        // be called once, when our variable changes.
        if(aVariableName == "GameState.beep" && ((bool) aValue) == true)
            EditorApplication.Beep();
    }
}


We do this for the inventory management in our Maniac Manfred Demo project(A small Point&Click Adventure), where the possession of an item is marked via global variable, everytime it changed the item gets added or removed from the players inventory. This turned out great for debugging, as even changing the GV via the inspector view, triggeres those events.

Hope that helped!

Best regards

Nico

All times are UTC
Page 1 of 1