Modify Gameplay Tags on Entity and Get Entities with Tags
gameplayTags property on Entity
Adding new property gameplayTags on Entity in Typescript, which is of type HorizonSetProperty<string>.
/**
* tags can be used to annotate entities with user-defined labels to identify and match objects.
*
* @remarks
* You can have multiple tags for one entity, be mindful of performance.
* It is case sensitive. Avoid using special characters. There is no check for duplicate tags.
* Tags set or modified in TS will only presist for the session, they will not be stored in the entity.
*
* @privateremark
* Tags are represented as a single, comma delimited string (tokenised over ",")
* because our data model does not currently support arrays.
* We should migrate these to a proper type once that's supported.
*
*/
gameplayTags: HorizonSetProperty<string> = new HorizonSetProperty<string>(
() => {
return bridgeInstance.invoke(BridgeMethod.GetGameplayTags, this);
},
(gameplayTags) => {
bridgeInstance.invoke(
BridgeMethod.SetGameplayTags,
this,
'ScriptInstance_gameplayTags',
gameplayTags,
);
},
);
getEntitiesWithTags API on World
This function allows the creator to gather all entities in a world with specified tags. It enables richer gameplay and game scripting.
Some possible use-cases/examples:
At night, get all entities with the tag “nocturnal” and increase their speed
If the spider boss is killed, get all entities with the tag “spider babies” and have them run away
export enum SearchOption {
ContainsAny = 'ContainsAny',
ContainsAll = 'ContainsAll',
}
/**
* Gets all entities with matching {@link tags \| tags} currently in the world.
* @remarks
* function takes one or more tags as input parameter.
* It is case sensitive. Avoid using special characters.
* @param tags - a case sentitive string, avoid special characters.
* @param SearchOption - {@link SearchOption \| option}.
* ContainsAny - returns true if any of the given tags is found
* ContainsAll - returns true is all of the given tags are found
* @returns An array of all entities with the given tags in the world.
*/
getEntitiesWithTags(tags: string[], option: SearchOption): Entity[] {
return bridgeInstance.invoke(
BridgeMethod.GetEntitiesWithTags,
tags,
option,
);
}
TypeScript Example Code
gameplayTags on Entity
// add tags to the entity
entity.gameplayTags.add('tag1');
entity.gameplayTags.add('tag2');
entity.gameplayTags.add('tag3');
// check if the entity contains a specific tag
console.log(entity.gameplayTags.contains('tag1')); // true
console.log(entity.gameplayTags.contains('tag4')); // false
// get the number of tags in the entity
console.log(entity.gameplayTags.length()); // 3
// remove a tag from the entity
entity.gameplayTags.remove('tag2');
console.log(entity.gameplayTags.contains('tag2')); // false
console.log(entity.gameplayTags.length()); // 2
// iterate over the tags in the entity
for (const tag of entity.gameplayTags) {
console.log(tag);
}
// output:
// tag1
// tag3
// get the underlying Set of tags
const tagsSet = entity.gameplayTags.get();
console.log(tagsSet); // Set { 'tag1', 'tag3' }
// clear all tags from the entity
entity.gameplayTags.clear();
console.log(entity.gameplayTags.length()); // 0
// create a new Set of tags
const newTagsSet = new Set<string>(['tag4', 'tag5']);
// set the new Set of tags for the entity
entity.gameplayTags.set(newTagsSet);
// verify the new tags
console.log(entity.gameplayTags.get()); // Set { 'tag4', 'tag5' }
console.log(entity.gameplayTags.length()); // 2
getEntitiesWithTags on World
This video demonstrates the ability to do object search based on tagging, using a script similar to the example below. Here you can see how using AND|OR type conditions can be applied in a script to highlight or manipulate game objects.
entity1.gameplayTags.add('tag1');
entity1.gameplayTags.add('tag2');
entity1.gameplayTags.add('tag3');
// set all tags for entity2 vs adding tags individually
entity2.gameplayTags.set(['tag2', 'tag4']);
// search for entities containing any of the specified tags
const entitiesWithTag1orTag4 = world.getEntitiesWithTags(['tag1', 'tag4'], SearchOptionContainsAny);
console.log(entitiesWithTag1orTag4); // [entity1, entity2]
// search for entities containing all of the specified tags
const entitiesWithTag1andTag2 = world.getEntitiesWithTags(['tag1', 'tag2'], SearchOption.ContainsAll);
console.log(entitiesWithTag1andTag2); // [entity1]