Skip to content

Commit b702cb0

Browse files
Clean up event system docs (#29)
* Clean up event system docs * Event Manager Tweaks * 'other' queue is unused * Clarify before events * Add a bit more information on custom queues
1 parent a1a8e56 commit b702cb0

File tree

1 file changed

+78
-39
lines changed

1 file changed

+78
-39
lines changed

Guide-‐-Event-Manager.md

Lines changed: 78 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,45 @@ However, you probably want to do something in this event. Let's cover some of th
1515
These can be passed in a table to the Event function.
1616

1717
trigger - string:
18-
- "immediate" (default) - Runs as soon as possible
19-
- "after" - Will run after a set amount of time. Also see delay
20-
- "condition" - Will not finish until the condition is met. For most cases, you probably just want to use immediate with a check (see func). See ref_table, ref_value and stop_val for how to set this.
21-
- "ease" - Used to interpolate values. Useful for score incrementing or movement. Also see ref_table, ref_value and ease_to for how to set this.
22-
- "before" - Will run immediately, but if it doesn't complete before the set amount of time, it will be cancelled. Also see delay
18+
- `"immediate"` (default) - Runs as soon as possible
19+
- `"after"` - Runs after a set amount of time specified in `delay`
20+
- `"before"` - Runs as soon as possible. Events after this one wait for both `func` and `delay`, whichever takes longer to finish.
21+
- `"condition"` - Waits until the condition is met (namely, when `config.ref_table[config.ref_value] == config.stop_val`). You probably want to use `immediate` instead.
22+
- `"ease"` - Used to interpolate values. Useful for score incrementing or movement. Acts something like `config.ref_table[config.ref_value] = config.func(ease_function(original_value, config.ease_to, config.duration))`
2323

2424
blocking - boolean: Whether or not this event may block other events. Default is true.
2525

2626
blockable - boolean: Whether or not this event may be blocked by other events. Default is true.
2727

28-
func - function: The function to call for the event. When this is called depends on the trigger type.
29-
This function takes no arguments.
30-
It returns whether it completed. If true, then the event is over, if false, then it will be called again next time the event manager processes events (the next frame).
28+
func - function: The body of the event. This is where you will perform any actions or custom delays. Once the event starts, this will be run once per frame until it returns true. This can be omitted without consequence.
3129
Its behavior for each trigger is as follows:
32-
- immediate - Called when events are next processed
33-
- after - Called when the time is over
34-
- condition - Behaves like immediate. Providing a function will overwrite the default condition behaviour. If you want to do stuff conditionally and use a function, then just do your check and return false if the condition is not satisfied.
35-
- ease - Called each frame with the current value of the ease. The function defintion is a bit different here. The first argument is the current value of the ease. The return value is then set to the value stored in the table. The default function just returns the current value.
36-
- "before" - Called immediately. If the event does not complete after the delay, then it is automatically cancelled.
30+
- `immediate` - Called when the event begins
31+
- `after` - Called when the time is over
32+
- `condition` - Behaves exactly like immediate. Providing a function will overwrite the default condition behaviour.
33+
- `ease` - Called each frame with the current interpolated value so you can modify the easing function. The default function returns its argument unmodified.
34+
- `before` - Called when the event begins
3735

38-
delay - number: The time to take, in seconds. Used for after, ease and before. Note this value is affected by the game speed option. Default is 0.
36+
delay - number: The time to take, in seconds. Used for after, ease and before. This value is typically affected by the game speed option. Default is 0.
3937

40-
ref_table - table: The table to check for the condition. Used for condition and ease. No default.
38+
ref_table - table: The table to ease a value in. Required for ease events. Also used for condition events.
4139

42-
ref_value - string: The key in ref_table to get the value. Used for condition and ease. No default.
40+
ref_value - string: The key in ref_table to ease. Required for ease events. Also used for condition events.
4341

44-
stop_val - any: The value to stop at. Used for condition. No default.
42+
ease_to - number: The value for the ease to end at. Required for ease events.
4543

46-
ease_to - number: The value to end at. Used for ease. No default.
44+
ease - string: The type of ease to use. Used for ease. Default is "lerp". Can be any of "lerp" (linear ease), "elastic" (wobbly ease in) or "quad" (quadratic ease in).
4745

48-
ease - string: The type of ease to use. Used for ease. Default is "lerp". Can be any of "lerp", "elastic" or "quad".
46+
## Advanced Properties
47+
48+
stop_val - any: The value to wait for in a condition event.
49+
50+
start_timer - boolean: Set to true to start the time component of a before or after event immediately, instead of when the event starts.
51+
52+
no_delete - boolean: Set to true to prevent the event from being deleted by `clear_queue`.
53+
54+
pause_force - boolean: Set to true to force this event to act as though it were created while the game was paused. Events without this will pause when the game is paused.
55+
56+
timer - string: Set this to a key in `G.TIMERS` to use a different timer than the standard one. If `pause_force` was set, this defaults to `'REAL'`, otherwise it defaults to `'TOTAL'`.
4957

5058
# Examples
5159
Great, now you've been info dumped, let's show you some more practical examples.
@@ -78,9 +86,9 @@ G.E_MANAGER:add_event(Event({
7886
}))
7987
```
8088

81-
(you may notice this might be less than 5 seconds. This is because the delay is affected by the game speed option)
89+
(This might take less than 5 seconds. This is because the delay is affected by the game speed option)
8290

83-
Also note that this event will block for those 5 seconds, so like earlier, other events will wait until after your event is over. In this case they'll wait until after those 5 seconds.
91+
Note that this event will block for those 5 seconds, so like earlier, other events will wait until after your event is over. In this case, they'll wait for 5 seconds.
8492

8593
Now, let's say you want to wait until a certain condition is met. You can do this by checking your condition, and returning false if it is not met. For example, if you want to wait until the player has 2 hands left, you can do this:
8694

@@ -118,27 +126,49 @@ and with that, you should have everything you need to use events. However, I'll
118126
## Queues
119127
So far we have just been adding events to the base queue. However, there are a few other queues for us to use.
120128

121-
When calling G.E_MANAGER:add_event, you can pass in a second argument for the queue. This is string corresponding to the queue type (default is "base"). The queues are as follows:
129+
When calling `G.E_MANAGER:add_event`, you can pass in a second argument for the queue. This is a string corresponding to the queue type (default is "base"). The queues are as follows:
122130

123131
- base - The default queue. This is where most events should go.
124-
- other - Was once used for displaying a video in the demo. Since it's not used, it makes for a prime candidate for putting stuff in, if you want to use blocking but not block the base queue.
132+
- other - Was once used for displaying a video in the demo. No longer used.
125133

126134
The rest are all mostly for super specific things, but are documented here for completeness.
127135

128136
- unlock - This queue is for when you unlock new stuff. This allows it to keep showing new items when you close the last one.
129137
- tutorial - This is used for the tutorial. Allows it to manage some of it's stuff. Also let's all the tutorial stuff be cleared at once.
130138
- achievement - This is used for unlocking achievements. Allows the ui to show one at a time.
131139

132-
add_event also has a third argument, front. This is a boolean that will put the event at the front of the queue. Useful to block stuff already in the queue.
140+
You can also create your own queue. This is useful if you want to use blocking events that run alongside the base queue events without interacting with them.
141+
```lua
142+
G.E_MANAGER.queues.my_cool_queue = {}
143+
-- Later, with empty queues:
144+
G.E_MANAGER:add_event(Event {
145+
trigger = "after",
146+
delay = 10,
147+
func = function()
148+
print("These events should fire on the same frame")
149+
return true
150+
end
151+
})
152+
G.E_MANAGER:add_event(Event {
153+
trigger = "after",
154+
delay = 10,
155+
func = function()
156+
print("These events don't have a guaranteed order")
157+
return true
158+
end
159+
}, "my_cool_queue")
160+
```
161+
162+
`add_event` also has a third argument, front. This is a boolean that will put the event at the front of the queue. Using this within an event will cause incorrect behavior.
133163

134164
## Clearing the queue
135-
The event manager also has another function you may use.
136-
G.E_MANAGER:clear_queue(queue, exception)
165+
The event manager has another function:
166+
`G.E_MANAGER:clear_queue(queue, exception)`
137167

138-
Passing it with no options will clear all the queues. If a queue is passed, it will clear that queue. If an exception is passed, it will not clear all queues, except the one passed. You can avoid being cleared by setting the no_delete property to true.
168+
Calling it with no options will clear all the queues. If a queue is passed, it will clear that queue. If an exception is passed, it will clear every queue except the one passed. An event can avoid being cleared by setting its `no_delete` property to true.
139169

140170
## Repeating on a timer
141-
Here is an example I made to run an event every 5 seconds. This does use a bit of hacking to get working, but I figured it might be handy.
171+
Here is an example to run an event every 5 seconds. This does use a bit of hacking to get working, but I figured it might be handy.
142172

143173
```lua
144174
local event
@@ -164,17 +194,26 @@ We use `timer = "UPTIME"` here as the default timer (`"REAL"`) is reset when ret
164194

165195
This event also sets some properties to make sure it doesn't get blocked or block other events, and it runs even if the game is paused. If you want to make sure you can stop this event, then you could, say, add some check for some variable then return if it's true.
166196

167-
168-
## More properties
169-
These are some more event properties you can use, but aren't really that useful in most situations.
170-
171-
start_timer - boolean: If true, then it will use the starting time of the event being created. Otherwise this will start the timer the first time the event is processed. Default is false.
172-
173-
no_delete - boolean: If true, then when clearing the event queue, this event will not be deleted. You probably don't want to use this unless you want to have an event that never stops. Default is false.
174-
175-
force_pause - boolean: If true, then the event will run even if the game is paused. This only has an effect if your event was created when the game wasn't paused. Default is false.
176-
177-
timer - string: The name of the timer to use. Default is "REAL" if created while paused (or with force_pause) otherwise "TOTAL". Can take any of the keys in G.TIMERS.
197+
Here's another way to accomplish the same thing:
198+
```lua
199+
local create_event
200+
create_event = function()
201+
G.E_MANAGER:add_event(Event {
202+
blockable = false,
203+
blocking = false,
204+
pause_force = true,
205+
no_delete = true,
206+
trigger = "after",
207+
delay = 5,
208+
func = function()
209+
print("Hi mom!")
210+
create_event()
211+
return true
212+
end
213+
})
214+
end
215+
create_event()
216+
```
178217

179218
***
180219
*Guide written by WilsontheWolf*

0 commit comments

Comments
 (0)