Skip to content

s4lt3d/MoonSharp-Unity-Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MoonSharp Unity Lua Scripting Example

A demonstration of integrating Lua scripting into Unity using the MoonSharp interpreter for C# ↔ Lua interop.


Overview

This example shows how to call Lua functions from C#, expose C# functions to Lua, and share variables between the two languages using MoonSharp within Unity.


Prerequisites


Key Features

1. Calling Lua Functions from C#

The example contains an update function written in Lua, which is called during Unity's Update method:

function update()
    local currentTime = time()  -- Get time from C# script
    print('Time since startup: ' .. currentTime)
end

In C#, the Lua update function is fetched as a DynValue and then invoked during Unity's Update:

luaUpdateFunction = luaScript.Globals.Get("update");
if (luaUpdateFunction.Type == DataType.Function) {
    luaScript.Call(luaUpdateFunction);
}

2. Exposing C# Functions to Lua

Register C# functions to be callable from Lua:

luaScript.Globals["print"] = (Action<string>)LuaPrint;
luaScript.Globals["magnitude"] = (Func<float, float, float>)CalculateMagnitude;
luaScript.Globals["time"] = (Func<float>)GetTime;

3. Accessing Lua Variables from C#

Retrieve values created in Lua scripts:

DynValue xValue = luaScript.Globals.Get("x");
Debug.Log("Getting variable x: " + xValue.Number);

Usage

  1. Ensure Unity 2022.3 is installed
  2. Import MoonSharp library into your project
  3. Attach LuaTest.cs to any GameObject
  4. Run the scene and check console for logs from both C# and Lua

Project Structure

Assets/
├── Scripts/
│   └── LuaTest.cs           — Main C# script managing Lua interaction
├── Lua/
│   └── script.lua           — Lua script with update function
└── README.md

How It Works

  1. Initialize MoonSharp — Create a new Lua environment in C#
  2. Load Lua script — Execute Lua code from string or file
  3. Register callbacks — Expose C# functions to Lua via globals
  4. Retrieve functions — Get Lua functions as DynValue objects
  5. Call functions — Invoke Lua functions from C# with parameters
  6. Exchange data — Share variables between languages

Common Patterns

Calling Lua from C#

DynValue function = lua.Globals.Get("functionName");
DynValue result = lua.Call(function, arg1, arg2);

Exposing C# Methods to Lua

lua.Globals["methodName"] = (Func<int, int>)MyMethod;

Getting Lua Variables

DynValue value = lua.Globals.Get("variableName");
int result = (int)value.Number;

Important Notes

  • Always wrap Lua execution in try-catch for error handling
  • Ensure C# method signatures match expected Lua function calls
  • Use DynValue for safe type conversion between C# and Lua
  • Keep Lua scripts small and focused for better performance

About

Showcases basic Lua scripting functionality within C# for Unity.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages