( Apologies if this is a bad question or if it’s already been answered/asked but I’ve looked at all posts that looked remotely similar and can’t find an answer that worked with my problem, so after an hour of trying to solve the problem I decided to ask here. )
I am new to Defold and am going through the War battles tutorial, however after “Program the player movement” section and the script having just been fully typed, nothing happens. print
works in init
and update
but does nothing in on_input
.
P.S. there are no errors printed to the console.
Things I’ve tried:
– There is msg.post(".", "acquire_input_focus")
in init.
– The script is on the game object.
– The main collection and game bindings are both hooked up correctly ( I didn’t change anything ).
– All the input bindings/events are there in the \input\game.input_bindings
file.
If this is relevant: I am on a Windows 10, 64-bit
This is my code:
function init(self)
msg.post(".", "acquire_input_focus")
self.moving = false
self.input = vmath.vector3()
self.dir = vmath.vector3(0,1,0)
self.speed = 50
end
function final(self)
msg.post(".", "release_input_focus")
end
function update(self, _dt)
print('in update')
if self.moving then
local pos = go.get_position()
pos = pos + (self.dir * self.speed * _dt)
go.set_position(pos)
end
self.input.x = 0
self.input.y = 0
self.moving = false
end
function on_input(self, action_id, action)
print('in 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
end
-- because the input doesn't work, I changed it back to the longer one above because that's what the tutorial uses
--self.input.y = (action_id==hash("up") and 1 or 0) - (action_id==hash("down") and 1 or 0)
--self.input.x = (action_id==hash("right") and 1 or 0) - (action_id==hash("left") and 1 or 0)
if vmath.length(self.input) > 0 then
self.moving = true
self.dir = vmath.normalize(self.input)
end
end
Thank you for your time.