[Solved] Defold platformer. Restarting and exiting levels by pressing single buttons?

So I am having problems with trying to implement a restart and exit features to happen in my levels binded to 2 different keys.

For the restart I want to say press “R” and then for my character to begin at the beginning of the level again.

For the exit I want to say press the “esc” key and for the game to return to the main menu.

Any suggestions on how I could do this please? Any help appreciated!

You need to start by setting up two input bindings in your .input_binding file, one for the R key and you can bind that to the action restart and the other for the ESC key and bound to main_menu.

Next you check for these in on_input():

function on_input(self, action_id, action)
	if action_id == hash("restart") and action.pressed then
		-- run code to restart level
	elseif action_id == hash("main_menu") and action.pressed then
		-- run code to load and show main menu
	end
end

Thank you. This is exactly what I needed but luckily I figured it out :slightly_smiling_face:

1 Like