Hello folks,
Defold newbie here: I’m trying to compare Vector3s, and for some reason am failing at it, as you can see from the following debug:
DEBUG:SCRIPT: key vmath.vector3(0, -1, 0) equals vmath.vector3(0, -1, 0) ? false
DEBUG:SCRIPT: key vmath.vector3(0, 1, 0) equals vmath.vector3(0, -1, 0) ? false
DEBUG:SCRIPT: key vmath.vector3(-1, 0, 0) equals vmath.vector3(0, -1, 0) ? false
DEBUG:SCRIPT: key vmath.vector3(1, 0, 0) equals vmath.vector3(0, -1, 0) ? false
DEBUG:SCRIPT: key vmath.vector3(-1, -1, 0) equals vmath.vector3(0, -1, 0) ? false
DEBUG:SCRIPT: key vmath.vector3(1, 1, 0) equals vmath.vector3(0, -1, 0) ? false
DEBUG:SCRIPT: key vmath.vector3(1, -1, 0) equals vmath.vector3(0, -1, 0) ? false
DEBUG:SCRIPT: key vmath.vector3(-1, 1, 0) equals vmath.vector3(0, -1, 0) ? false
DEBUG:SCRIPT: updated the animation for input: vmath.vector3(0, -1, 0) => nil
It seems to me the very first line should read “true” instead of “false”, what am I missing?
Here’s the code (I’m trying to select the correct sprite animation to play given the character’s direction):
function init(self)
-- some other initialisation
self.input = vmath.vector3()
-- some other initialisation
end
local function update_animation(input)
local animations = {
[vmath.vector3(-1, -1, 0)] = "player-down-left",
[vmath.vector3(-1, 1, 0)] = "player-up-left",
[vmath.vector3(1, -1, 0)] = "player-down-right",
[vmath.vector3(1, 1, 0)] = "player-up-right",
[vmath.vector3(1, 0, 0)] = "player-right",
[vmath.vector3(-1, 0, 0)] = "player-left",
[vmath.vector3(0, 1, 0)] = "player-up",
[vmath.vector3(0, -1, 0)] = "player-down",
}
for key, value in pairs(animations) do
print("key", key, "equals", input, "?", value == input)
end
print("updated the animation for input:", input, " => ", animations[input])
return animations[input]
end
function on_input(self, action_id, action)
local previous_input = vmath.vector3(self.input) -- clone self.input
if action_id == hash("up") then
self.input.y = 1
elseif action_id == hash("down") then
self.input.y = -1
elseif action_id == hash("left") then
self.input.x = -1
elseif action_id == hash("right") then
self.input.x = 1
elseif action_id == hash("fire") and action.pressed then
self.firing = true
end
if self.input ~= previous_input then
self.animation = update_animation(vmath.vector3(self.input))
end
end
I’m sure I’m missing something obvious, but I couldn’t find what, even after going through the documentation on tables and constructors, and it seems from my google searches that Vector3s should be comparable?