I have an NPC class with a DialogueTree object as a field. DialogueTree objects are automatically constructed recursively by loading in the contents of .xml files where they're stored.

In building my XMLDialogueLoader class, I see two main approaches:

  1. Load in every dialogues for every NPC on game start, available as a sort of pool from which any components that depend on it can access. Sort of like a global dictionary, when NPCs are constructed they index their ID in that Dialogue pool to retrieve their dialogues.
dialogue_pool: dict[str, DialogueTree] = dialogue_loader.load('dialogues/')
npc = NPC('John', dialogue_pool['John']
npc = NPC('Tom', dialogue_pool['Tom']
  1. Load in every dialogue for NPCs only when they are being constructed. I would likely do this by creating an NPCFactory object that takes in a DialogueLoader with a method to load in a specific NPC's dialogue at a time, like self.dialogue_loader.load('npc_name')
class NPCFactory:
  dialogue_loader: DialogueLoader
  build(self, name: str):
    dialogue_tree = dialogue_loader.load(name)
    return NPC(name, dialogue_tree)

I suppose another set of variations would be loading in specific dialogues just in time rather than all at once, but that seems maybe excessive.

What advantages and disadvantages exist in these approaches? What is typical in industry? Are there better ways of doing this or alternative approaches?


1 Answer 1


There is little difference in the given approaches unless load speed is an issue(multi-megabyte trees).

The final memory utilization will be the same no matter what once they are loaded.

The industry "standard," if such a thing exists, would be to load all dialog for the current "level" or streamed partition, and dispose of it on level change(or partition exit).

Memory disposal is relatively easy in C++ (free). In Python, removal works but the effects can be delayed.

For most smaller games, the load times nor memory is an issue, always keep an eye on memory, just in case(excessive swapping will kill performance, so always test on minimum RAM):

So load away, and unload when no longer needed.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .