C#: Lua Value Property

Use Case

Your C# LunyScript subclass needs to expose Lua script values to other C# scripts.

Lua Implementation

Your Lua script defines a script.currentHealth value that you wish to make available in C#:

local script = {...}
script.currentHealth = 100
...
return script

C# Implementation

You can implement a C# property in the accompanying LunyScript subclass.

The following snippet binds the Lua script.currentHealth value by using the same variable name.

public float currentHealth
{
    get => GetFloat(nameof(currentHealth));
    set => SetFloat(nameof(currentHealth), value);
}

This keeps the script.currentHealth value in synch when the property is read or assigned to in C#.

Note

You can also bind LuaTable and LuaValue types for best flexibility, for instance as data transfer object.

Limitations

The Lua variable must be defined in the script table.

CodeSmile

CodeSmile

Game Engineer and Code Composer since 1999

Leave a Reply

Your email address will not be published. Required fields are marked *