Node Metadata

Introduction

In this chapter you will learn how to manipulate a node’s metadata.

What is Node Metadata?

Metadata is data about data. So Node Metadata is data about a node.

You may use metadata to store:

The node’s type, light levels, and orientation are not stored in the metadata, but rather are part of the data itself.

Metadata is stored in a key value relationship.

Key Value
foo bar
owner player1
counter 5

Getting a Metadata Object

Once you have a position of a node, you can do this:

local meta = minetest.get_meta(pos)
-- where pos = { x = 1, y = 5, z = 7 }

Reading Metadata

local value = meta:get_string("key")

if value then
	print(value)
else
	-- value == nil
	-- metadata of key "key" does not exist
	print(value)
end

Here are all the get functions you can use, as of writing:

In order to do booleans, you should use get_string and minetest.is_yes:

local value = minetest.is_yes(meta:get_string("key"))

if value then
	print("is yes")
else
	print("is no")
end

Setting Metadata

Setting meta data works pretty much exactly the same way.

local value = "one"
meta:set_string("key", value)

meta:set_string("foo", "bar")

Here are all the set functions you can use, as of writing:

Lua Tables

You can convert to and from lua tables using to_table and from_table:

local tmp = meta:to_table()
tmp.foo = "bar"
meta:from_table(tmp)

Infotext

The Minetest Engine reads the field infotext in order to make text appear on mouse-over. This is used by furnaces to show progress and signs to show their text.

meta:set_string("infotext", "Here is some text that will appear on mouse over!")

Your Turn