Save system and new actor fields

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
ava
Posts: 17
Joined: Tue Oct 30, 2018 2:53 pm

Save system and new actor fields

Post 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!
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save system and new actor fields

Post 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;
                    }
                }
            }
        }
    }
}
ava
Posts: 17
Joined: Tue Oct 30, 2018 2:53 pm

Re: Save system and new actor fields

Post by ava »

Superb! Thanks!
ava
Posts: 17
Joined: Tue Oct 30, 2018 2:53 pm

Re: Save system and new actor fields

Post 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);
                }
            }
        }
    }
}
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save system and new actor fields

Post 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.
Post Reply