Page 1 of 1
On Variable Changed event
Posted: Fri Sep 18, 2020 8:02 am
by Deadcow
Is there a way to be notified when some variable value is changed during runtime?
For instance, in dialogue entry script field I got this "Variable["CogsInMainClock"] = Variable["CogsInMainClock"] + 1"
This script is increases int value of CogsInMainClock variable, and I'd like to log this event for debug purposes. Any way to do this?
Re: On Variable Changed event
Posted: Fri Sep 18, 2020 9:53 am
by Tony Li
Hi,
Yes, you can add a
Lua Observer.
Re: On Variable Changed event
Posted: Mon Sep 21, 2020 4:21 am
by Deadcow
Thanks! I'm able to connect to specific field changes with this, and log the results, that's cool.
Is there a way to observe all variables? I tried "Variable[]" and ""Variable" as an expression and it not helped.
I may try to get the list of all variables and create observers for all of them at runtime, but maybe there is a better way?
Re: On Variable Changed event
Posted: Mon Sep 21, 2020 4:34 am
by Deadcow
well no bother, this code do just what I needed, thanks!
Code: Select all
foreach (var variable in DialogueManager.databaseManager.DefaultDatabase.variables)
{
if (variable.Type != FieldType.Number && variable.Type != FieldType.Boolean) continue;
DialogueManager.AddLuaObserver("Variable[\""+variable.Name+"\"]",
LuaWatchFrequency.EveryDialogueEntry,
(item, value) => Debug.Log("Dialogue Variable — " + variable.Name + " changed to: " + (value.isNumber ? value.asInt.ToString() : value.asBool.ToString())));
}
Re: On Variable Changed event
Posted: Mon Sep 21, 2020 9:23 am
by Tony Li
Sounds good. Specifying LuaWatchFrequency.EveryDialogueEntry or LuaWatchFrequency.EndOfConversation should be fine. But, since this is a polling technique, make sure not to use LuaWatchFrequency.EveryFrame. (It would be like writing C# code to scan every C# variable in every C# script every frame for changes, which would be far from efficient.)