Skip to content
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

Merged
merged 3 commits into from
Jul 14, 2024
Merged

Conversation

Perryvw
Copy link
Member

@Perryvw Perryvw commented Jul 13, 2024

No description provided.


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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
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:


</SideBySide>

This means we need some extra helper code to correctly register these global variables so your environment can access them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
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.


## 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.
Copy link
Member

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.

Comment on lines 177 to 199
## 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.
Copy link
Member

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.

Comment on lines 181 to 186
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;
```
Copy link
Member

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?

@Perryvw Perryvw changed the title Add new docs abbout assigning variables to global scope Add new docs about assigning variables to global scope Jul 14, 2024
@Perryvw Perryvw merged commit 44cbe51 into source Jul 14, 2024
2 checks passed
@Perryvw Perryvw deleted the globals-docs branch July 14, 2024 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants