Tags
Tags are names, labels or keywords that group game objects, such as enemies, friends, trees, etc. They are useful for classifying different types of objects.
Using tags
Tags can be added on the add([])
function array as a string.
const ghost = add([
sprite("ghosty"),
"enemy",
]);
This makes it so ghosty has a tag, enemy, and there’s many operations you can do with it.
Tag operations
How to add a tag
// Add a tag on creation
const ghosty = add([
sprite("ghosty"),
"enemy",
]);
// Add 1 tag or many tags!
ghosty.tag("friend"); // now is a friend :D
ghosty.tag(["friend", "invisible"]); // invisible forever
How to remove a tag
ghosty.untag("friend"); // D:
How to know if a tag exists in a game object
ghosty.is("enemy"); // true if tag is there, otherwise false
How to get a list of all tags
ghosty.tags; // read-only array with all tags.
// result: ["enemy"]
Using tags in events and methods
A large number of methods and functions work using tags.
obj.get("enemy"); // returns an array with all enemy game objects
Special tag: *
All game objects have the *
tag by default. This can be useful for example to
get all game objects
get("*"); // list of all game objects
Components as tags
In v3001, KAPLAY by default will take component IDs as tags:
// this means if your object have a sprite() component, it also have a "sprite" tag
const tga = add([
sprite("tga"),
]);
tga.is("sprite"); // true
In next versions this is disabled by default, but if you want to enable it or
disable it in v3001 version, you can use the tagsAsComponents
option:
kaplay({
tagsAsComponents: false,
});
tga.is("sprite"); // false