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
Copy file name to clipboardExpand all lines: docs/assigning-global-variables.md
+25-29Lines changed: 25 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Setting global variables or functions
4
4
5
5
import { SideBySide } from "@site/src/components/SideBySide";
6
6
7
-
In some Lua environments, the host application expects you to define some global variables or functions as part of the API. For example, some engines might allow you to define some event handlers in your Lua, that will be called by the engine when different events happen:
7
+
In some Lua environments, the host application expects you to define global variables or functions as part of the API. For example, some engines might allow you to define some event handlers in your Lua, that will be called by the engine when different events happen:
8
8
9
9
```lua title=example.lua
10
10
functionOnStart()
@@ -38,11 +38,31 @@ end
38
38
39
39
</SideBySide>
40
40
41
-
This means we need some extra helper code to correctly register these global variables so your environment can access them.
41
+
This means we need extra helper code to correctly register these global variables so your environment can access them.
42
+
43
+
## Assigning to globals with declarations
44
+
45
+
The easiest way to set global variables is to first declare them as existing globals, and then assign their values. As an example:
In the example above, the declarations are in the same file as the value assignments. Alternatively, you could choose to put them in a .d.ts file included in your project. If these globals have pre-defined names specified by the engine the API, it is also possible to include these declarations in the .d.ts files (or types package) for this environment.
42
60
43
61
## Setting global variables with a helper function
44
62
45
-
One way to assign global variables and functions is to use a helper function like this:
63
+
Another way to assign global variables and functions is to use a helper function. The benefit is that you can strictly type the helper functions to ensure correct types are assigned, as well as having the ability to do additional logic like wrapping or modifying the value.
64
+
65
+
The simplest helper function looks like this:
46
66
47
67
```typescript
48
68
function registerEventHandler<TArgsextendsunknown[]>(
## Registering functions as class methods with a decorator
80
100
81
-
Sometimes you don't want to register just a loose function, but instead register a class or class method. A very nice way to do this is to use decorators (they unfortunately only work on classes, and not for loose functions).
101
+
Sometimes you don't want to register just a loose function, but instead register a class or class method. A nice way to do this is to use decorators (they unfortunately only work on classes, and not for loose functions).
82
102
83
103
One example of such a decorator is:
84
104
@@ -117,7 +137,7 @@ In the above example, `this` will be `nil` in the methods, do not try to use oth
117
137
118
138
## Registering classes with a decorator
119
139
120
-
Sometimes you want to register classes as globals, you can also do that with a decorator:
140
+
Sometimes you want to register classes instead of functions, you can also do that with a decorator:
121
141
122
142
```typescript
123
143
function registerClass<TClass, TArgsextendsunknown[]>(
@@ -173,27 +193,3 @@ class EventHandlers {
173
193
}
174
194
}
175
195
```
176
-
177
-
## Assigning to globals with declarations
178
-
179
-
The main weakness of the above methods is that you can declare any string, not protecting you from typos, and not giving any kind of editor support. This is fine if there are only a few such registrations that need to be done, but is somewhat error prone.
180
-
181
-
An alternative method would be to explicitly declare the global variables in a declarations file:
You can then assign to these functions as if they were global variables:
189
-
190
-
```typescript
191
-
OnStart= () => {
192
-
// start event handling code
193
-
};
194
-
OnStateChanged= (newState:State) => {
195
-
// state change event handler code
196
-
};
197
-
```
198
-
199
-
This of course only works if you know the names of the global variables beforehand, if these names are dynamic, consider using one of the other methods instead.
0 commit comments