minetest_modding_book/_en/map/node_metadata.md

2.6 KiB

title layout root idx description redirect_from
Node Metadata default ../.. 3.3 Using get_meta to obtain a NodeMetaRef. /en/chapters/node_metadata.html

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:

  • A node's inventory (such as in a chest).
  • Progress on crafting (such as in a furnace).
  • Who owns the node (such as the owner of a locked chest).

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

Metadata is stored in a key value relationship. For example:

Key Value
foo bar
owner player1
counter 5

Getting a Metadata Object

If you know the position of a node, you can retrieve its metadata:

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

Reading Metadata

After retrieving the metadata, you can read its values:

local value = meta:get_string("key")

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

The functions available include:

  • get_string
  • get_int
  • get_float
  • get_inventory

To get a Boolean, 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

You can set node metadata. For example:

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

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

This can be done using the following functions:

  • set_string
  • set_int
  • set_float

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 to make text appear on mouse-over. This is used by furnaces to show progress and by signs to show their text. For example:

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

Your Turn

  • Make a node which disappears after it has been punched five times. (Use on_punch in the node definition and minetest.set_node.)