S&box Wiki

Entity Tags

Entities can be tagged with string tags. These tags are networked, so are also available for query on the client.

Entity tags are (in most cases) automatically propagated to all physics shapes of the entity, which are then used for determine which shapes should collide with each other.

Game developers can then set up which tags collide with each other in the games' project settings.

image.png

Setting Tags

// set one tag on the entity player_entity.Tags.Add( "player" ); // set multiple tags on the entity enemy_entity.Tags.Add( "enemy", "threat" ); // updating tags programmatically (adds if true, removes if false) door_entity.Tags.Set( "locked", IsLocked() ); // removing tags enemy_entity.Tags.Remove( "threat" );

Querying Tags

if ( enemy.Tags.Has( "threat" ) ) { // run away }

Using in Traces

// Only hit entities with the world tag tr = Trace.Ray( startpos, endpos ) .WithTag( "world" ) .Run(); // Hit enemies that are a threat tr = Trace.Ray( startpos, endpos ) .WithAllTags( "enemy", "threat" ) .Run(); // Hit enemies or players tr = Trace.Ray( startpos, endpos ) .WithAnyTags( "enemy", "player" ) .Run(); // Hit anything but players tr = Trace.Ray( startpos, endpos ) .WithoutTags( "player" ) .Run(); // Hit anything but players and world tr = Trace.Ray( startpos, endpos ) .WithoutTags( "player", "world" ) .Run();

Detecting Tag Changes

protected override void OnTagAdded( string tag ) { } protected override void OnTagRemoved( string tag ) { }