Creating interactive linked groups in TrenchBroom

Creating interactive linked groups
Creating interactive linked groups

Table of contents:

1. TrenchBroom linked groups

TrenchBroom 2022.1 introduced a powerful feature called 'linked groups'. Similar to a template instancing system, it lets the user create linked duplicates of a group of brushes and entities. Any changes made to a duplicate are automatically applied to all other linked duplicates.

For example, all the chairs in this screenshot are linked duplicates of one another:

Office chairs, linked groups
Office chairs, linked groups

Modifying one of the chairs automatically modifies all of them:

Modified office chairs
Modified office chairs

2. Creating a toggleable desk lamp

We can make this office a bit more interactive by creating a toggleable desk lamp. First, we'll create the lamp itself:

A simple desk lamp
A simple desk lamp

Next, we'll add a light entity with the following properties:

key value
targetnamedesk_light
_light255 255 255 200

Now we'll turn the lamp into a func_button with these properties:

key value
targetdesk_light
spawnflags33 (Don't move, Toggle)

Let's compile our map to make sure that our lamp works:

Desk lamp in-game
Desk lamp in-game

3. Duplicating the desk lamp

Our office needs more light, so let's group the lamp and the light together (Ctrl + G) and create a linked duplicate of that group (Ctrl + Shift + D). We can now move this duplicate to another desk and rotate it a bit:

Duplicated desk lamps
Duplicated desk lamps

However, we do have a problem now: all lamps and lights use the same target name (desk_light), so using any desk lamp will toggle all lights! We could solve this by protecting the targetname and target properties, which allows us to override their values in specific instances, but then we would have to modify every single instance separately. That makes our linked groups harder to use, and we could easily forget to update a few instances!

Fortunately, MESS has special support for linked groups. If we add {_tb_group} to an entity property value, MESS will replace that with the ID of the current linked group instance. All we need to do is to change the name of our light:

key value
targetnamedesk_light{_tb_group}

And the target of our button:

key value
targetdesk_light{_tb_group}

If we recompile the map, we can see that using a lamp now only toggles its own light:

Duplicated desk lamps with unique names
Duplicated desk lamps with unique names

If we open the processed .map file, we can see that MESS changed the names and targets of our entities to desk_light6 and desk_light7. The actual numbers shouldn't be relied upon - they may change when linked groups are added or removed. What's important is that each instance now uses distinct entity names.

And that's all there is to it: adding {_tb_group} to the name or target of an entity will result in different names in different instances.

4. Example map

Example map: