lua
lua
Syntaxe:
appel statique
variable.functionname(param1, ...)
appel pour une instance
variable:functionname(param1, ...)
Instructions:
local varname1 = value1
local varname1, varname2, varname3 = value1, value2, value3
local varname1, _, _ = funcName() -- where funcName return three variables
local function name(param1, param2)
code...
return expr
end
break -- quitte une boucle
return -- quitte une fonction
Structures:
if expr then
code
end
if expr then
code
else
code
end
if expr then code end
for valueName in someArray do
code
end
for varName in initialValue, endValue do
code
end
for key, value in pairs(table) do
code
end
Conversion:
nil est faux dans if nil then
"" est faux dans if "" then
Opérateur:
. accès au champ d'une table. ex: a = {key = "b"} a.key
: accède à la propriété/méthode d'un objet
.. concatène deux chaînes. ex: "abc" .. "\n"
== égalité. ex "a" == "a"
~= n'est pas égal. ex: "a" ~= "b"
and a and b
or a or b
# longueur d'une chaine. ex: text = "abc" #text is 3
+ addition
- soustraction
/ division
not contraire. ex: not nil
Exemples:
string ~= "" and string or text (si string n'est pas vide, retourne string, sinon text)
math
math.ceil(float) : int
math.random(int max) : int
string
:gmatch("selector") : array // exemple "ab":gmatch("(ab|c)")
:gsub(string, string) : int
string.format(string, arg1, arg2, ...) : string // exemple string.format("%04x", 255)
tonumber(string) : int
minetest
-- string
minetest.deserialize("string")
-- form
minetest.formspec_escape("string")
minetest.show_formspec("playername", "formname", "aFormspecString")
minetest.register_on_player_receive_fields(function(Player player, string formname, table fields) code end)
-- table fields contains fields accessible with their name
-- as fields.fieldname
-- environnement 3d
minetest.add_item(Position, ItemStack)
minetest.get_meta(Position) : Meta
minetest.get_node(Position) : Node
minetest.get_node_light(Position) : int
minetest.registered_nodes = {nodename: {properties like description}, ...}
minetest.set_node(Position, {name = "nodename"})
minetest.node_dig(Position, Node, digger?)
-- settings
minetest.settings
:get_bool(string) : bool
-- sound
minetest.sound_play("filenamewithoutextension", {pos = Position, max_hear_distance = int, gain = float})
-- protection
minetest.record_protection_violation(Position pos, string playername)
-- chat
minetest.chat_send_player(string playername, string message)
-- player
minetest.get_inventory({type = "player", name = string}) : Inventory
-- register
minetest.register_alias('name', 'realname')
minetest.register_abm({
label = string,
nodenames = {string, string},
neighbors = {string, string},
interval = int, -- (ex: 1, 12, ...)
chance = int, -- (ex: 2, 83, 71, ...)
catch_up = false,
action = function,
})
minetest.register_craft({
[type = "toolrepair"|"cooking"|"fuel"|"shapeless",]
output = 'name'|'name number',
recipe = {
{'name'}
}|{
{'name', 'name', 'name'},
{'', 'name', ''},
{'', '', 'name'},
}|{
{'name'},
{'name'},
{'name'},
}|{
{'name', 'name', 'name'},
{'name', 'name', 'name'},
}|{
{'name', 'name'}
}|"name",
[additional_wear = float,]
[cooktime = int,]
})
Note:
if type = "cooking", output is "name", recipe = "string". Propriétés: cooktime
if type = toolrepair. Propriétés: additional_wear
if type = fuel. Propriétés: cooktime
if no type, output is an oibject.
minetest.register_craftitem("name", {
description = "description",
inventory_image = "file.png",
groups = {
book = 0|1,
stick = 0|1,
coal = 0|1,
flammable = int,
not_in_creative_inventory = function, -- chest
}
on_skeleton_key_use = function(Position pos, Player user, string secret)
return nil
return stringSecret, stringLabel, stringOwner
end, -- (chess)
stack_max = int,
onuse = function(ItemStack itemstack, Player user, table pointed_thing = {type: "node"|, under: Position, ...})
return itemstack
end,
})
minetest.register_node("name", {
description = string,
drawtype = string, -- example "plantlike"|"nodebox"
waving = 0|1,
tiles = {"filename.png"},
inventory_image = "filename.png"|"filename.png^file.png^filename.png^[makealpha:255,10,10"
wield_image = "filename.png",
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
walkable = false,
selection_box = {
type = "fixed"'
fixed = {float, float, float, float, float, float},
},
groups = {
snappy = 3,
flammable = 2,
choppy = 2,
oddly_breakable_by_hand = 2,
fence = 1
},
sounds = {},
after_dig_node = function(pos, node, metadata, digger) -- digger can be nil
end,
-- type nodebox
node_box = {
type = "connected",
fixed = {{float, float, float, float, float, float}},
connected_front = {{6 floats},{6 floats}},
connected_left = {{6 floats},{6 floats}},
connected_back = {{6 floats},{6 floats}},
connected_right = {{6 floats},{6 floats}},
},
connects_to = {"name", "name", "name"},
})
-- events
minetest.register_on_craft(function(ItemStack itemstack, Player player, old_craft_grid, craft_inv) code end)
Node
.name
Position
.x
.y
.z
Meta
:to_table() : table {fields = {owner = "ownername", title = "", text = "", text_len = int, page = "", page_max = int, ...}}
:from_table({ fields = table }) -- save new fields
:get_string(string fieldName) : string
:get_invetory() : Inventory
Inventory
:room_for_item(string name, ItemStack item)
:get_size(string name)
:set_stack(string name, int position, ItemStack)
:add_item(string name, ItemStack item) : ItemStack
:get_stack(string name, int position) : ItemStack
ItemStack
constructor: varName = ItemStack(string name)
:get_count() : int
:get_meta() : Meta
:get_metadata() : string
:get_name() : string
:set_name(string name)
:set_count(int)
:take_item()
:to_table()
Player
:get_inventory()
:get_wielded_item() : ItemStack
:set_wielded_item(ItemStack)
:get_player_name() : string
:getpos() : Position
formspec string:
size[int width,int height]
bgcolor[#codeRGBA;boolean]
background[float,float;float,float;filename;boolean]
field[float,float;float,int;name,label;value]
textarea[float,float;float,float;name;label;value]
button[float,float;float,float;name;label]
button_exit[float,float;float,float;name;label]
label[float,float;label]
tablecolumns[color;text]
tableoptions[background=#RGBA;highlight=#RGBA;border=false]
table[float,float;float,float;title;#RGBA,value]