Revision Difference
Entity_Tags#544114
<cat>Code.Misc</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.
# 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 programatically (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 )⤶
{⤶
⤶
}⤶
⤶
```⤶