Ix Dev 3 - Templates

Ix Game Engine - Templates

As mentioned, Ix has both a low-level system layer (runGameUpdate), a higher-level DSL through Effects, and Entities of different types (player, enemy, hero, area, block, etc). Bridging these layers, and forming the meat of the game content, is the template system.

Template Shapes

Templates are largely plain JSON objects, loaded at game startup (both new game and upon loading a saved game). All templates have a templateType value. Templates within a category have a standardized form - shortcut templates have a type distinct from recipe templates. Some templates are further specialized - Entity templates are used to determine preconfigured entities, and have entity-type-specific overrides to enable creating any shape of entity through JSON (or IxScript).

Loading syntax highlighter...

Loading templates

Core game templates are loaded at start:

Loading syntax highlighter...

Using templates

When needing a definition for an ID - eg an item definition - I use a standardized getTemplate(type, id)/getRequiredTemplate(type, id) pair, with the former nullable and the latter throwing if the template isn't found. I can also use getTemplates(type) to get the full list for a category.

The game has a concept of core templates and "local" templates; core templates are discarded on save and loaded fresh every page load, while local templates are persisted to the save file. When fetching template definitions, local templates take precedence over core templates, allowing runtime overrides.

At the moment, local templates are used as an in-game development tool; using the scripting language and either the quick-script console, or the full monaco editor, I can quick spawn entities, add buttons to the UI, and test game logic updates. I hope to extend it further down the road as a standardized modding tool, making it easy to customize the game as a player.

Note: most templates are loaded as JSON, but some template categories (items, recipes) are verbose and have many records; for those, I still use a JSON template object, but I have a TSV parser and light inheritance to simplify things.

template templates

Why use templates?

Using a standardized model and language makes it easy to work with templates as data, and to build better developer tooling to support them. It's clear where the logic for a game component or mod should live, and putting the core interactions (abilities, shortcuts, items, game screens) in data helps force the system to be flexible and extendable.

This isn't a new concept - Factorio famously uses "prototypes" for their modding layer and much of the core game logic, and many game engines encourage using "data" in some form to store entity definitions.

My solution at the moment:

  • allows runtime modification for most templates
  • describes itself via TemplateTemplates, making it easier for me to build in-game tooling
  • is exposed via the IxScript console and script editor:
Loading syntax highlighter...
item templates