NodeMetaRef
Jump to navigation
Jump to search
The instance of a node in the world normally only contains the three values mentioned in "Nodes". However, it is possible to insert extra data into a node. It is called "node metadata".
Metadata contains two things:
- A key-value store
- An inventory
Some of the values in the key-value store are handled specially:
formspec
— Defines a right-click inventory menu. See "Formspec".infotext
— Text shown on the screen when the node is pointed at
Can be gotten via minetest.env:get_meta(pos)
.
Warning all metadata is sent to client, don't store sensitive stuff here.
Methods
set_string(name, value)
get_string(name)
set_int(name, value)
get_int(name)
set_float(name, value)
get_float(name)
[NodeMetaRef]get_inventory()
— returns InvRef[NodeMetaRef]to_table()
— returnsnil
or{fields = {...}, inventory = {list1 = {}, ...}}
from_table(nil
or{})
Example
local meta = minetest.env:get_meta(pos)
meta:set_string("formspec",
"invsize[8,9;]"..
"list[context;main;0,0;8,4;]"..
"list[current_player;main;0,5;8,4;]")
meta:set_string("infotext", "Chest");
local inv = meta:[NodeMetaRef]get_inventory()
inv:set_size("main", 8*4)
print(dump(meta:[NodeMetaRef]to_table()))
meta:from_table({
inventory = {
main = {[1] = "default:dirt", [2] = "", [3] = "", [4] = "", [5] = "", [6] = "", [7] = "", [8] = "", [9] = "", [10] = "", [11] = "", [12] = "", [13] = "", [14] = "default:cobble", [15] = "", [16] = "", [17] = "", [18] = "", [19] = "", [20] = "default:cobble", [21] = "", [22] = "", [23] = "", [24] = "", [25] = "", [26] = "", [27] = "", [28] = "", [29] = "", [30] = "", [31] = "", [32] = ""}
},
fields = {
formspec = "invsize[8,9;]list[context;main;0,0;8,4;]list[current_player;main;0,5;8,4;]",
infotext = "Chest"
}
})
An excerpt from an e-mail between Minetest developers
> When I attach a Lua table to a node I seem to have a > choice: to store things with many calls to > meta.set_int/meta.set_string etc. or via meta:from_table. > meta:from_table lets me store an arbitrary table under "fields": > > local meta = minetest.env:get_meta(pos) > local mt = meta:to_table() > > len = tonumber(mt.fields.length_remaining) > > ... only I have noticed that every key under fields can only store > strings. So as a general purpose 'table' store, this is not so useful. > I came upon this because I wanted to store the initial position of a > node. This requires either: > > meta:set_int('x', pos.x) > meta:set_int('y', pos.y) > meta:set_int('z', pos.z) > > or something like: > > mt.fields.tail_pos = "return {" .. pos.x .. "," .. pos.y .. "," .. pos.z .. "}" > > and then something like this to read it back: > tail_pos = loadstring(mt.fields.tail_pos)() > > > ...correct? Or am I missing an obvious alternate solution? The api > docs are not detailed on this. Answer by celeron55: For simplicity, all the fields are internally strings, and don't store any type information. I can see the inconvenience though. You shouldn't use loadstring() directly, because then somebody could create a world which stores arbitrary Lua code, which can eg. remove all files, or something similar. For storing Lua variables with type information, you can use minetest.serialize() and minetest.deserialize(). They take any serializable Lua table, which can contain: - tables - strings - numbers - functions that don't access anything outside of what is stored in the data itself and what is passed as a parameter)) and make it into a string, and the other way around, and they will not execute any foreign code (unless you do certain complicated-ish stupid things, which probably won't happen). You can check out the implementation in builtin/serialize.lua. So if you want to store a position in the data of a node, you can use: local pos = {x=this, y=and, z=that} meta:set_string("tail_pos", minetest.serialize(pos)) and local pos = minetest.deserialize(meta:get_string("tail_pos")). Keep in mind though that that thing isn't very largely in use, and there might be unnoticed problems.