Can input actions in defold not have the same name? (DEF-1855)

Hello all,

I’m new to defold but it looks like an awesome engine. I am watching all of the live coding sessions form setthebet and it’s cool to see someone make prototypes live and uncut and really showcases the power of the engine.

I was working through a quick prototype, and I found that my input wasn’t working. I had a touch, key, and mouse input setup all called player_action. I then tried renaming each to have a unique name, and the input started working fine. I read through the input manual, and I didn’t see in the manual where it said that inputs need a unique name. This is what I came up with:

local msg_player_action = {}

msg_player_action[hash('key_player_action')] = ''
msg_player_action[hash('mouse_player_action')] = ''
msg_player_action[hash('touch_player_action')] = ''

function init(self)
    msg.post(".", "acquire_input_focus")
end

function final(self)
    msg.post(".", "release_input_focus")
end

function on_input(self, action_id, action)
    if msg_player_action[action_id] and action.pressed then
	print('We got some actions fo real!!!')
    end
end
1 Like

You can have multiple input actions with the same name, I’m not sure why you didn’t get it to work.

It is worth mentioning that your example will break in a release build since you can’t use hash as a table key. You could use hash_to_hex() first to convert the hash to a string, but it will impact performance somewhat if it’s done many times per frame. I use this approach in the input util module I created for my LowRezInvaders game.

1 Like

Hey britzl, thanks for the response

I just tested again and I definitely can’t seem to get it working with same name input action. Does this setup look right?

function on_input(self, action_id, action)
    if hash("player_action") then
    	if action.pressed then
    	    flying = true
    	elseif action.released then
            flying = false
        end
    end
end

Thanks for the hash_to_hex tip!

I just did a test and multiple actions with the same name works, except for touch triggers. Added bug DEF-1855

3 Likes