quick question about some entity code

Post Reply
fessmK
Member
Posts: 49
Joined: Wed Sep 25, 2013 15:56

quick question about some entity code

by fessmK » Post

Hello all, long time no see.
I ran into an interesting problem while running an experimental entity.
error:

Code: Select all

2017-12-06 15:09:01: ACTION[Server]: singleplayer invokes /spawnentity, entityname="test:basic_mob"
2017-12-06 15:09:01: [Server]: 2
2017-12-06 15:09:01: ERROR[Main]: ServerError: AsyncErr: ServerThread::run Lua: Runtime error from mod 'test' in callback luaentity_Step(): Invalid float vector dimension range 'x' (expected -2.14748e+006 < x < 2.14748e+006 got nan).
2017-12-06 15:09:01: ERROR[Main]: stack traceback:
2017-12-06 15:09:01: ERROR[Main]: 	[C]: in function 'setvelocity'
2017-12-06 15:09:01: ERROR[Main]: 	...test-0.4.16-win64\bin\..\games\temple\mods\test\init.lua:50: in function 'set_velocity'
2017-12-06 15:09:01: ERROR[Main]: 	...test-0.4.16-win64\bin\..\games\temple\mods\test\init.lua:126: in function 'func'
2017-12-06 15:09:01: ERROR[Main]: 	...0.4.16-win64\bin\..\builtin\profiler\instrumentation.lua:106: in function <...0.4.16-win64\bin\..\builtin\profiler\instrumentation.lua:100>
here is the function:

Code: Select all

set_velocity = function(self, v)
		
		local yaw = self.object:getyaw()
		if self.drawtype == "side" then
			yaw = yaw+(math.pi/2)
		end
		local x = math.sin(yaw) * -v
		local z = math.cos(yaw) * v
		self.object:setvelocity({x=x, y=self.object:getvelocity().y, z=z}) --sometimes crashes because a NaN is passed to x
	end,
and the parameters when called:(only called in on_step(dtime))

Code: Select all

minetest.debug(self.run_velocity) --reason for all those 2s
self.set_velocity(self, self.run_velocity)

--and

self.set_velocity(self, 0)
I tried checking to see if the variable x, and z, were numbers, but that always came back as true.


this experimental code was based on PlzAdam's Mobs mod

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: quick question about some entity code

by Krock » Post

NaN is represented in the 64-bit IEEE floating point number, which Lua uses by default for numbers. So it reasonable that checking this against being the number type succeeds every time.

More interesting would be where "self.run_velocity" comes from, as "math.sin(0/0)" (passing NaN as function argument) returns NaN, which gets then passed to "self.object:setvelocity".

PS: "setvelocity" and "getyaw" (and similar) are deprecated. Use "set_velocity" and "get_yaw" instead.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

fessmK
Member
Posts: 49
Joined: Wed Sep 25, 2013 15:56

Re: quick question about some entity code

by fessmK » Post

Krock wrote: More interesting would be where "self.run_velocity" comes from
it is set to 2

Code: Select all

run_velocity = 2,
edit: it is probably worth mentioning that, after the initial spawn with /spawnentity and the resulting crash, it runs just fine. Also, using an item to spawn the entity does not result in a crash.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests