how to check if player is touched by and object?

This how I usually do it. Anything behind -- is a comment, something the game won't try to read or execute.

Feel free to copy-paste and then edit.

Example Lua:
local function playercollide(mo, ring)
    --First, we check if both object exists and the ring box is actually the ring box we're looking for!

    if mo.valid and ring and ring.valid and (ring.type == MT_RING_BOX)
    
    --Next...the mobjcollide functions do NOT check for vertical hitspace,
    --So we need to manually check if we're vertically alligned.
    
        if (mo.z > ring.z+ring.height)
        or (ring.z > mo.z+mo.height)
        return --Return means canceling the function in this case, because we're not vertically alligned.
        end

        --It didn't return...? Then congrats, you touched the box! Time for your custom action.

        mo.insertacustomactionhere = true
        return true

        --Now, one more thing. If you do "return true", it forces a "colission".
        --If you do "return false", you phase through it, but it will still execute whatever your custom action is.
        --If you just write "return" or "return nil", the game will decide for you.
    end
end

--To eliminate any guesswork whether we need MobjMoveCollide or MobjCollide
--I like to just make both hooks that refer to a local function.

--If you're an optimization nerd, use the appropriate hooks.
--Another optimization is to give the MobjCollide hook to MT_RING_BOX rather than MT_PLAYER
--assuming that's the ONLY object you need custom Lua stuff for.
--Otherwise we have to run this hook everytime a player touches anything, which'll be often.
--As a beginner, though, focus on getting stuff done rather than being a perfectionist.

addHook("MobjCollide", playercollide, MT_PLAYER)
addHook("MobjMoveCollide", playercollide, MT_PLAYER)
 

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top