Hi Zeldarck,
It sounds you like want to do 2 different things, so let me first explain:
The "Create GlobalVariables" menu entry is to create a new copy of your global variables. One default instance is always there and is
stored inside the database. If you want another copy, for example to have 2 different flow player work at the same time but with completely independent data, you would create such a clone. But this is not necessary for saving.
If you want to save your global variables, that can be done via the Variables property on your global variables. If you want to save the default variables, just use ArticyDatabase.DefaultGlobalVariables.
How you save is up to you, for example if you are using JSON.NET, it could look something like this
- Code: Select all
// save the variables
File.WriteAllText(@"d:\SaveFile.json", JsonConvert.SerializeObject(ArticyDatabase.DefaultGlobalVariables.Variables));
// load the variables
ArticyDatabase.DefaultGlobalVariables.Variables = JsonConvert.DeserializeObject<Dictionary<string, object>>(File.ReadAllText(@"D:\SaveFile.json"));
If you don't have JSON.NET you could also write to a text file like this.
This is how you would save
- Code: Select all
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\SaveFile.txt"))
{
foreach (var pair in ArticyDatabase.DefaultGlobalVariables.Variables)
{
file.WriteLine(string.Format("{0} = {1}", pair.Key, pair.Value));
}
}
and this how you read the data
- Code: Select all
using (System.IO.StreamReader file = new System.IO.StreamReader(@"D:\SaveFile.txt"))
{
Dictionary<string, object> savedVars = new Dictionary<string, object>();
string line;
while ((line = file.ReadLine()) != null)
{
var split = line.Split('=');
var name = split[0];
var value = split[1];
savedVars[name] = name;
UnityEngine.Debug.LogFormat("Read saved var: {0} Value: {1}", name, value);
}
ArticyDatabase.DefaultGlobalVariables.Variables = savedVars;
}
I don't find if it's exist a method to do like "GetArticyGlobalVariable" to save. And "SetArticyGlobalVariable" to load.
There are methods to set variables directly, but not on the database. You will find those on the global variables. For example if you want to change a variable on the default set of global variables, you could write something like this:
- Code: Select all
ArticyDatabase.DefaultGlobalVariables.SetVariableByString("GameState.Health", 100);
var health = ArticyDatabase.DefaultGlobalVariables.GetVariableByString<int>("GameState.Health");
Hope that clears things up a bit,
Best regards
Nico