defold-trenchfold
defold-trenchfold copied to clipboard
A toolkit to design a level in TrenchBroom and export it to Defold

TrenchFold
๐ผ Also in this series:
- ๐ Kinematic Walker
- ๐ฅ Operator
- ๐ Illumination
- ๐ง Blockout Textures
Overview
This is a kit of game configuration files and importing scripts to design your level with TrenchBroom and export it to Defold as the collection.
TrenchBroom was originally created to design Quake-format levels, but thanks to its flexible game configurations it's suitable for any other game with low-polygon level geometry. It's cross-platform, has a great manual and usability.
๐ฎ Play HTML5 demo.
๐ฌ Discuss on the forum.
๐งช Look at Retro Texture Pack by Little Martian used in the demo.
Features
- [x] Convert level geometry to meshes and collision objects.
- [x] Use flag textures and checkboxes to define faces behavior.
- [x] Place triggers, kinematic or dynamic bodies.
- [x] Convert entities to game objects.
- [x] Attach file components to your entities.
- [x] Set custom entity properties and read them from the game logic.
- [x] Define areas and handle their coordinates in scripts.
- [x] Run importing with the editor script or the standalone lua module.
- [x] Expand the game configuration file with your own classes.
- [ ] Request by adding an issue or contribute.
Running the Example
To run the example project, you first need to run two editor extension functions on a map file provided, according the Export and Import part.
With the project open in Defold, right click on the level.map file then click on Convert Map to Collection. This will create a variety of folders and files next to the .map file. You can now build and run the example.
Install
- Add link to the zip-archive of the latest version of defold-trenchfold to your Defold project as dependency.
- Copy the
trenchfold/games/Defoldfolder according this instruction to TrenchBroom user data folder. - Place your texture packs at path
assets/texturesto use them in TrenchBroom. - Set your game project path as the game path in TrenchBroom preferences when creating the first map.
- Setup
textel_sizeandmaterialin the worldspawn entity.
Export and Import
Export
Before import you need to export .obj file from TrenchBroom by menu File / Export / Wavefront OBJ. The importing script uses .obj data to parse vertices, so it must be done every time the geometry is changed.
It would be possible to skip this step by solving the issue #1.
Import
Editor Script
Find your .map file in the resources pane of the editor and right click on it and select the Convert Map to Collection action. It does the magic and creates the collection file and all the components files: buffers, meshes, convexshapes, collisionobjects and some scripts.
Lua Module
There is also the trenchfold/cli.lua module to run the import script outside the editor. Just pass it two arguments - relative/map_folder and map_name.
For example, there is .vscode/launch.json to run trenchfold/cli.lua with the level map in VS Code. You can try to run it by installing Defold Kit or just local-lua-debugger.
Textures

The game configuration includes marking textures at flags/textures. They are handled by the exporting script to provide specific behaviour to the faces without normal textures.
unused
This face will be skipped when exporting.
Use it to remove useless faces from the geometry.
clip
Creates a collision object without texture.
Use it to create invisible walls and useful collision geometry.
trigger
Creates a trigger collision object.
area
Doesn't create collision objects but its vertices positions will be sent to the object with the init_area message.
Use it to process the area programmaticaly.
Flags
There are few content flags in the face properties.
ghost
The face isn't solid and doesn't generate a collision object vertices.
Use it on objects that can be passed through or that the player will never reach.
separated
The face generates a separate plane collision object.
A rare use case is when you have a wall corner with two solid faces and you don't want to create a triangular prism collision shape on its vertices.
Entities

There are brush entities and point entities. The difference is that a brush entity contains geometry brushes, while the point entity has only an origin position and rotation.
worldspawn
The default entity for all the geometry outside of the other entities. Also has some general settings of exporting.
textel_sizeโ how much Trenchbroom grid units are equal to one unit in Defold metrics.materialโ the relative path to the material that will be used for generated meshes by default.textureNโ path to the texture whereNis number from1to7. See texture path patterns.physics_*โ collision object properties used by default.
static*
A brush entity with the static collision type. The only reason to use it is to attach components and set properties because the worldspawn is static by default. To use areas or destroy parts of the level, e.g.
idโ the identifier of the game object.#component_idโ the relative path to the file component that will be attached to this game object ascomponent_id.#component_id.propertyโ the script component property override.materialโ the relative path to the material that will be set in generated meshes.textureNโ path to the texture whereNis number from1to7. See texture path patterns.physics_*โ collision object properties related to static collision type.
trigger*
To be fair, triggers are created by the trigger texture, not the entity. But you also need to put scripts and parameters on this trigger, so which is what this entity is for.
If you place brushes with normal textures to this entity they also become triggers.
idโ the identifier of the game object.#component_idโ the relative path to the file component that will be attached to this game object ascomponent_id.#component_id.propertyโ the script component property override.physics_*โ collision object properties related to trigger collision type.
kinematic*
A brush entity with the kinematic collision type. Use it for moving platforms or sliding doors, for example.
idโ the identifier of the game object.#component_idโ the relative path to the file component that will be attached to this game object ascomponent_id.#component_id.propertyโ the script component property override.materialโ the relative path to the material that will be set in generated meshes.textureNโ path to the texture whereNis number from1to7. See texture path patterns.physics_*โ collision object properties related to kinematic collision type.
dynamic*
A brush entity with dthe ynamic collision type. This could be, for example, a crate that the player can move.
idโ the identifier of the game object.#component_idโ the relative path to the file component that will be attached to this game object ascomponent_id.#component_id.propertyโ the script component property override.materialโ the relative path to the material that will be set in generated meshes.textureNโ path to the texture whereNis number from1to7. See texture path patterns.physics_*โ collision object properties related to dynamic collision type.
go
This is a point entity to add a game object without meshes and collision objects. You can attach any file components to it or replace it with you .go file.
originโ the position of the game object that defined automatically when you place it.angleโ the Y-axis rotation of the game object that defined automatically when you rotate it.rotationโ X, Y and Z-axis rotation of the game object. Y-axis will be ignored if theangleproperty exists.idโ the identifier of the game object.goโ the relative path to the.gofile that should replace the entity.#component_idโ the relative path to the file component that will be attached to this game object ascomponent_id. Ignored if thegoproperty exists.#component_id.propertyโ the script component property override.
illumination, light_point, light_spot
These are helpers for placing ๐ก Illumination objects on the map. Don't forget to fill the go property with default value.
Texture Path Patterns
The textureN property allows to set additional material textures 1-7. The next patterns are available:
/path/to/texture.pngโ a specific texture./path/to/prefix_*_suffix.pngโ a specific path where * is texture0 original name./path/to/alternative/*โ a specific folder with the same texture file name.prefix_*_suffix.pngโ the same folder.prefix_*_suffixโ the same folder and extension.
Custom Properties
All the custom properties will be a part of the script component with the properties identifier that attached to the game object. These properties can be accessed in runtime by calling go.get().
go.get('#properties', 'property')
bool
The values true and false are converted to boolean.
number
The value which can be handled with tonumber() is converted to number.
If the number is flags value then you can parse it with utils.flags_from_integer(value) from the trenchfold/utils.lua module.
vectors
- Value
x yis converted tomath.vector3(x, y, 0). - Value
x y zis converted tomath.vector3(x, y, w). - Value
x y z wis converted tomath.vector4(x, y, z, w).
*url
Property ending with url is converted to msg.url('value').
hash
Any other string property is converted to hash 'value'.