https://github.com/matttuttle/hx-lua
Simple lua wrapper in a haxe extension
https://github.com/matttuttle/hx-lua
Last synced: 8 months ago
JSON representation
Simple lua wrapper in a haxe extension
- Host: GitHub
- URL: https://github.com/matttuttle/hx-lua
- Owner: MattTuttle
- Created: 2014-05-24T01:25:25.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-05-29T15:16:02.000Z (about 12 years ago)
- Last Synced: 2023-03-11T23:27:25.385Z (over 3 years ago)
- Language: C
- Homepage:
- Size: 2.09 MB
- Stars: 41
- Watchers: 9
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Run Lua code in Haxe
====================
Run any Lua code inside Haxe on neko/cpp targets. Has the option of passing a context object that will set variables before running the script.
```haxe
var result = Lua.run("return true"); // returns a Bool to Haxe
```
Pass in values with a context object. The key names are used as variable names in the Lua script.
```haxe
var result = Lua.run("if num > 14 then return 14 else return num end", {num: 15.3});
```
What if you want to call a function created in Haxe from Lua? Just pass the function in the context!
```haxe
var result = Lua.run("return plus1(15)", { plus1: function(val:Int) { return val + 1; } });
if (result == 16)
trace("success!");
```
Lua instances
=============
It's possible to create multiple Lua instances to run scripts with different contexts/libraries.
```haxe
var lua = new Lua();
lua.loadLibs(["base", "math"]);
lua.setVars({ myVar: 1 });
var result = lua.execute("return myVar");
```
Calling Lua functions
---------------------
Calling global functions defined in lua can be done after executing a chunk of Lua code. You can either pass in a single value (for single argument functions) or an array (for multiple argument functions).
```haxe
var lua = new Lua();
lua.execute("function add(a, b) return a + b end");
var result = lua.call("add", [1, 5]); // returns 6
```