You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`pcall`| ✅ |Continue on error| Basic | Errors are logged; other subscribers still run; code after trigger continues.|
57
+
|`xpcall`| ❌ |Continue on error| Full | Same as pcall but with detailed tracebacks. More memory usage.|
58
+
|`none`| ❌ |Stop on error| Full | Error stops all execution immediately. Callbacks run with xpcall; error rethrown w/ traceback. |
59
59
60
-
You can set the Event Mode with code:
61
-
62
-
```lua
63
-
event.set_mode("pcall")
64
-
event.set_mode("xpcall")
65
-
event.set_mode("none")
66
-
```
67
60
68
61
## What is context?
69
62
70
63
Context is the script context where the event is triggered. It can be a GO script or a GUI script in Defold. Without context changing, you can't call `gui.set_text` from GO script for example.
71
64
72
-
## Error Behavior
73
65
74
-
**Continue on error** (`pcall` and `xpcall` modes):
75
-
- If one subscriber throws an error, it gets logged but other subscribers still run
76
-
- Code after `event:trigger()` continues executing
66
+
## Basic Usage
67
+
68
+
```lua
69
+
-- Lua module /my_module.lua
70
+
localevent=require("event.event")
71
+
72
+
localM= {}
73
+
74
+
-- Create events anywhere
75
+
M.on_value_changed=event.create()
76
+
77
+
functionM.set_value(self, value)
78
+
M._value=value
79
+
-- Trigger the event when required to call subscribers
80
+
M.on_value_changed:trigger(value)
81
+
end
77
82
78
-
**Stop on error** (`none` mode):
79
-
- If any subscriber throws an error, execution stops immediately
80
-
- No more subscribers are called, code after `event:trigger()` doesn't run
81
-
-**Important limitation**: Cross-context switching is disabled. Callbacks execute in the trigger's context rather than their subscription context, which removes the primary advantage of this event system over standard Lua function calls
83
+
returnM
82
84
83
-
The mode setting is global and affects all events in your project.
85
+
-- Lua script /my_script.script
86
+
localmy_module=require("my_module")
84
87
85
-
**Recommendation**: Use `pcall` for production (safe, fast) and `xpcall` for debugging (detailed errors).
88
+
localfunctionon_value_changed(self, value)
89
+
print("Value changed to:", value)
90
+
end
91
+
92
+
functioninit(self)
93
+
-- Subscribe to the event when required to call subscribers
94
+
-- Self is passed as the first parameter to the callback
Copy file name to clipboardExpand all lines: api/event_api.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ By default, the module uses the `pprint` logger for errors.
40
40
event.set_mode(mode)
41
41
```
42
42
43
-
Set the mode of the event module.
43
+
Set the mode of the event module. All modes support cross-context. In "none" mode callbacks run with xpcall; on error, the error is rethrown with full traceback.
0 commit comments