LUA: "If" statement to identify if current player is touching something

When there's object-to-object collision, you usually want to handle it immediately through a collision hook, or one of the damage hooks if the object is hazardous. This way, if it's something you want to override and do something else, or make sure to run something *before* the game takes action, you can do so.
If you want to handle it at a later moment in time (why?), you can store who's the attacker in the victim object, and then make use of that value in some other hook, like so:
ideal way (untested):
addHook("MobjDamage", function(vct, inf, src)
	vct.iGotHit = {by=src, with=inf}
end)

addHook("PlayerThink", function(ply)
	local m = ply.mo
	if m then
		if m.iGotHit then
			local by,with = m.iGotHit.by, m.iGotHit.with
			m.iGotHit = nil
			
			-- do things with these two?
			ultrakill(with)
		end
	end
end)

Handling object collision with PlayerThink alone... well, there's no optimal way to handle that!
There's not much functionality that abstracts this, so most things you could do have to be manual.
A way I can think of doing this that wouldn't murder the game's performance would be using searchBlockmap to look around a subset of objects around the player (instead of ALL of the objects in the map. That's going to kill your CPU lmao), and then checking each to see if it intersects the player.
Sorta like this:
horrible way (untested):
-- TN: i use "pm" for "player mobj", and "fm" for "found mobj"
addHook("PlayerThink", function(ply)
	local pm = ply.mo
	if pm then
		
		searchBlockmap("objects", function(_, fm)
			-- this function will be called ONCE _PER_ OBJECT found within radius.
			-- searchBlockmap searches through the blockmap, meaning radius will snap to the nearest 128x128 coords.
			
			-- function *will* be called with objects that *may not* be intersecting.
			-- some manual checking is needed
			
			local xCollision = (pm.x + pm.radius > fm.x) and (fm.x + fm.radius >= pm.x)
			local yCollision = (pm.y + pm.radius > fm.y) and (fm.y + fm.radius >= pm.y)
			local zCollision = (pm.z + pm.height > fm.z) and (fm.z + fm.height >= pm.z)
			if xCollision and yCollision and zCollision then
				-- colliding!
				-- do stuff
				ultrakill(fm)
			end
			
		end, pm)
		
	end
end)
But even with this, I certainly do not recommend it. There are better ways available that don't involve this much yapping which can be simplified with a single collision hook, as proven above. :shitsfree:
 
Thanks man! Will test it. The idea was, inside of the playerthink hook, a specific mobj should be spawned, and I already have the code for that, but it should only spawn if the player is touching something hazardous.
 

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

Back
Top