minetest_modding_book/_en/map/node_metadata.md

127 lines
2.7 KiB
Markdown
Raw Normal View History

2015-09-25 09:38:44 -07:00
---
title: Node Metadata
layout: default
2017-08-26 08:40:30 -07:00
root: ../../
2018-07-15 07:28:10 -07:00
idx: 3.3
description: Using get_meta to obtain a NodeMetaRef.
2015-09-25 09:38:44 -07:00
---
## Introduction
In this chapter you will learn how to manipulate a node's metadata.
* [What is Node Metadata?](#what-is-node-medadata)
* [Getting a Metadata Object](#getting-a-metadata-object)
* [Reading Metadata](#reading-metadata)
* [Setting Metadata](#setting-metadata)
* [Lua Tables](#lua-tables)
* [Infotext](#infotext)
* [Your Turn](#your-turn)
2015-09-25 09:38:44 -07:00
## 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).
2015-09-25 09:38:44 -07:00
The node's type, light levels, and orientation are not stored in the metadata.
These are part of the data itself.
2015-09-25 09:38:44 -07:00
Metadata is stored in a key value relationship. For example:
2015-09-25 09:38:44 -07:00
| 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:
2015-09-25 09:38:44 -07:00
{% highlight lua %}
local meta = minetest.get_meta(pos)
-- where pos = { x = 1, y = 5, z = 7 }
{% endhighlight %}
## Reading Metadata
After retrieving the metadata, you can read its values:
2015-09-25 09:38:44 -07:00
{% highlight lua %}
local value = meta:get_string("key")
if value then
print(value)
2015-09-25 09:38:44 -07:00
else
-- value == nil
-- metadata of key "key" does not exist
print(value)
2015-09-25 09:38:44 -07:00
end
{% endhighlight %}
The functions available include:
2015-09-25 09:38:44 -07:00
* get_string
* get_int
* get_float
* get_inventory
To get booleans, you should use `get_string` and `minetest.is_yes`:
2015-09-25 09:38:44 -07:00
{% highlight lua %}
local value = minetest.is_yes(meta:get_string("key"))
if value then
print("is yes")
2015-09-25 09:38:44 -07:00
else
print("is no")
2015-09-25 09:38:44 -07:00
end
{% endhighlight %}
## Setting Metadata
You can set node metadata. For example:
2015-09-25 09:38:44 -07:00
{% highlight lua %}
local value = "one"
meta:set_string("key", value)
meta:set_string("foo", "bar")
{% endhighlight %}
This can be done using the following functions:
2015-09-25 09:38:44 -07:00
* set_string
* set_int
* set_float
## Lua Tables
You can convert to and from lua tables using `to_table` and `from_table`:
{% highlight lua %}
local tmp = meta:to_table()
tmp.foo = "bar"
meta:from_table(tmp)
{% endhighlight %}
## 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:
{% highlight lua %}
meta:set_string("infotext", "Here is some text that will appear on mouse-over!")
{% endhighlight %}
2015-09-25 09:38:44 -07:00
## 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`.)