-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new docs about assigning variables to global scope #186
Conversation
docs/assigning-global-variables.md
Outdated
|
||
import { SideBySide } from "@site/src/components/SideBySide"; | ||
|
||
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit
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: | |
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: |
docs/assigning-global-variables.md
Outdated
|
||
</SideBySide> | ||
|
||
This means we need some extra helper code to correctly register these global variables so your environment can access them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit
This means we need some extra helper code to correctly register these global variables so your environment can access them. | |
This means we need extra helper code to correctly register these global variables so your environment can access them. |
docs/assigning-global-variables.md
Outdated
|
||
## Assigning to globals with declarations | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the first two options you also don't have to specify a string, right?
Also we should probably mention these issues earlier. Think it is important to highlight that editor support/type safety goes out the window as soon as you use any of the above solutions.
docs/assigning-global-variables.md
Outdated
## Assigning to globals with declarations | ||
|
||
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. | ||
|
||
An alternative method would be to explicitly declare the global variables in a declarations file: | ||
|
||
```ts | ||
declare var OnStart: (this: void) => void; | ||
declare var OnStateChanged: (this: void, newState: State) => void; | ||
``` | ||
|
||
You can then assign to these functions as if they were global variables: | ||
|
||
```typescript | ||
OnStart = () => { | ||
// start event handling code | ||
}; | ||
OnStateChanged = (newState: State) => { | ||
// state change event handler code | ||
}; | ||
``` | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be the first option.
As this is the "most correct/safe" option. The other ones maybe more convenient, but also slightly more hacky.
docs/assigning-global-variables.md
Outdated
An alternative method would be to explicitly declare the global variables in a declarations file: | ||
|
||
```ts | ||
declare var OnStart: (this: void) => void; | ||
declare var OnStateChanged: (this: void, newState: State) => void; | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be declared in a declarations file or can this be declared in any ts file?
No description provided.