Hi folks!
I found Defold last week, and have been going through the manuals and finished the runner tutorial over the weekend.
I’m doing a basic asteroids clone to familiarize myself with the engine, and I’d like to find out if I’m doing things correct. Please find a snippet of my player control code below:
function init(self)
self.maxVel = 5;
self.accel = 2;
self.drag = 0.98;
self.velocity = vmath.vector3()
msg.post('.', "acquire_input_focus")
self.lastDt = 0;
end
function update(self, dt)
self.lastDt = dt;
go.set_position(go.get_position('.')+self.velocity)
slowdown(self)
end
function slowdown(self)
self.velocity = self.velocity * self.drag;
end
function on_input(self, action_id, action)
if action_id == hash("left") then
local currVel = self.velocity;
currVel = currVel + vmath.vector3(-1 * self.accel,0,0) * self.lastDt;
self.velocity = currVel;
elseif action_id == hash("right") then
local currVel = self.velocity;
currVel = currVel + vmath.vector3(1 * self.accel,0,0) * self.lastDt;
self.velocity = currVel;
end
end
I’m using the on_input function to calculate my velocity. I don’t think there’s a global dt (is there?) so I’m using self.lastDt to capture the last deltaTime.
There’s two things I’m not sure about a) Is this the correct way to utilize dt, and b) is this the proper way to capture multiple inputs?
As it stands, the code works, but how does it recognize multiple keys being held down? Does it send them on multiple on_input calls per frame?
Here is my take on it (trying to be as close to your original code as possible)
function init(self)
self.maxVel = 5
self.accel = 2
self.drag = 0.98
self.velocity = vmath.vector3()
self.input = vmath.vector3()
self.pos = go.get_position()
msg.post('.', "acquire_input_focus")
end
function update(self, dt)
self.velocity = self.velocity + self.input * self.accel * dt
self.pos = self.pos + self.velocity
go.set_position(self.pos)
slowdown(self)
self.input = vmath.vector3() -- reset input for next frame
end
function slowdown(self)
self.velocity = self.velocity * self.drag;
end
function on_input(self, action_id, action)
if action_id == hash("left") then
self.input.x = self.input.x - 1
elseif action_id == hash("right") then
self.input.x = self.input.x + 1
end
end
Instead of dealing with the “physics” in on_input I would just store the inputs in a vector (which will deal with multiple keypresses to be zeroed out). EDIT: Yes on_input is being called for each keypress or other input that is taking place.
Then in update (which is triggered after all on_input) we deal with all that physics.
Pointer 1: Storing your position in pos will be a little more efficient as go.get_position() takes more time.
Pointer 2: input vector must be resetted in the end of update so it doesnt accumulate it’s value.
Pointer 3:This hasnt been tried at all hope it works
Hey andreas.strangequest!
Thanks for the code, I’m going to try it shortly, never thought of it that way! I need to re-think my workflows from unity XD
Do you happen to know if the on_input gets called multiple times per frame if I hold down two or more buttons, though?
Yes, you will get one call per input source that you’ve mapped in your input bindings (so one for every key that changes it’s state, for every mouse and game pad and touch and so on).