Unable to set Variables via LUA or C#

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
ChargerIIC
Posts: 5
Joined: Mon Jul 01, 2019 12:48 pm

Unable to set Variables via LUA or C#

Post by ChargerIIC »

So I auto-generate a lot of stuff when a new player game is started (names, locations, etc.). I've tried to assign these variables to the design system database as they'll be referenced in a lot of places and I dont' want to pay the performance cost of the reflection methods every time i need to pull up one of these strings if I can avoid it.

I've created a series of methods like the following:

Code: Select all

        
        public string PlayerName()
        {
            return PlayerData.PlayerName;
        }

        public string PlayerTitle()
        {
            return PlayerData.CurrentTitle;
        }
and registered as noted:

Code: Select all

        void OnEnable()
        {
            Lua.RegisterFunction("PlayerName", this, SymbolExtensions.GetMethodInfo(() => this.PlayerName()));
            Lua.RegisterFunction("PlayerTitle", this, SymbolExtensions.GetMethodInfo(() => this.PlayerTitle()));
        }

        void OnDisable()
        {
            Lua.UnregisterFunction("PlayerName");
            Lua.UnregisterFunction("PlayerTitle");
        }
I've then tried to update the variable both via C#:

Code: Select all

  DialogManager.masterDatabase.variables.FirstOrDefault(v => v.Name == "PlayerName").InitialValue = sheet.PlayerName;
  DialogManager.masterDatabase.variables.FirstOrDefault(v => v.Name == "PlayerTitle").InitialValue = sheet.CurrentTitle;
and via the Lua script on the first conversation's start node:

Code: Select all

Variable["PlayerTitle"] = PlayerTitle();
Referencing the Get methods directly (lua[PlayerTitle()]) works so I know the methods are functional, but no matter what I do, any attempt to reference the variable shows the old (default) value.
User avatar
Tony Li
Posts: 22037
Joined: Thu Jul 18, 2013 1:27 pm

Re: Unable to set Variables via LUA or C#

Post by Tony Li »

Hi,

Treat the dialogue database as read-only at runtime. Its values will not change at runtime.

The runtime values of variables are in Lua. To get and set them at runtime, use DialogueLua.GetVariable() and SetVariable(). Example:

Code: Select all

DialogueLua.SetVariable("PlayerTitle", PlayerTitle());

Debug.Log("Player's title is: " + DialogueLua.GetVariable("PlayerTitle").asString);
ChargerIIC
Posts: 5
Joined: Mon Jul 01, 2019 12:48 pm

Re: Unable to set Variables via LUA or C#

Post by ChargerIIC »

Thanks. That helps a lot
User avatar
Tony Li
Posts: 22037
Joined: Thu Jul 18, 2013 1:27 pm

Re: Unable to set Variables via LUA or C#

Post by Tony Li »

Glad to help!
Post Reply