minetest-mods / castle_gates

Large swinging and sliding gate components suitable for a castle
MIT License
9 stars 8 forks source link

attempt to perform arithmetic on field 'z' (a nil value) #7

Closed ghost closed 6 years ago

ghost commented 6 years ago

@FaceDeer thank you for restoring this mod, I really appreciate your efforts to fix these issues. :)

I've found another, this time related to the gate's swing. I've built this drawbridge:

screenshot

Everything works as expected if you click on the hinged panels (i.e. the drawbridge lifts and stops against the tower's wall) but if you click on any of the other panels (castle_gates:gate_panel) the game crashes dumping this error:

--- LOG START

ERROR[Main]: ServerError: AsyncErr: ServerThread::run Lua: Runtime error from mod 'default' in callback item_OnPlace(): ...chi/minetest-stable-0.4/bin/../builtin/common/vector.lua:68: attempt to perform arithmetic on field 'z' (a nil value)

ERROR[Main]: stack traceback:

ERROR[Main]: ...chi/minetest-stable-0.4/bin/../builtin/common/vector.lua:68: in function 'direction'

ERROR[Main]: ...games/hamlets_quest/mods/castle_gates/gate_functions.lua:258: in function 'get_door_layout'

ERROR[Main]: ...games/hamlets_quest/mods/castle_gates/gate_functions.lua:320: in function 'on_rightclick'

ERROR[Main]: .../Giochi/minetest-stable-0.4/bin/../builtin/game/item.lua:347: in function <.../Giochi/minetest-stable-0.4/bin/../builtin/game/item.lua:340>

--- LOG END

It seems to have the same issue with field Y, I don't know about others.

FaceDeer commented 6 years ago

Neat. I initially wasn't able to reproduce this, but it turns out the key was rotating the gate panels to match yours exactly - when the gate panels are rotated so that the grain runs lengthwise (matching the hinge panels) there's no crash.

I think I see why the crash is happening, but the code it's in is very gnarly. May take a bit of work.

FaceDeer commented 6 years ago

Okay, I think I got it.

The problem was that the code was building an understanding of the door that included the axis of rotation of the hinge, the "back" directional vector, and the "right" directional vector. But it was getting the "back" and "right" directional vectors from the node that was clicked on, not the hinge, so if you rotated the clicked-on gate node differently from the hinge node it could get a "right" vector that pointed in a direction I didn't account for in my code. I guess when I tested this stuff I always had the gate's nodes rotated to match the hinge's nodes. Curse my instinct for tidiness! :)

I've changed the code so that if the gate has a hinge in it the hinge's back and right vectors will override the ones from the clicked-on node, and everything seems to be working correctly regardless of the orientation of the gate node I click on.