AddLuaObserver

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Sengi
Posts: 5
Joined: Wed Apr 12, 2017 8:37 am

AddLuaObserver

Post by Sengi »

Hi guys, I need some help to observe when any location field had changed. I know I can observe when particular field had changed like so:
Image

Code: Select all

  void Start() {
        _dialogController = gameObject.GetComponent<DialogueSystemController>();
        DialogueManager.AddLuaObserver("Location[\"Person\"].Lighting Technician", LuaWatchFrequency.EveryUpdate, OnFieldChanged);
    }

    void OnFieldChanged(LuaWatchItem luaWatchItem, Lua.Result newValue){
        Debug.Log("Number of credits changed to: " + newValue.AsString);
    }
But how can I observe with a single method when any field had changed, something like this:

Code: Select all

 DialogueManager.AddLuaObserver("Location[\"Person\"].fields", LuaWatchFrequency.EveryUpdate, OnFieldChanged);
thank you!
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: AddLuaObserver

Post by Tony Li »

Hi,

I'm out of the office right now, but when I get back I can post a Lua function that should be able to take care of that.

However, running Lua code every Update may be a little excessive. Checking every Update is called polling. Often, event-driven checking can be more efficient since it only checks when you actually change a field. Do you have control over how Location fields get changed? For example, do you change it in your code or conversations, like:

Code: Select all

Lua.Run("Location['Person'].MC = true");
Or a similar line in a dialogue entry's Script field?

If so, you could add a C# method named SetLocationField(), like this:

Code: Select all

public void SetLocationField(string locationName, string fieldName, object value) {
    DialogueLua.SetLocationField(locationName, fieldName, value);
    OnLocationFieldChanged(locationName, fieldName);
}

void OnLocationFieldChanged(string locationName, string fieldName) {
    Debug.Log("Location " + locationName + " field " + fieldName + " changed!");
}
Then instead of:

Code: Select all

Lua.Run("Location['Person'].MC = true");
You could use this:

Code: Select all

SetLocationField("Person", "MC", true);
Would that work better for you, or would you like a Lua function to help check every Update?
Sengi
Posts: 5
Joined: Wed Apr 12, 2017 8:37 am

Re: AddLuaObserver

Post by Sengi »

Thank you Tony Li , one of the best points in Dilogue System - is quick support. The thing is with Location field - is that it can change inside the dialogue system, through the action in conversations. For example Location, or Person can be unlocked when some dialog choices been made. I need to know this situation through the script, so I can adjust and tweek another part of the code.

I already resolved this by using Event field (when I change some location statement - I`m calling Event to launch the update in code), but it requires to take additional care, like edding events to every place where designers need to update all stuff. I thought there is just some kind of observer, that calls a method when some Location/Item field had changed, so I could process this changes in the game.

Thanks again for the quick response!
Post Reply