Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement entity:setTriggerListener #2012

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lua/starfall/libs_sv/entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,43 @@ function ents_methods:setPhysicsUpdateListener(func)
end
end

--- Marks an entity as a trigger, setting a callback function to run whenever other entities enter or leave the bounds of the first.
--- The entity will still invoke the callback even if not solid and no physical collision occurs, unlike Entity:addCollisionListener.
--- The callback function arguments are: (Entity object, bool entering) (entering is true if the object is entering the bounds, false if leaving)
--- https://developer.valvesoftware.com/wiki/Triggers
---
--- You can only use this function on these classes:
--- - starfall_prop
--- - starfall_processor
-- @param function|nil func The callback function. Use nil to remove an existing callback.
function ents_methods:setTriggerListener(func)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to have two function input args for startTouch and endTouch instead of using a boolean. Code looks fine, but I will test it soon.

local ent = getent(self)
checkpermission(instance, ent, "entities.canTool")

local class = Ent_GetClass(ent)
if not physUpdateWhitelist[class] then SF.Throw("Cannot use trigger listener on " .. class, 2) end

local entTable = Ent_GetTable(ent)
if func then
checkluatype(func, TYPE_FUNCTION)

ent:SetTrigger(true)

entTable.StartTouch = function(_, touchingEnt)
instance:runFunction(func, owrap(touchingEnt), true)
end

entTable.EndTouch = function(_, touchingEnt)
instance:runFunction(func, owrap(touchingEnt), false)
end
else
ent:SetTrigger(false)

entTable.StartTouch = nil
entTable.EndTouch = nil
end
end

--- Returns a copy of the entity's sanitized internal glua table.
-- @return table The entity's table.
function ents_methods:getTable()
Expand Down