Skip to content

Lua Integration

Shulkerscript supports writing Lua code directly in your scripts. This allows you to use Lua’s powerful features and libraries to extend the functionality of your scripts.

The Lua code is embedded in the Shulkerscript code using the lua keyword. You can pass variables from Shulkerscript to Lua, and the Lua code can return values that can be used in Shulkerscript.

int a = 5;
int b = lua(a) {
-- Lua code goes here
print("The objective is: " .. a.objective);
print("The target is: " .. a.target);
return 10;
};

Your Lua code should return a string value when used in combination with the run keyword. This string returned will be included in the output.

run lua() {
-- Lua code goes here
return "Hello, Lua!";
};

The variables from Shulkerscript can be passed to lua by putting the variable name in the parentheses of the lua keyword. The variable will be available in the Lua code as a global variable with the same name.

Depending on the type of the variable, the Lua code will receive a different type of value. The following table shows the mapping of Shulkerscript types to Lua types:

Shulkerscript TypeLua Type
inttable { objective: string, target: string }
booltable { storage: string, path: string }
macro function parameterstring ”$(macro)$
fn greet(name) {
run `say Greeting $(name) from Lua:`;
run lua(name) {
return {
value="Hello, " .. name .. "!",
contains_macro=true
}
};
}

The Lua environment has access to a few globals that are provided by Shulkerscript. These globals are available in the shulkerscript table. The following values are available in the shulkerscript table:

  • version: The version of Shulkerscript that is being used in string format.
  • file_path: The path to the file where the Lua code is written.
  • start_line: The line number where the Lua code starts.
  • end_line: The line number where the Lua code ends.
  • start_column: The column number where the Lua code starts.
  • end_column: The column number where the Lua code ends.

After variables are introduced in Shulkerscript, it is planned to make them available in the Lua environment as well.