Page 2 of 2

Re: get variables from saved without applying to database?

Posted: Thu Mar 16, 2023 12:50 am
by joeylu
Hi Tony, your demo runs fine, so I guess it must be something from my code.

by adding few debug in Assignments.cs, I have found the error might relates to the conversation[x] parts from the saved log

here are my conversation[x] records looks like in the savedData

Code: Select all

Conversation[1].SimX="1;u;2;u;3;u;4;u;5;u;6;u;7;u;8;u;9;u;10;u;11;u;12;u;13;u;14;u;15;u;0;u;17;u;18;u;19;u;20;u;21;u;22;u;23;u;24;u;25;u;26;u;27;u;28;u;29;u"; Conversation[2].SimX="1;d;2;d;3;d;4;d;5;d;6;d;7;d;0;d"; Conversation[2].SimX="1;u;2;u;3;u;4;u;5;u;0;u;7;u;8;u;9;u;10;u;11;u;12;u;13;u;14;u"; 
and the null error showing: SimX Cannot assign to a null value. Are you trying to assign to a nonexistent table element?
after I added "nameAccess.Name" in line #47 in assignments.cs

perhaps I need to Language.Lua.LuaInterpreter.Interpreter those conversation records as well? let me know your thoughts

Re: get variables from saved without applying to database?

Posted: Thu Mar 16, 2023 8:59 am
by Tony Li
Yes, you'll need a little more code since you're using SimStatus. Here's the updated script:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class TestLua : MonoBehaviour
{
    string saveData;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1))
        {
            saveData = PersistentDataManager.GetSaveData();
            Debug.Log(saveData);
        }
        else if (Input.GetKeyDown(KeyCode.F2))
        {
            var tempLuaEnvironment = Language.Lua.LuaInterpreter.CreateGlobalEnviroment();
            Language.Lua.LuaInterpreter.Interpreter("Actor = {}; Item = {}; Quest = Item; Location = {}; Conversation = {}; Variable = {}; Variable[\"Alert\"] = \"\"", tempLuaEnvironment);
            foreach (var item in DialogueManager.masterDatabase.items)
            {
                var itemIndex = DialogueLua.StringToTableIndex(item.Name);
                Language.Lua.LuaInterpreter.Interpreter($"Item['{itemIndex}'] = {{ Name='{item.Name}' }}", tempLuaEnvironment);
            }
            foreach (var conversation in DialogueManager.masterDatabase.conversations)
            {
                Language.Lua.LuaInterpreter.Interpreter($"Conversation[{conversation.id}] = {{ }}", tempLuaEnvironment);
            }
            Language.Lua.LuaInterpreter.Interpreter(saveData, tempLuaEnvironment);
            var variableValue = new Lua.Result(Language.Lua.LuaInterpreter.Interpreter("return Variable['enemiesKilled']", tempLuaEnvironment));
            Debug.Log($"enemiesKilled: {variableValue.asInt}");
        }
    }
}
I just added this bit:

Code: Select all

            foreach (var conversation in DialogueManager.masterDatabase.conversations)
            {
                Language.Lua.LuaInterpreter.Interpreter($"Conversation[{conversation.id}] = {{ }}", tempLuaEnvironment);
            }

Re: get variables from saved without applying to database?

Posted: Sat Mar 18, 2023 12:29 am
by joeylu
that's exactly what causing the problem, thank you again

Re: get variables from saved without applying to database?

Posted: Sat Mar 18, 2023 8:32 am
by Tony Li
Glad to help!

Re: get variables from saved without applying to database?

Posted: Tue Apr 04, 2023 1:26 pm
by Tony Li
This is a version that reads save data from the Dialogue System save system, for folks who are wondering how to do that:

Code: Select all

using UnityEngine;
using PixelCrushers;
using PixelCrushers.DialogueSystem;

public class TestLua : MonoBehaviour
{
    public int saveSlot = 1;
    public string dialogueSystemSaverKey = "ds";

    string saveData;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1))
        {
            var savedGame = SaveSystem.storer.RetrieveSavedGameData(saveSlot);
            saveData = savedGame.GetData(dialogueSystemSaverKey);
            Debug.Log(saveData);
        }
        else if (Input.GetKeyDown(KeyCode.F2))
        {
            var tempLuaEnvironment = Language.Lua.LuaInterpreter.CreateGlobalEnviroment();
            Language.Lua.LuaInterpreter.Interpreter("Actor = {}; Item = {}; Quest = Item; Location = {}; Conversation = {}; Variable = {}; Variable[\"Alert\"] = \"\"", tempLuaEnvironment);
            foreach (var item in DialogueManager.masterDatabase.items)
            {
                var itemIndex = DialogueLua.StringToTableIndex(item.Name);
                Language.Lua.LuaInterpreter.Interpreter($"Item['{itemIndex}'] = {{ Name='{item.Name}' }}", tempLuaEnvironment);
            }
            Language.Lua.LuaInterpreter.Interpreter(saveData, tempLuaEnvironment);
            var variableValue = new Lua.Result(Language.Lua.LuaInterpreter.Interpreter("return Variable['enemiesKilled']", tempLuaEnvironment));
            Debug.Log($"enemiesKilled: {variableValue.asInt}");
        }
    }
}