On_input not working (SOLVED)

( 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.

Check input section in your game.project file.

Game binding is connected to the correct .input_binding file. If that’s what you mean.

The script is good. The collision object of your player.go should be kinematic, and not static. Is this okay too ?

Currently I’m just trying to get used to the engine more than anything. I’m doing this by going through the tutorials of which the War battles tutorial seems to be the first, so I’m doing that as my first project. I’ll move things to a better system when I’m familiar with Defold. The problem is that the on_input function doesn’t even call ( though even if I wanted to use kinematic or dynamic movement I don’t know how to in Defold yet ).
Thank you though.

I think the first tutorial is the side-scroller one, you can find it here :

Hope you will come back !

Can you make another empty game object with a script and on_input function?
And add mouse triggers in the input bindings, maybe they will get through.
Also could you post a screenshot of your input bindings?

How queer; I made a new GO with a new script that just moves itself if the left mouse button is pressed and it works… is the problem something with my keyboard?

Here’s my input bindings at the moment:

Strange. Do you have another keyboard?

Not one I can access any time soon, what’s the idea?
I’m on the most recent keyboard drivers update and I also tried using the on-screen keyboard.

So this is strange, I opened up the Side scroller tutorial and hit build; the controls work…
I checked the key bindings, script and .project but their pretty much the same thing as the ones in the other project.

Can you share the whole project?

Here: War battles tutorial.zip (2.0 MB)

Tell me if there’s a better way of posting Defold projects (I removed all assets I’m not using at the moment).

At the bottom of player.script you’ve got an extra on_input function which is empty - this will override the one with actual code in it. If you just delete the empty one, your code should work.

8 Likes

Wow… I can’t believe I didn’t see that…
Thank you, sorry for my incompetence.

1 Like