Author Topic: The dangerous escape key  (Read 242 times)

Max

  • Full Member
  • ***
  • Posts: 137
    • View Profile
The dangerous escape key
« on: April 28, 2018, 09:38:44 pm »
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:
Code: Lua
  1. function game:on_ley_pressed(key, modifiers)
  2.   if key == "esc" then (some random code that's not quitting the game) end
  3. end
  4.  

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.

Diarandor

  • Hero Member
  • *****
  • Posts: 979
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: The dangerous escape key
« Reply #1 on: April 28, 2018, 11:21:37 pm »
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.)
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Max

  • Full Member
  • ***
  • Posts: 137
    • View Profile
Re: The dangerous escape key
« Reply #2 on: April 29, 2018, 02:26:20 am »
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?

llamazing

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: The dangerous escape key
« Reply #3 on: April 29, 2018, 05:27:46 am »
Your problem is from the main.lua line 49 to 53. Change the behavior to something else or comment out line 51.

Code: Lua
  1. -- This is the main Lua script of your project.
  2. -- You will probably make a title screen and then start a game.
  3. -- See the Lua API! http://www.solarus-games.org/doc/latest
  4.  
  5. require("scripts/features")
  6. local game_manager = require("scripts/game_manager")
  7.  
  8. -- This function is called when Solarus starts.
  9. function sol.main:on_started()
  10.   --preload the sounds for faster access
  11.   sol.audio.preload_sounds()
  12.   --set the language
  13.   sol.language.set_language("en")
  14.  
  15.  
  16. --[[local solarus_logo = require("scripts/menus/solarus_logo")
  17.  
  18.   -- Show the Solarus logo initially.
  19.   sol.menu.start(self, solarus_logo)
  20.  
  21.   -- Start the game when the Solarus logo menu is finished.
  22.   solarus_logo.on_finished = function()
  23.  
  24.   end
  25. --]]
  26.  
  27.     game_manager:start_game("save1.dat")
  28. end
  29.  
  30.  
  31.  
  32. -- Event called when the player pressed a keyboard key.
  33. function sol.main:on_key_pressed(key, modifiers)
  34.  
  35.   local handled = false
  36.   if key == "f5" then
  37.     -- F5: change the video mode.
  38.     sol.video.switch_mode()
  39.     handled = true
  40.   elseif key == "f11" or
  41.     (key == "return" and (modifiers.alt or modifiers.control)) then
  42.     -- F11 or Ctrl + return or Alt + Return: switch fullscreen.
  43.     sol.video.set_fullscreen(not sol.video.is_fullscreen())
  44.     handled = true
  45.   elseif key == "f4" and modifiers.alt then
  46.     -- Alt + F4: stop the program.
  47.     sol.main.exit()
  48.     handled = true
  49.   elseif key == "escape" and sol.main.game == nil then
  50.     -- Escape in title screens: stop the program.
  51.     sol.main.exit()
  52.     handled = true
  53.   end
  54.  
  55.   return handled
  56. end
« Last Edit: April 29, 2018, 05:30:12 am by llamazing »

Max

  • Full Member
  • ***
  • Posts: 137
    • View Profile
Re: The dangerous escape key
« Reply #4 on: May 02, 2018, 06:04:37 pm »
Thanks, llamazing! This works great.