blob: 4d3af26d84433237d848e3d09448e8a2bf1f82a5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# Common Issues and Solutions
## ESC Key Closes the Game
### Problem
By default, RayLib sets ESC as the exit key. When you press ESC, `WindowShouldClose()` returns true and the game closes immediately, even if you want to use ESC for pause menus or other game functionality.
### Solution
Disable the default ESC exit key behavior by calling `RL.SetExitKey(0)` in your `RL.init()` function:
```lua
function RL.init()
-- Window setup
RL.SetWindowTitle("My Game")
-- Disable ESC key from closing the window
RL.SetExitKey(0) -- 0 = KEY_NULL (no exit key)
-- OR set a different exit key
-- RL.SetExitKey(RL.KEY_F12) -- Use F12 instead
-- Rest of initialization...
end
```
### Options
**Option 1: No Exit Key (Recommended for Games)**
```lua
RL.SetExitKey(0) -- Disable exit key completely
```
Now ESC (and any key) won't close the game. Handle exit through:
- Menu option (File → Exit)
- Alt+F4 (Windows default)
- Close window button (X)
**Option 2: Different Exit Key**
```lua
RL.SetExitKey(RL.KEY_F12) -- Use F12 to exit
```
Now only F12 will close the game, ESC is free for pause menus.
**Option 3: Custom Exit Handling**
```lua
-- In RL.update()
if RL.IsKeyPressed(RL.KEY_ESCAPE) then
-- Show pause menu or confirmation dialog
GameState.push(pauseMenu)
end
if RL.IsKeyPressed(RL.KEY_F10) then
-- Exit game
RL.CloseWindow()
end
```
### Technical Details
**What happens by default:**
1. RayLib sets ESC as the default exit key
2. When ESC is pressed, `WindowShouldClose()` returns true
3. Main loop in `src/main.c` checks this and exits
**After calling `RL.SetExitKey(0)`:**
1. No key triggers `WindowShouldClose()`
2. ESC is now available for your game logic
3. You control when the game exits
### Example: Pause Menu with ESC
```lua
-- In your game state:
function GameState:update(dt)
-- Pause with ESC
if RL.IsKeyPressed(RL.KEY_ESCAPE) then
if self.paused then
self.paused = false
else
self.paused = true
end
end
if not self.paused then
-- Update game logic
end
end
```
### Template Fix
The template's `main.lua` has been updated with:
```lua
RL.SetExitKey(0) -- Disable ESC exit key
```
This allows the game state to use ESC for pause functionality without closing the game.
### Related Functions
- `RL.SetExitKey(key)` - Set which key exits the game
- `RL.WindowShouldClose()` - Check if game should close
- `RL.CloseWindow()` - Manually close the window
- `RL.IsKeyPressed(key)` - Check if key was pressed this frame
### Development Tip
During development, you might want quick exit. Consider:
```lua
function RL.init()
RL.SetExitKey(0) -- Disable ESC
-- But add debug exit key
if DEBUG_MODE then
RL.SetExitKey(RL.KEY_F12)
end
end
```
Or handle it in update:
```lua
function RL.update(dt)
-- Debug: Quick exit with Shift+ESC
if RL.IsKeyDown(RL.KEY_LEFT_SHIFT) and RL.IsKeyPressed(RL.KEY_ESCAPE) then
RL.CloseWindow()
end
end
```
## Other Common Issues
### Window Closes When X Button Clicked
This is normal behavior. If you want to show a confirmation dialog:
```lua
function RL.update(dt)
if RL.WindowShouldClose() then
-- Show "Are you sure?" dialog
-- If player cancels, need to prevent close somehow
end
end
```
Note: Preventing X button close is tricky with RayLib's current API.
### Alt+F4 Closes Game
This is OS-level behavior and cannot be easily disabled. It's recommended to save game state frequently.
---
**Updated**: 2025-11-05
**Template Version**: 1.0
|