atl/trigger.hpp
Area triggers (sm.areaTrigger): volumes that report what enters, stays in and leaves them.
Functions
Section titled "Functions"inline AreaTrigger trackTrigger(Value v);Takes ownership of a trigger made in Lua, so it's destroyed with the current script like the ones the sm::areaTrigger functions create.
Parameters
v(Value): the Lua AreaTrigger.
Returns AreaTrigger: the tracked trigger.
AreaTrigger
Section titled "AreaTrigger"An invisible volume in the world that reports which objects are inside it.
Use one to react when a player walks into an area, to find the shapes near a part, or to make a pool of water or lava. Create triggers with the sm::areaTrigger functions, then read what is inside with contents() or get told about changes with onEnter(), onStay() and onExit().
A trigger created by a script is destroyed automatically when that script is destroyed, and its callbacks are freed with it. Call destroy() to remove it earlier.
class AreaTrigger : public atl::ObjectAreaTrigger() = default;An empty trigger that refers to nothing. Assign a created trigger to it before use.
explicit AreaTrigger(atl::Value v);Wraps an AreaTrigger value that came from Lua.
Parameters
v(atl::Value): the Lua AreaTrigger.
int id() const;The trigger's id, unique while the trigger exists.
Returns int: the id.
Lua: AreaTrigger:getId
Vec3 worldPosition() const;Where the trigger's center is in the world.
Returns Vec3: the position, in meters.
Lua: AreaTrigger:getWorldPosition
Quat worldRotation() const;How the trigger is rotated in the world.
Returns Quat: the rotation.
Lua: AreaTrigger:getWorldRotation
void setWorldPosition(Vec3 p) const;Moves the trigger. Has no effect on a trigger attached to a host, which follows the host.
Parameters
p(Vec3): the new center position, in meters.
Lua: AreaTrigger:setWorldPosition
void setWorldRotation(Quat q) const;Rotates the trigger. Has no effect on a trigger attached to a host.
Parameters
q(Quat): the new rotation.
Lua: AreaTrigger:setWorldRotation
Vec3 size() const;The size of the volume.
Returns Vec3: for a box, its size along x, y and z; for a sphere, the radius in x; for a cylinder, the radius in x and the half length in z.
Lua: AreaTrigger:getSize
void setSize(Vec3 s) const;Resizes the volume.
Parameters
s(Vec3): the new size, in the same layout size() returns.
Lua: AreaTrigger:setSize
Aabb worldBounds() const;The axis-aligned box in world space that contains the whole trigger.
Returns Aabb: the box's minimum and maximum corners.
Lua: AreaTrigger:getWorldMin
World world() const;The world the trigger is in.
Returns World: the world.
Lua: AreaTrigger:getWorld
Value userData() const;The user data given when the trigger was created (Options::userData).
Returns Value: the user data, or nil if none was given.
Lua: AreaTrigger:getUserData
Interactable hostInteractable() const;The interactable of the part this trigger is attached to.
Returns Interactable: the interactable, or an empty one if the trigger isn't attached to a part.
Lua: AreaTrigger:getHostInteractable
Vector<Target> contents() const;Everything inside the trigger right now: bodies, characters and harvestables, plus single shapes if setIncludeShapesInContent() is on.
Returns Vector<Target>: the objects inside. Use Target::is<T>() to tell them apart.
Vector<Shape> shapes() const;The shapes inside the trigger, one per body. Shape detection must be on.
Returns Vector<Shape>: the shapes.
Vector<Character> characters() const;The characters inside the trigger. Character detection must be on.
Returns Vector<Character>: the characters.
Vector<Harvestable> harvestables() const;The harvestables inside the trigger. Harvestable detection must be on.
Returns Vector<Harvestable>: the harvestables.
void setShapeDetection(bool on) const;Turns detection of shapes on or off. Needed for shapes().
Parameters
on(bool): true to detect shapes.
Lua: AreaTrigger:setShapeDetection
void setCharacterDetection(bool on) const;Turns detection of characters on or off. Needed for characters().
Parameters
on(bool): true to detect characters.
Lua: AreaTrigger:setCharacterDetection
void setHarvestableDetection(bool on) const;Turns detection of harvestables on or off. Needed for harvestables().
Parameters
on(bool): true to detect harvestables.
Lua: AreaTrigger:setHarvestableDetection
void setIncludeShapesInContent(bool on) const;Makes contents() list each shape inside the trigger, not just the bodies.
Parameters
on(bool): true to include shapes.
Lua: AreaTrigger:setIncludeShapesInContent
void destroy();Removes the trigger from the world now and frees its callbacks.
Lua: AreaTrigger:destroy
AreaTrigger &onEnter(atl::Function<void(Vector<Target>)> fn);Runs fn whenever objects enter the trigger.
Parameters
fn(atl::Function<void (Vector<Target>)>): called with the objects that entered since the last tick.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onExit(atl::Function<void(Vector<Target>)> fn);Runs fn whenever objects leave the trigger.
Parameters
fn(atl::Function<void (Vector<Target>)>): called with the objects that left since the last tick.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onStay(atl::Function<void(Vector<Target>)> fn);Runs fn every tick while objects are inside the trigger.
Parameters
fn(atl::Function<void (Vector<Target>)>): called with the objects inside.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onProjectile(atl::Function<void(const ProjectileHit &)> fn);Runs fn when a projectile hits the trigger.
Parameters
fn(atl::Function<void (const ProjectileHit &)>): called with the hit's details: position, damage, shooter and so on.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onMelee(atl::Function<void(const MeleeHit &)> fn);Runs fn when a melee attack, such as a sledgehammer swing, hits the trigger.
Parameters
fn(atl::Function<void (const MeleeHit &)>): called with the hit's details.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onInteract(atl::Function<void(Character, bool)> fn);Runs fn when a player presses or releases the interact key while looking at the trigger.
Parameters
fn(atl::Function<void (Character, bool)>): called with the player's character, and true on press or false on release.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &canInteract(atl::Function<bool(Character)> fn);Decides whether a player can interact with the trigger. Without it, every player can.
Parameters
fn(atl::Function<bool (Character)>): called with the player's character; return true to allow interacting.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onDestroy(atl::Function<void()> fn);Runs fn when the trigger is destroyed.
Parameters
fn(atl::Function<void ()>): called once, as the trigger goes away.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onHostDestroy(atl::Function<void()> fn);Runs fn when the object this trigger is attached to is destroyed.
Parameters
fn(atl::Function<void ()>): called once, when the host is destroyed.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onHostUnload(atl::Function<void()> fn);Runs fn when the object this trigger is attached to is unloaded, for example because no
player is near it any more.
Parameters
fn(atl::Function<void ()>): called once, when the host unloads.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &canErase(atl::Function<bool()> fn);[client] Decides whether a player may erase the trigger with the lift's erase tool.
Parameters
fn(atl::Function<bool ()>): called when a player tries to erase; return true to allow it.
Returns AreaTrigger &: this trigger, so calls can be chained.
AreaTrigger &onErase(atl::Function<void()> fn);[client] Runs fn when a player erases the trigger.
Parameters
fn(atl::Function<void ()>): called once the erase finishes.
Returns AreaTrigger &: this trigger, so calls can be chained.
void setEraseTime(double seconds) const;How long a player has to hold the erase tool on the trigger to erase it.
Parameters
seconds(double): the time, in seconds.
Lua: AreaTrigger:setEraseTime
void setDestroyOnErase(bool destroy) const;Whether erasing the trigger also destroys it.
Parameters
destroy(bool): true to destroy the trigger when it's erased.
Lua: AreaTrigger:setDestroyOnErase
void setWaterType(int liquidType) const;Sets which liquid a water trigger is. Only works on triggers created with the water proxy type. Lava occasionally destroys shapes in it, which needs setIncludeShapesInContent(true).
Parameters
liquidType(int): a value from sm.areaTrigger.liquidType, such as water or lava.
Lua: AreaTrigger:setWaterType
bool hasVoxelTerrainContact() const;Whether the trigger overlaps voxel terrain (the diggable ground).
Returns bool: true if it touches voxel terrain.
Lua: AreaTrigger:hasVoxelTerrainContact
void setVoxelDestructible(bool destructible) const;Lets the trigger dig away the voxel terrain it overlaps.
Parameters
destructible(bool): true to let it destroy voxel terrain.
Lua: AreaTrigger:setVoxelDestructible
sm::areaTrigger
Section titled "sm::areaTrigger"Creates area triggers. Every trigger is destroyed automatically with the script that created it.
inline int filter(StringView name);Looks up a detection filter by name, for Options::filter. Filters can be combined with |.
Parameters
name(StringView): a key of sm.areaTrigger.filter, such as "character", "dynamicBody" or "all".
Returns int: the filter's bit value.
Lua: sm.areaTrigger.filter
inline AreaTrigger created(Value v, const Options &o);Applies the detection options to a newly created trigger and tracks it. Used by the create functions.
Parameters
v(Value): the new Lua AreaTrigger.o(const Options &): the options it was created with.
Returns AreaTrigger: the tracked trigger.
inline AreaTrigger createBox(Vec3 size, Vec3 position, const Options &o = {});Creates a box-shaped trigger that stays where it's put.
Parameters
size(Vec3): the box's size along x, y and z, in meters.position(Vec3): where its center goes in the world.o(const Options &): rotation, filter, user data and detection options.
Returns AreaTrigger: the new trigger.
Lua: sm.areaTrigger.createBox
inline AreaTrigger createSphere(double radius, Vec3 position, const Options &o = {});Creates a sphere-shaped trigger that stays where it's put.
Parameters
radius(double): the sphere's radius, in meters.position(Vec3): where its center goes in the world.o(const Options &): rotation, filter, user data and detection options.
Returns AreaTrigger: the new trigger.
Lua: sm.areaTrigger.createSphere
inline AreaTrigger createCylinder(double radius, double halfLength, Vec3 position, const Options &o = {});Creates a cylinder-shaped trigger that stays where it's put. The cylinder runs along its local z axis.
Parameters
radius(double): the cylinder's radius, in meters.halfLength(double): half the cylinder's length, in meters.position(Vec3): where its center goes in the world.o(const Options &): rotation, filter, user data and detection options.
Returns AreaTrigger: the new trigger.
Lua: sm.areaTrigger.createCylinder
template <class Host> AreaTrigger attachedBox(const Host &host, Vec3 size, Vec3 offset = {}, const Options &o = {});Creates a box-shaped trigger that moves with a shape, character or harvestable.
Parameters
host(const Host &): the object to attach it to.size(Vec3): the box's size along x, y and z, in meters.offset(Vec3): the box's center relative to the host.o(const Options &): rotation, filter, user data and detection options.
Returns AreaTrigger: the new trigger.
Lua: sm.areaTrigger.createAttachedBox
template <class Host> AreaTrigger attachedSphere(const Host &host, double radius, Vec3 offset = {}, const Options &o = {});Creates a sphere-shaped trigger that moves with a shape, character or harvestable.
Parameters
host(const Host &): the object to attach it to.radius(double): the sphere's radius, in meters.offset(Vec3): the sphere's center relative to the host.o(const Options &): rotation, filter, user data and detection options.
Returns AreaTrigger: the new trigger.
Lua: sm.areaTrigger.createAttachedSphere
template <class Host> AreaTrigger attachedCylinder(const Host &host, double radius, double halfLength, Vec3 offset = {}, const Options &o = {});Creates a cylinder-shaped trigger that moves with a shape, character or harvestable.
Parameters
host(const Host &): the object to attach it to.radius(double): the cylinder's radius, in meters.halfLength(double): half the cylinder's length, in meters.offset(Vec3): the cylinder's center relative to the host.o(const Options &): rotation, filter, user data and detection options.
Returns AreaTrigger: the new trigger.
Lua: sm.areaTrigger.createAttachedCylinder
inline AreaTrigger createHull(StringView hull, Vec3 position, const Options &o = {}, const World &world = {}, Optional<int> proxyType = atl::none);Creates a trigger shaped like a mesh, such as a pool of water with an uneven bottom.
Parameters
hull(StringView): the path of the mesh that gives the shape.position(Vec3): where the mesh's origin goes in the world.o(const Options &): rotation, filter, user data and detection options.world(const World &): the world to create it in. Leave it empty for the current world.proxyType(Optional<int>): what the trigger acts as, from sm.areaTrigger.areaTriggerProxyType, for example water. Leave it out for a plain trigger.
Returns AreaTrigger: the new trigger.
Lua: sm.areaTrigger.createHull
template <class Host> AreaTrigger attachedHull(const Host &host, StringView hull, Vec3 offset = {}, const Options &o = {}, Optional<int> proxyType = atl::none, StringView bone = {});Creates a mesh-shaped trigger that moves with a shape, character or harvestable.
Parameters
host(const Host &): the object to attach it to.hull(StringView): the path of the mesh that gives the shape.offset(Vec3): the mesh's origin relative to the host.o(const Options &): rotation, filter, user data and detection options.proxyType(Optional<int>): what the trigger acts as, from sm.areaTrigger.areaTriggerProxyType.bone(StringView): the name of the host's bone to follow, for animated characters. Leave it empty to follow the host itself.
Returns AreaTrigger: the new trigger.
Lua: sm.areaTrigger.createAttachedHull
Options
Section titled "Options"Options for creating a trigger. Every field has a default, so {} gives an unrotated trigger
that detects characters, shapes and harvestables.
rotation: how the trigger is rotated.filter: which object types it can detect, from filter(). Leave empty for all.userData: any value to keep with the trigger, read back with AreaTrigger::userData().detectCharacters,detectShapes,detectHarvestables: which kinds of object it reports.
struct OptionsQuat rotation;Optional<int> filter;Value userData;bool detectCharacters = true;bool detectShapes = true;bool detectHarvestables = true;