Create variables from script

Announcements, support questions, and discussion for the Dialogue System.
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create variables from script

Post by Tony Li »

Glad to help!
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Re: Create variables from script

Post by joeylu »

Hi Tony, sorry I have to re-open this thread again, as I'm facing another variable issue with the database.
So continue from the previous discussion, as I'm using DialogueLua.SetVariable() to add/update variables in readonly master database, it's been working.
Until today that I'm adding a script in a dialogue entry, for example:

Code: Select all

Variable["test_variable"] += 1;
Notice that "test_variable" has never added to the database from dialogue editor, it is created and added in runtime from SetVariable(), I'm actually can get the "true" result by invoking DoesVariableExist("test_variable") after adding.

However, once that dialogue entry node is played, I'm getting a Null exception error, the only thing that I can guess is because this "test_variable" has never added from editor? does that mean SetVariable() adding new variables won't work with the entry node's script/condition setting?

tks

FYI, here's the error I'm getting
PixelCrushers.DialogueSystem.Lua:RunRaw (string,bool,bool) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:228)
PixelCrushers.DialogueSystem.Lua:Run (string,bool,bool) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:129)
PixelCrushers.DialogueSystem.ConversationModel:GetState (PixelCrushers.DialogueSystem.DialogueEntry,bool,bool,bool) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Model/ConversationModel.cs:234)
PixelCrushers.DialogueSystem.ConversationModel:GetState (PixelCrushers.DialogueSystem.DialogueEntry) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Model/ConversationModel.cs:304)
PixelCrushers.DialogueSystem.ConversationController:OnSelectedResponse (object,PixelCrushers.DialogueSystem.SelectedResponseEventArgs) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Controller/ConversationController.cs:304)
PixelCrushers.DialogueSystem.ConversationView:SelectResponse (PixelCrushers.DialogueSystem.SelectedResponseEventArgs) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/View/View/ConversationView.cs:504)
PixelCrushers.DialogueSystem.ConversationView:OnSelectedResponse (object,PixelCrushers.DialogueSystem.SelectedResponseEventArgs) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/View/View/ConversationView.cs:497)
PixelCrushers.DialogueSystem.AbstractDialogueUI:OnClick (object) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/UI/Abstract/Dialogue/AbstractDialogueUI.cs:346)
PixelCrushers.DialogueSystem.StandardDialogueUI:OnClick (object) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/UI/Standard/Dialogue/StandardDialogueUI.cs:352)
PixelCrushers.DialogueSystem.StandardUIResponseButton:OnClick () (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/UI/Standard/Dialogue/StandardUIResponseButton.cs:132)
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create variables from script

Post by Tony Li »

That's a misleading error. The issue is that Lua doesn't have a "+=" operator. Use this instead:

Code: Select all

Variable["test_variable"] = Variable["test_variable"] + 1
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Re: Create variables from script

Post by joeylu »

ahh, how stupid I am.. now it's working perfectly again !
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Re: Create variables from script

Post by joeylu »

I'm also getting to a point to save variables by using PersistentDataManager
here's my steps
Using DialogueLua.SetVariable() to add variables that has generated from runtime
then PlayerPrefs.SetString(slotname, PersistentDataManager.GetSaveData()); to get all variables and save it to the playerPrefs

Somehow the result doesn't include variables that I have added from SetVariable(), only variables from the database. I must missed some steps, or PersistentDataManager.GetSaveData() is not something I should look for? tks
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create variables from script

Post by Tony Li »

Hi,

PersistentDataManager.GetSaveData() will also save variables that you've created at runtime, such as through DialogueLua.SetVariable().

Examine the string returned by PersistentDataManager.GetSaveData(). Does it contain the variables? If so, then maybe the issue occurs when you're re-applying the save data. For example, maybe when you're loading a game you apply the save data (which restores the runtime variables) but then afterward call something like DialogueManager.ResetDatabase() which would reset the variables to their initial database state.

If the string doesn't contain the variables, double check that they exist at the time you call GetSaveData(). You can use the Dialogue Editor window's Watches tab > Menu > Add All Runtime Variables to see a list of them. Or you can add a Lua Console to your scene, press ~+L to open it during play, and enter "return Variable" to see the list.
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Re: Create variables from script

Post by joeylu »

tks, indeed there's ResetDatabase was called and removing it solves the issue
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create variables from script

Post by Tony Li »

Glad you got to the bottom of it!
Post Reply