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
feat: add global types cache making get_type_by_name redundant (#370)
# Summary
using `get_type_by_name` is slightly cumbersome. In order to not repeat
yourself AND avoid polluting globals, you need to call:
```lua
local Type = world.get_type_by_name("MYType")
```
in EVERY callback, or alternatively do it once at the top of your
script, but doing that goes against the advice to not run code in the
body of the script outside of any functions.
this PR adds a global `types` cache, which lets you do:
```lua
types.MyType
```
or for more complex types:
```lua
types["MyGenericType<MyOtherType>"]
```
The types populated in the cache are equivalent to the types available
as static globals with the addition of generic types.
The change makes use of changes in #369 to correctly type this global as
`HashMap<String, ScriptComponentRegistration |
ScriptResourceRegistration | ScriptTypeRegistration >` meaning once we
start generating decleration files for lua we will have the correct
types at hand
# Migration Guide
- Not a breaking change, but you should change instances where you call
`get_type_by_name("T")` to `types["T"]` etc. this will increase
performance as well as make your scripts easier to read!
/// Similar to [`Self::get_type_by_name`] but returns a type erased [`ScriptTypeRegistration`], [`ScriptComponentRegistration`] or [`ScriptResourceRegistration`]
835
+
/// depending on the underlying type and state of the world.
0 commit comments