I'm new and I don't know why it does not works

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


function on_input(self, action_id, action)
	if action_id == hash("move_left") and action.pressed then
		repeat
				local pos = go.get_position()
				pos.x = pos.x - 10
				go.set_position(pos)
				local pos = go.get_position()
			until cond action.released
		end
	elseif action_id == hash("move_right") and action.pressed then
		local pos = go.get_position()
		pos.x = pos.x + 10
		go.set_position(pos)
	end
end

Can someone help me?

Don’t you get errors in the console with this? That is the first place you should look when thinks don’t work. 'cond' is not a keyword in Lua, that will cause your build to fail. It also looks like you are making an infinite loop with that repeat...until block, just remove that. action.pressed and action.released will not change within a single input event. Each time on_input is called, it’s a single event, not a continuous thing.

Side note: If you add three backticks ( ` ) before and after you copy your code into your forum post, it will be formatted nicely.

2 Likes

Thank you! But can you say me what i have to do if i want that the player only have to hold the butten?

I am guessing you have some background in other coding? Hard to break those habits sometimes. :rofl:

The LUA can be much simpler. Maybe something like this will work?

function init(self)
msg.post(".", “acquire_input_focus”)
self.pos = go.get_position()
end

function on_input(self, action_id, action)
  if action_id == hash(“move_left”) then
    self.pos.x = self.pos.x - 10
    go.set_position(self.pos)

  elseif action_id == hash(“move_right”) then
    self.pos.x = self.pos.x + 10
    go.set_position(self.pos)
  end
end
1 Like

Thank you! It works really nice, but the problem is that I have to press the butten again and again and I can’t hold it down.

“action.pressed” is only true for the first frame the key is pressed.

If you want it to trigger every frame you only need to remove the and action.pressed

1 Like

Thank you