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
Additionally, `LuaFunction` operates asynchronously. Therefore, you can define a function that waits for an operation in Lua, such as the example below:
236
+
> [!TIP]
237
+
> Defining functions with `LuaFunction` can be somewhat verbose. When adding multiple functions, it is recommended to use the Source Generator with the `[LuaObject]` attribute. For more details, see the [LuaObject](#luaobject) section.
238
+
239
+
## Integration with async/await
240
+
241
+
`LuaFunction` operates asynchronously. Therefore, you can define a function that waits for an operation in Lua, such as the example below:
237
242
238
243
```cs
239
244
// Define a function that waits for the given number of seconds using Task.Delay
@@ -298,6 +303,146 @@ for (int i = 0; i < 10; i++)
298
303
}
299
304
```
300
305
306
+
## LuaObject
307
+
308
+
By applying the `[LuaObject]` attribute, you can create custom classes that run within Lua. Adding this attribute to a class that you wish to use in Lua allows the Source Generator to automatically generate the code required for interaction from Lua.
309
+
310
+
The following is an example implementation of a wrapper class for `System.Numerics.Vector3` that can be used in Lua:
311
+
312
+
```cs
313
+
usingSystem.Numerics;
314
+
usingLua;
315
+
316
+
varstate=LuaState.Create();
317
+
318
+
// Add an instance of the defined LuaObject as a global variable
319
+
// (Implicit conversion to LuaValue is automatically defined for classes with the LuaObject attribute)
320
+
state.Environment["Vector3"] =newLuaVector3();
321
+
322
+
awaitstate.DoFileAsync("vector3_sample.lua");
323
+
324
+
// Add LuaObject attribute and partial keyword
325
+
[LuaObject]
326
+
publicpartialclassLuaVector3
327
+
{
328
+
Vector3vector;
329
+
330
+
// Add LuaMember attribute to members that will be used in Lua
331
+
// The argument specifies the name used in Lua (if omitted, the member name is used)
332
+
[LuaMember("x")]
333
+
publicfloatX
334
+
{
335
+
get=>vector.X;
336
+
set=>vector=vectorwith { X=value };
337
+
}
338
+
339
+
[LuaMember("y")]
340
+
publicfloatY
341
+
{
342
+
get=>vector.Y;
343
+
set=>vector=vectorwith { Y=value };
344
+
}
345
+
346
+
[LuaMember("z")]
347
+
publicfloatZ
348
+
{
349
+
get=>vector.Z;
350
+
set=>vector=vectorwith { Z=value };
351
+
}
352
+
353
+
// Static methods are treated as regular Lua functions
The types of fields/properties with the `[LuaMember]` attribute, as well as the argument and return types of methods, must be either `LuaValue` or convertible to/from `LuaValue`.
389
+
390
+
Return types such as `void`, `Task/Task<T>`, `ValueTask/ValueTask<T>`, `UniTask/UniTask<T>`, and `Awaitable/Awaitable<T>` are also supported.
391
+
392
+
If the type is not supported, the Source Generator will output a compile-time error.
393
+
394
+
### LuaMetamethod
395
+
396
+
By adding the `[LuaMetamethod]` attribute, you can designate a C# method to be used as a Lua metamethod.
397
+
398
+
Here is an example that adds the `__add`, `__sub`, and `__tostring` metamethods to the `LuaVector3` class:
> `__index` and `__newindex` cannot be set as they are used internally by the code generated by `[LuaObject]`.
445
+
301
446
## Module Loading
302
447
303
448
In Lua, you can load modules using the `require` function. In regular Lua, modules are managed by searchers within the `package.searchers` function list. In Lua-CSharp, this is replaced by the `ILuaModuleLoader` interface.
0 commit comments