Scenes and the editor

A scene is a set of entities saved to a file. There's no per-type save code: every reflected component on an entity tagged SceneEntityTag is saved and loaded by the same generic code. Add a field to a component, and it's in your scenes.

Loading a scene

Loading the level
SceneSystem scenes;
scene_system_create(&scenes, world, assets, &alloc);
SceneHandle level = scene_load_file(&scenes, "my_game/main.hzt");

Loading is asynchronous: the entities appear a few frames later, once their assets are in. Systems don't care, they just start matching when the entities exist. scene_unload removes the scene's entities again.

Scenes are saved as .hzt text: a small manifest plus chunks of entities. They diff well in git, and you can edit them by hand.

The editor

Kaigen doesn't have an editor application. The editor is code that runs inside your game, on top of the same world, so what you edit is the game itself, running.

The templates already have it wired up: press F1 to open it and F5 to save the scene. In it you get:

The editor is made of parts (ecs_editor_default_init puts all of them together), so you can also build your own tools out of them: a level editor for your game, a tuning panel, a material editor. See hz/src/engine/ecs/ecs_editor/.

Authored and runtime data

Saving writes what the components hold right now. If a system is animating a door when you press save, the half-open door is what's saved. So keep the two apart:

The template does this with its SolidBox tag: the scene only says "this entity is a solid box", and a system adds the physics collider when the entity appears.

Worth knowing

The API is in hz/src/engine/scene/scene.h.