Quick question- I saw a couple times in Christopho's let's play of my game, he accidentally pressed the ESC key and quit the game without saving. This is the default behavior of Solarus, but how would I change that so nobody else accidentally quits and loses progress?
I tried this:
function game:on_ley_pressed(key, modifiers)
if key == "esc" then (some random code that's not quitting the game) end
end
But the game still just quits when the esc key is pressed. It's nice when I'm testing things quickly but dangerous if you're actually playing.
There is a typo in the name of your event. (Also, if you define your function like that, make sure that you are not overriding other function in other script, which could break some code.)
Oops, I retyped it on the forum, there isn't a typo in my real code. It's the same function that handles the map, which works fine, so that's not the issue. But it should override the quit function if it just plays a sound effect or something?
Your problem is from the main.lua line 49 to 53. Change the behavior to something else or comment out line 51.
-- This is the main Lua script of your project.
-- You will probably make a title screen and then start a game.
-- See the Lua API! http://www.solarus-games.org/doc/latest
require("scripts/features")
local game_manager = require("scripts/game_manager")
-- This function is called when Solarus starts.
function sol.main:on_started()
--preload the sounds for faster access
sol.audio.preload_sounds()
--set the language
sol.language.set_language("en")
--[[local solarus_logo = require("scripts/menus/solarus_logo")
-- Show the Solarus logo initially.
sol.menu.start(self, solarus_logo)
-- Start the game when the Solarus logo menu is finished.
solarus_logo.on_finished = function()
end
--]]
game_manager:start_game("save1.dat")
end
-- Event called when the player pressed a keyboard key.
function sol.main:on_key_pressed(key, modifiers)
local handled = false
if key == "f5" then
-- F5: change the video mode.
sol.video.switch_mode()
handled = true
elseif key == "f11" or
(key == "return" and (modifiers.alt or modifiers.control)) then
-- F11 or Ctrl + return or Alt + Return: switch fullscreen.
sol.video.set_fullscreen(not sol.video.is_fullscreen())
handled = true
elseif key == "f4" and modifiers.alt then
-- Alt + F4: stop the program.
sol.main.exit()
handled = true
elseif key == "escape" and sol.main.game == nil then
-- Escape in title screens: stop the program.
sol.main.exit()
handled = true
end
return handled
end
Thanks, llamazing! This works great.