Revision Difference
Entity_Tags#548091
<cat>Code.Entity</cat>
<title>Entity Tags</title>
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](https://asset.party/api/Sandbox.PhysicsShape) 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.⤶
⤶
<upload src="70c/8dac70d7ed26611.png" size="81491" name="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 )
{
}
```