Saving data for runtime generated actors

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
nratcliff
Posts: 23
Joined: Tue Sep 27, 2022 10:47 am

Saving data for runtime generated actors

Post by nratcliff »

We are generating NPCs with corresponding dialogue actors at runtime. We're adding these actors to the database on generation (when the player first encounters a spawn point for a new NPC in the world):

Code: Select all

actor = new Actor(npcActorTemplate)
{
	Name = characterData.FullName,
	id = DialogueManager.MasterDatabase.actors.Count
};

var database = CreateInstance<DialogueDatabase>();
database.actors.Add(actor);
DialogueManager.AddDatabase(database);
We also assign some data to the actor's lua fields during generation and gameplay. For example, we set "HasMetPlayer" to true when the NPC has met the player for the first time.

When the game is saved and reloaded the save data is restored to the lua system properly. However, the actor data is lost when the actor is later added to the database using DialogueManager.AddDatabase during generation. As far as I can tell this is due to AddToTable called from AddChatMapperVariables because it is replacing the actor's fields table with the fields on the actor being added (empty/template).

I'm adding the actors to the database with AddDatabase so their lua data isn't lost in the future when Quest Engine calls AddDatabase to add generated dialogue for actors.

Is there any way to ensure the lua data is preserved when we generate the actor? I thought about getting the Actor table when OnApplyPersistentData is called but I couldn't figure out how to convert the actors in the Actor table to database actors.

Any direction on the best way to go about this is highly appreciated!
Lead Dev @ aesthetic.games
Creator of Easy Feedback Form
User avatar
Tony Li
Posts: 21970
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving data for runtime generated actors

Post by Tony Li »

Hi,

If you're saving games using the Dialogue System's save system, make sure to add a DialogueSystemSaver component to the Dialogue Manager GameObject and set a unique key. If you're using the save system or just calling PersistentDataManager.GetData() yourself, tick the Dialogue Manager's Persistent Data Settings > Include Actor Data. This will save all actors in the Actor[] Lua table.

When you spawn an NPC, you can check if the Actor[] table already has an actor in the Actor[] table and, if so, use it.

It generally shouldn't matter that the actor isn't in the dialogue database. But you can add the actor to the dialogue database, too, by using these steps:

1. Tick the Dialogue Manager's Other Settings > Instantiate Database checkbox. This will ensure that you're modifying an in-memory instantiated copy of the database instead of directly changing the asset file.

2. Manually add an Actor object to DialogueManager.masterDatabase:

Code: Select all

actor = new Actor(npcActorTemplate)
{
	Name = characterData.FullName,
	id = DialogueManager.MasterDatabase.actors.Count
};
// Actor is already in Lua table, so just add to database to keep them parallel:
DialogueManager.masterDatabase.actors.Add(actor);
User avatar
nratcliff
Posts: 23
Joined: Tue Sep 27, 2022 10:47 am

Re: Saving data for runtime generated actors

Post by nratcliff »

Thanks so much for the quick reply! You saved my butt on the last day before build lock. :D

That I just need to care about the lua data is the key detail I needed. For some reason I was under the impression that the actors would need to be in the runtime database, too. Ignoring that solved all the issues!
Lead Dev @ aesthetic.games
Creator of Easy Feedback Form
User avatar
Tony Li
Posts: 21970
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving data for runtime generated actors

Post by Tony Li »

Glad to help!
Post Reply