Create variables from script
Create variables from script
Hi Tony,
Our game has a database and its own MVC that generates conditions by variables, currently we have to copy and paste all those variables (all boolean type) to the dialogue editor variable field, my question is that if there's an ideal way to shorten the process, for instance, from a script.
In below description: (db is a dialogue database object)
Purpose #1.
In play mode, we can use DialogueLua.SetVariable to add variables, but those variables won't be shown in dialogue editor nor be listed in db.variables, I assume it was becasue the variables from dialogue system were cached from memory?
So how do we find those variables that we added in runtime?
Purpose #2
In editor mode, DialogueLua.SetVariable won't be functioning, what is the best way to add variables in Editor mode from script, and make sure to be shown in dialogue editor? I tried db.variable.Add(), it returns a object null error
Purpose #3
If above two request can be resolved, we might need to clear the entire variable list from dialogue system sometimes, is there a way to do that? or is there a way to deleve/remove an existing variable from the db?
btw, dialouge system is really a great asset for handling conversations in our narrative game!! well done!!
Our game has a database and its own MVC that generates conditions by variables, currently we have to copy and paste all those variables (all boolean type) to the dialogue editor variable field, my question is that if there's an ideal way to shorten the process, for instance, from a script.
In below description: (db is a dialogue database object)
Purpose #1.
In play mode, we can use DialogueLua.SetVariable to add variables, but those variables won't be shown in dialogue editor nor be listed in db.variables, I assume it was becasue the variables from dialogue system were cached from memory?
So how do we find those variables that we added in runtime?
Purpose #2
In editor mode, DialogueLua.SetVariable won't be functioning, what is the best way to add variables in Editor mode from script, and make sure to be shown in dialogue editor? I tried db.variable.Add(), it returns a object null error
Purpose #3
If above two request can be resolved, we might need to clear the entire variable list from dialogue system sometimes, is there a way to do that? or is there a way to deleve/remove an existing variable from the db?
btw, dialouge system is really a great asset for handling conversations in our narrative game!! well done!!
Re: Create variables from script
Hi,
At runtime, the Dialogue System populates its Lua environment with the variables defined in your dialogue database. DialogueLua.SetVariable() also updates/adds variables in the Lua environment, not the dialogue database. (At runtime, treat the dialogue database itself as read-only.) Let me know if you need to get a reference to the entire runtime Lua table (array) of variables, but it sounds like you want to add them at edit time instead, so I'll cover that below.joeylu wrote: ↑Wed Mar 09, 2022 4:16 amPurpose #1.
In play mode, we can use DialogueLua.SetVariable to add variables, but those variables won't be shown in dialogue editor nor be listed in db.variables, I assume it was becasue the variables from dialogue system were cached from memory?
So how do we find those variables that we added in runtime?
Something like that should work. It's best to use the Template class. Here's an excerpt of some example editor code. It assumes you already have a reference 'db' to your dialogue database. If not, you can use EditorTools.FindInitialDatabase(), which will look for a Dialogue Manager in the open scene and return its Initial Database.
Code: Select all
using PixelCrushers.DialogueSystem;
...
Undo.RecordObject(db, "Add Variables"); // Tell Unity editor to make this operation undo-able.
Template template = TemplateTools.LoadFromEditorPrefs();
string[] variablesToCreate = new string[] { "var1", "var2", "var3" };
foreach (string variableName in variablesToCreate)
{
Variable variable = template.CreateVariable(template.GetNextVariableID(db), variableName, "false", FieldType.Boolean);
db.variables.Add(variable);
}
EditorUtility.SetDirty(db);
At edit time, you can remove variables from the dialogue database like this:
Code: Select all
Undo.RecordObject(db, "Remove Variable");
string variableToRemove = "var2";
Variable variable = db.variables.Find(x => x.Name == variableToRemove);
db.variables.Remove(variable);
EditorUtility.SetDirty(db);
Thanks!
Re: Create variables from script
tks Tony, works like charm as always
a follow up question, my understanding is that despite how many databases are created in unity, in playmode, all database will be merged into one master database, and I can get it by calling DialogueManager.masterDatabase right?
if that is true, say I have a variable "AAA" in two dialogue database, in playmode, the masterDatabase.variables will only have on "AAA" in its list right?
If that is true, if I modify it through masterDatabase.variable[0].initialBool(true), assume [0] is "AAA"'s current index, does that reflects to both "AAA" variables in two different dialogue database? same to removing or adding, or maybe my logic was wrong at first place?
a follow up question, my understanding is that despite how many databases are created in unity, in playmode, all database will be merged into one master database, and I can get it by calling DialogueManager.masterDatabase right?
if that is true, say I have a variable "AAA" in two dialogue database, in playmode, the masterDatabase.variables will only have on "AAA" in its list right?
If that is true, if I modify it through masterDatabase.variable[0].initialBool(true), assume [0] is "AAA"'s current index, does that reflects to both "AAA" variables in two different dialogue database? same to removing or adding, or maybe my logic was wrong at first place?
Re: Create variables from script
In general, at runtime treat dialogue databases (even DialogueManager.masterDatabase) as read-only. To set a runtime value, use DialogueLua.SetVariable().
When the Dialogue System loads a database into memory at runtime, it creates corresponding variables in its Lua environment. Think of the variable definition in the database not as the runtime representation of the variable's value but as a template that describes how the Lua variable should be created . The Lua variable is what's used at runtime by dialogue entry Conditions and Script fields, DialogueLua.SetVariable(), etc.
When the Dialogue System loads a database into memory at runtime, it creates corresponding variables in its Lua environment. Think of the variable definition in the database not as the runtime representation of the variable's value but as a template that describes how the Lua variable should be created . The Lua variable is what's used at runtime by dialogue entry Conditions and Script fields, DialogueLua.SetVariable(), etc.
Re: Create variables from script
hi Tony, last question regards this post
TemplateTools adds variable perfectly
However, when using DialogueLua.SetVariable(), I'm able to add variables to the database and able to retrieve it with GetVariable() in runtime. The problem is that I can't get them to saved to the dialogue editor (variable tab), appearantly using DialogueLua Get/Set only saves the variables in memory?
What is the best way to save them into the editor? using your save plugin? or any sync tools? tks
TemplateTools adds variable perfectly
However, when using DialogueLua.SetVariable(), I'm able to add variables to the database and able to retrieve it with GetVariable() in runtime. The problem is that I can't get them to saved to the dialogue editor (variable tab), appearantly using DialogueLua Get/Set only saves the variables in memory?
What is the best way to save them into the editor? using your save plugin? or any sync tools? tks
Re: Create variables from script
Hi,
At runtime, the Dialogue System creates a Lua environment and creates a table of Lua variables from the variable definitions in your dialogue database. From that point on at runtime, the dialogue database is treated as read-only. DialogueLua.GetVariable() will get the current value of a Lua variable, not the initial value of the dialogue database's variable definition. DialogueLua.SetVariable() will add/change the values of Lua variables; it won't modify the dialogue database.
At runtime, you can see the values of all variables, including new variables added at runtime, in the Dialogue Editor's Watches tab and in the separate Variable Viewer window.
If you want to add a variable definition to your dialogue database, not to the runtime Lua environment, use an editor script outside of runtime. (See my previous post for example code.)
At runtime, the Dialogue System creates a Lua environment and creates a table of Lua variables from the variable definitions in your dialogue database. From that point on at runtime, the dialogue database is treated as read-only. DialogueLua.GetVariable() will get the current value of a Lua variable, not the initial value of the dialogue database's variable definition. DialogueLua.SetVariable() will add/change the values of Lua variables; it won't modify the dialogue database.
At runtime, you can see the values of all variables, including new variables added at runtime, in the Dialogue Editor's Watches tab and in the separate Variable Viewer window.
If you want to add a variable definition to your dialogue database, not to the runtime Lua environment, use an editor script outside of runtime. (See my previous post for example code.)
Re: Create variables from script
understood, tks tony
Re: Create variables from script
Hi Tony, for instance, I'm loopiing the master database in runtime and getting all the dialogue conversation and dialogue entries. I need to set some dialogue entry's userScript and conditions string. I have no problem to set it from looping actual database, but not in masterDatabase, because it is readonly.
So my question is that while getting DialoguEntry from readOnly masterDatabase, how can I set its conditions and userScripts in runtime? tks
So my question is that while getting DialoguEntry from readOnly masterDatabase, how can I set its conditions and userScripts in runtime? tks
Re: Create variables from script
If you really need to do it at runtime, make sure the Dialogue Manager's Other Settings > Instantiate Database checkbox is ticked. The masterDatabase will load an instantiated copy of the Initial Database that you can change at runtime.
Keep in mind that the Dialogue System will read certain values from the Lua environment and not the database, even if it's instantiated. Those values are: Actor field, Item & Quest fields, Location fields, and Variables.
Keep in mind that the Dialogue System will read certain values from the Lua environment and not the database, even if it's instantiated. Those values are: Actor field, Item & Quest fields, Location fields, and Variables.
Re: Create variables from script
just courious that if this is possible in runtime, tks for your ideas, again, very helpful