Page 1 of 1

Unable to set Variables via LUA or C#

Posted: Tue May 11, 2021 4:32 pm
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.

Re: Unable to set Variables via LUA or C#

Posted: Tue May 11, 2021 4:49 pm
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);

Re: Unable to set Variables via LUA or C#

Posted: Tue May 11, 2021 5:11 pm
by ChargerIIC
Thanks. That helps a lot

Re: Unable to set Variables via LUA or C#

Posted: Tue May 11, 2021 5:17 pm
by Tony Li
Glad to help!