On Variable Changed event

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Deadcow
Posts: 28
Joined: Tue Apr 30, 2019 8:21 am

On Variable Changed event

Post 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?
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: On Variable Changed event

Post by Tony Li »

Hi,

Yes, you can add a Lua Observer.
Deadcow
Posts: 28
Joined: Tue Apr 30, 2019 8:21 am

Re: On Variable Changed event

Post 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?
Deadcow
Posts: 28
Joined: Tue Apr 30, 2019 8:21 am

Re: On Variable Changed event

Post 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())));
}
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: On Variable Changed event

Post 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.)
Post Reply