MetaDataRef

Meta data is data about something. You can store meta data in nodes and stacks.

Node meta data can be gotten via, and item stack meta data from.

Warning all metadata is sent to client, don't store sensitive stuff here.

Methods

 * — returns  or
 * or
 * — returns  or
 * or
 * — returns  or
 * or
 * — returns  or
 * or

Nodes only

 * — returns InvRef, only on nodes
 * — nodes only, stops the field from being sent to the client

Nodes
You can use the following keys in node meta data for a special use:


 * — Defines a right-click inventory menu. See "Formspec".
 * — Text shown on the screen when the node is pointed at

Item Stacks
You can use the following keys in stack meta data for a special use:


 * — Overrides the tooltip in an inventory

Example
This adds a chatcommand, that opens a formspecs in that you can change the color and name of an item, if you havethe rename priv.

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.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.