Save Global Variable

Fri 10. Mar 2017, 13:50

Hi,

I would like to save the Global variable and load them when I relaunch my game.
I don't find if it's exist a method to do like "GetArticyGlobalVariable" to save. And "SetArticyGlobalVariable" to load.
I tried to pass by an other GlobalVariable Set, but when I create this kind of object by Articy=> Advanced=> Create GlobalVariable and put on my ArticyFlowPlayer it, this one it's not modified or call by some

Code: Select all
var result = articyZone.Template.ZoneCondition.ClickCondition.CallScript();

or
Code: Select all
articyZone.Template.ZoneCondition.OnClickInstruction.CallScript(sceneHandler);


So Is there a mean to say to all this script to use an other set than DefaultGlobalVariable ?
Or a better idea to how save GlobalVariable and Load them?

Thank you !

Have a nice day
Guest
 

Re: Save Global Variable

Fri 10. Mar 2017, 13:51

Arf it's posted as Guest ><
Sorry
User avatar
Zeldarck
 
Posts: 55
Joined: Thu 9. Mar 2017, 16:03

Re: Save Global Variable

Fri 10. Mar 2017, 14:45

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
Nico Probst
Senior Software Engineer | Articy | LinkedIn
User avatar
[Articy] Nico Probst
Articy Staff
Articy Staff
 
Posts: 217
Joined: Wed 23. Nov 2011, 09:45
Location: Bochum

Re: Save Global Variable

Fri 10. Mar 2017, 14:58

Yeah it's perfect, thank you :)

I searched this kind method in Articy.Unity.BaseGlobalVariables ^^"
User avatar
Zeldarck
 
Posts: 55
Joined: Thu 9. Mar 2017, 16:03

Re: Save Global Variable

Tue 14. Mar 2017, 13:17

Hum, me again ^^'

It's there a fonction to save the articy database and load it? It's the better mean to save my game :)
User avatar
Zeldarck
 
Posts: 55
Joined: Thu 9. Mar 2017, 16:03

Re: Save Global Variable

Tue 14. Mar 2017, 15:19

It's there a fonction to save the articy database and load it?


At this point, no. Loading and saving is usually very project specific and i didn't want to create a half baked solution just to have one.
That doesn't mean of course that i'm not planning to incorporate that at some point, because saving and loading in most games is rather important and most people can live with a simple but basic save/loading functionality, without the need of versions, deltas and different formats (binary, json) etc.

But you can of course build your own save/load functionality, especially making use of the setProp() and getProp() methods on each object.
While this can be a bit cumbersome, especially if you can't make sure which objects/types/property you need to save, it should work.

Here is a very basic solution to get you started, this uses json, but using BinaryWriter/Reader should be very similar.

Code: Select all
public class SaveFileHandler
{
   public void Save()
   {
      using (StreamWriter file = File.CreateText(@"D:\save.json"))
      {
         using (JsonWriter writer = new JsonTextWriter(file))
         {
            writer.Formatting = Formatting.Indented;

            var playerCharacter = ArticyDatabase.GetObject<Character>("PlayerCharacter");

            writer.WriteStartObject();
            WriteObjectProperty(writer, playerCharacter, "DisplayName");
            // read all the other important properties

            writer.WriteEndObject();
         }
      }
   }

   public void Load()
   {
      var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(File.ReadAllText(@"D:\save.json"));

      var playerCharacter = ArticyDatabase.GetObject<Character>("PlayerCharacter");
      playerCharacter.setProp("DisplayName", values["DisplayName"]);
      // read all the other important properties
   }


   public void WriteObjectProperty(JsonWriter aJsonWriter, IPropertyProvider aObject, string aName)
   {
      aJsonWriter.WritePropertyName(aName);
      aJsonWriter.WriteValue((string)aObject.getProp(aName));
   }
}


As you see you have to write down which objects and which property you want to save/load. While not perfect, it might be a start for your solution.

Hope that helps you a bit!

Best regards

Nico
Nico Probst
Senior Software Engineer | Articy | LinkedIn
User avatar
[Articy] Nico Probst
Articy Staff
Articy Staff
 
Posts: 217
Joined: Wed 23. Nov 2011, 09:45
Location: Bochum

Re: Save Global Variable

Sun 26. Mar 2017, 11:03

Ok thank you :)

I think it's can be cool if you developped a solution to just save/load/duplicate all database :)

Have a nice day
User avatar
Zeldarck
 
Posts: 55
Joined: Thu 9. Mar 2017, 16:03

Re: Save Global Variable

Tue 25. Jul 2017, 12:03

I was back on save of the databases:
Code: Select all
    public void OnApplicationPause(bool pauseStatus)
    {
        Save();
    }

    public void OnApplicationQuit()
    {
        Save();
    }



    /// <summary>
    ///  Sauvegarde - à refaire
    /// </summary>
    public void Save()
    {
        //Manque save des isNew des feature DisplayCondition

        if (isLoaded)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.persistentDataPath  + "\\SaveFileDatabase.txt"))
            {
                foreach (var pair in ArticyDatabase.DefaultGlobalVariables.Variables)
                {
                    UnityEngine.Debug.LogFormat("Save var: {0} Value: {1}.", pair.Key, pair.Value);
         
                    file.WriteLine(string.Format("{0}={1}", pair.Key, pair.Value));
                }



            }


            using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.persistentDataPath + "\\SaveFileTemplate.txt"))
            {

                List<Message> listMessage = ArticyDatabase.GetAllOfType<Message>();
                file.WriteLine("Message");
                foreach (Message msg in listMessage)
                {

                    file.WriteLine(string.Format("{0}={1}={2}", msg.TechnicalName, "Read",msg.Template.Path.isPath));

                }

                List<CallEvent> listCall = ArticyDatabase.GetAllOfType<CallEvent>();
                file.WriteLine("CallEvent");
                foreach (CallEvent call in listCall)
                {

                    file.WriteLine(string.Format("{0}={1}={2}", call.TechnicalName, "Read", call.Template.Call.Listen));
                }
            }
        }
    }



I didn't change the function wich worked in the past.

If I change the status of a boolean variable, if I click outside of Unity and back, the save it's ok (variabe=True is write in save file). But when I leave the game, the variable it's save again but to the old states (variable=False is write in save file). Have you change anything on the behavior of databases and global variables since March when we leave the game?
User avatar
Zeldarck
 
Posts: 55
Joined: Thu 9. Mar 2017, 16:03

Re: Save Global Variable

Tue 25. Jul 2017, 13:06

Hi Zeldarck,

besides bug fixes in how the database asset itself is stored, i can't recall any changes that could affect global variables.
But i'm not sure i understand the issue, i try to recap:


  • You have the game running in the editor
  • You change a boolean variable, lets say true
  • You use your save method to write all variables into the save file. Is the variable here still true? Because it should.
  • You leave the play mode and are back into the editor, here the boolean variable is false, which would be correct. Or is your Save method called again? That would be wrong

Best regards

Nico
Nico Probst
Senior Software Engineer | Articy | LinkedIn
User avatar
[Articy] Nico Probst
Articy Staff
Articy Staff
 
Posts: 217
Joined: Wed 23. Nov 2011, 09:45
Location: Bochum

Re: Save Global Variable

Tue 25. Jul 2017, 13:12

My method for save is call when we leave or pause the game. (OnApplicationPause && OnApplicationQuit of Unity)

So :
  • Game running in the editor
  • Change a variable to true
  • Pause the game => the articy variable variable stay at true - the variable still true
  • I exit play mode => Save method is called - but the articy variable is false at this moment

EDIT : Hum, I just tested on my android phone and here it's worked well. It's come of the editor Unity so. i am on 5.6.1f1
User avatar
Zeldarck
 
Posts: 55
Joined: Thu 9. Mar 2017, 16:03

Next

Return to articy:draft Unity Importer

Who is online

Users browsing this forum: No registered users and 75 guests

Who We Are
Contact Us
Social Links