Page 1 of 1

Save system and new actor fields

Posted: Sat Sep 21, 2019 11:40 am
by ava
Hi,

In my database I added a new actorfield that I use here and there via DialogueLua. However, these fields don't exist in old saves.
Is it possible to add these new fields to old savegames when they are loaded somehow? Based on the version number of the database perhaps?

Thanks!

Re: Save system and new actor fields

Posted: Sat Sep 21, 2019 1:11 pm
by Tony Li
Hi,

When loading a saved game, the Dialogue System will automatically add any new variables, quest entries, and SimStatus (if enabled) that you've added since the save, but it doesn't do the same for actor fields.

I recommend making a subclass of DialogueSystemSaver. Use this subclass instead of DialogueSystemSaver. Override the ApplyData(string) method to do something like this:

Code: Select all

using PixelCrushers.DialogueSystem;
using Language.Lua;
public class CustomDialogueSystemSaver : DialogueSystemSaver
{
    public override ApplyData(string data)
    {
        base.ApplyData();
        AddMissingActorFields();
    }
    
    public void AddMissingActorFields()
    {
        foreach (var actor in DialogueManager.masterDatabase.actors)
        {
            foreach (var field in actor.fields)
            {
                if (DialogueLua.GetActorField(actor.Name, field.title) == LuaNil.Nil)
                { // Field isn't in Lua, so add it:
                    switch (field.type)
                    {
                        case FieldType.Boolean:
                            DialogueLua.SetActorField(actor.Name, field.title, Tools.StringToBool(field.value));
                            break;
                        case FieldType.Actor:
                        case FieldType.Item:
                        case FieldType.Location:
                        case FieldType.Number:
                            DialogueLua.SetVariable(actor.Name, field.title, Tools.StringToFloat(field.value));
                            break;
                        default:
                            DialogueLua.SetVariable(actor.Name, field.title, field.value);
                            break;
                    }
                }
            }
        }
    }
}

Re: Save system and new actor fields

Posted: Sat Sep 21, 2019 1:23 pm
by ava
Superb! Thanks!

Re: Save system and new actor fields

Posted: Sat Sep 21, 2019 1:32 pm
by ava
To be complete, I made it a little shorter:

Code: Select all

using PixelCrushers.DialogueSystem;
public class GotsDialogueSystemSaver : DialogueSystemSaver
{
    public override void ApplyData(string data)
    {
        base.ApplyData(data);
        AddMissingActorFields();
    }

    public void AddMissingActorFields()
    {
        foreach (var actor in DialogueManager.masterDatabase.actors)
        {
            foreach (var field in actor.fields)
            {
                if (!DialogueLua.GetActorField(actor.Name, field.title).hasReturnValue)
                {   // Field isn't in Lua, so add it:
                    DialogueLua.SetActorField(actor.Name, field.title, field.value);
                }
            }
        }
    }
}

Re: Save system and new actor fields

Posted: Sat Sep 21, 2019 7:03 pm
by Tony Li
Looks good! If your fields are numbers or Booleans, consider using the switch statement in my previous reply. This will ensure that the right type gets assigned in Lua.