Skip to content

Commit ed1a330

Browse files
committed
review comments
1 parent 0fff5f4 commit ed1a330

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

docs/assigning-global-variables.md

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Setting global variables or functions
44

55
import { SideBySide } from "@site/src/components/SideBySide";
66

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:
88

99
```lua title=example.lua
1010
function OnStart()
@@ -38,11 +38,31 @@ end
3838

3939
</SideBySide>
4040

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:
46+
47+
```ts
48+
declare var OnStart: (this: void) => void;
49+
declare var OnStateChanged: (this: void, newState: State) => void;
50+
51+
OnStart = () => {
52+
// start event handling code
53+
};
54+
OnStateChanged = (newState: State) => {
55+
// state change event handler code
56+
};
57+
```
58+
59+
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.
4260

4361
## Setting global variables with a helper function
4462

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:
4666

4767
```typescript
4868
function registerEventHandler<TArgs extends unknown[]>(
@@ -78,7 +98,7 @@ function registerGlobalVariable<T>(variableName: string, value: T): void {
7898

7999
## Registering functions as class methods with a decorator
80100

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).
82102

83103
One example of such a decorator is:
84104

@@ -117,7 +137,7 @@ In the above example, `this` will be `nil` in the methods, do not try to use oth
117137

118138
## Registering classes with a decorator
119139

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:
121141

122142
```typescript
123143
function registerClass<TClass, TArgs extends unknown[]>(
@@ -173,27 +193,3 @@ class EventHandlers {
173193
}
174194
}
175195
```
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:
182-
183-
```ts
184-
declare var OnStart: (this: void) => void;
185-
declare var OnStateChanged: (this: void, newState: State) => void;
186-
```
187-
188-
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

Comments
 (0)