Jump to content

PlanarEntity component failing to find attackers position.


Scrimbles
  • Pending

I've had a bug report sent to me that seems to indicate that if something attacks a 'PlanarEntity' and immediately removes itself for any reason, the game will crash.

 

[string "scripts/components/planarentity.lua"]:21: attempt to perform arithmetic on local 'z1' (a nil value)
LUA ERROR stack traceback:
scripts/components/planarentity.lua:21 in (method) OnResistNonPlanarAttack (Lua) <13-32>
function PlanarEntity:OnResistNonPlanarAttack(attacker)
	local fx = SpawnPrefab("planar_resist_fx")
	local radius = self.inst:GetPhysicsRadius(0) + .2 + math.random() * .5
	local x, y, z = self.inst.Transform:GetWorldPosition()
	local theta
	if attacker ~= nil then
		local x1, y1, z1 = attacker.Transform:GetWorldPosition()
		if x ~= x1 or z ~= z1 then -- |||| x1 AND OR z1 ARE BEING RETURNED AS NIL
			theta = math.atan2(z - z1, x1 - x) + math.random() * 2 - 1
		end
	end
	if theta == nil then
		theta = math.random() * TWOPI
	end
	fx.Transform:SetPosition(
		x + radius * math.cos(theta),
		math.random(),
		z - radius * math.sin(theta)
	)
end


Whats odd about this is that you would think doing a nil check for the attacker would prevent this, but, y'know, #JustCodingThings


Steps to Reproduce

Specifically happens with a mod related feature, however, if any entity was attacked and removed within a frame or so, the same thing could happen.




User Feedback


Quote

Whats odd about this is that you would think doing a nil check for the attacker would prevent this, but, y'know, #JustCodingThings

@ScrimblesWhen an entity is removed, it's 'EntityScript' wrapper will still exist if there are still references to it(which is what 'attacker' is in this case).

You'd want to call it's 'IsValid' function to see if the actual entity in the engine has been retired or not.

Share this comment


Link to comment
Share on other sites

23 hours ago, Hornete said:

@ScrimblesWhen an entity is removed, it's 'EntityScript' wrapper will still exist if there are still references to it(which is what 'attacker' is in this case).

You'd want to call it's 'IsValid' function to see if the actual entity in the engine has been retired or not.

Well, Klei would, atleast.

Share this comment


Link to comment
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
  • Create New...