ultra-lua

Easy-to-implement Lua scripting

Usage no npm install needed!

<script type="module">
  import ultraLua from 'https://cdn.skypack.dev/ultra-lua';
</script>

README

ultra-lua allows you to add both simple and advanced Lua scripting into just about any NodeJS program. Example:

const lua = require("ultra-lua");  
lua.Execute(`
    print("Hello world!")
`);

Ultra-lua allows you to go even deeper with Includes, Predefinitions, and Responses.

As a simple example we will be making a program that allows people to write Lua code just as usual but we will also add a "ExitProgram" function that prints something to the console then exits the NodeJS app.

predef.lua

--include <exit.json> as ExitResponse  
  
function ExitProgram()  
    print(ExitResponse)  
end

This will be the Lua script that runs before the script the user has written. It's use is to store functions and variables the user could use in their script while not filling up the user's screen with hundreds of functions and variables (it also doesn't let the user to directly modify your code).

  • The --include keyword allows you to include in responses declarations (which allow your NodeJS script to know about certain events happening when the event's response declaration is printed to the console.

exit.json

{  
  "response": true,  
  "name": "Exit",  
  "data": 0  
}

This is the response declaration we just included, it's just a JSON file storing data that will be passed to our NodeJS script. The "response": true and the "name": "blah blah" parts are necessary as it tells ultra-lua not to print it out like it's a normal JSON file but instead to send it to our NodeJS script (and the name part is how we will know which response is which).

script.lua

print("Hello world!");  
ExitProgram()

Prints out "Hello world!" and calls the "ExitProgram" function we made earlier.

example.js (main)

const lua = require("ultra-lua");
const result = lua.Execute(
    lua.File(__dirname + "\\script.lua"), // The File function just reads data from a file.
    lua.File(__dirname + "\\predef.lua")  
);

const responses = lua.FindResponse(result);
for (let i = 0; i < responses.length; i++) {  
    switch(responses[i].name) {  
        case "Exit":
            console.log("Exiting..");
            process.exit(responses[i].data);
            break;
  }  
}

Now i know, that's quite a bit of code, but the main thing this is doing is loading in both of the scripts we made (loading the predefined script before our normal one) into the Execute function (which ties everything up together and runs the code). Then it tries to get a list of responses (and the Execute function returns every single thing we printed out to the console, even the hidden responses we printed to the console earlier) and it goes trough a for loop seeing if there is any response being used who has a certain name (and we only added one function and one response declaration and we named it "Exit") so when "Exit" is called it will print something to the console then it will execute process.exit passing in the "data" section from the JSON (which is 0) so the NodeJS app exits with the code of 0.