Add metadata to tool

Post Reply
User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Add metadata to tool

by runs » Post

How?

I would like in the on_construct function, but is not a node (with a pos).

maybe in tool_capabilities could I add a custom key/value?

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Add metadata to tool

by rubenwardy » Post

Please can you stop making duplicate posts, and learn to use the edit button.

Answer:

Code: Select all

local meta = stack:get_meta()
Make sure to save the stack by returning or setting it
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: Add metadata to tool

by runs » Post

Thanks :-)
rubenwardy wrote:Please can you stop making duplicate posts, and learn to use the edit button.

Answer:

Code: Select all

local meta = stack:get_meta()
Make sure to save the stack by returning or setting it

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

Re: Add metadata to tool

by AiTechEye » Post

i suspect you need know how to get and set the data too:

local item=itemstack:to_table()
item.metadata -- the meta
itemstack:replace(item) -- set meta and data

but if you need to set more then text you need to use serialize

https://github.com/AiTechEye/bows/blob/ ... ua#L77-L92

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: Add metadata to tool

by runs » Post

AiTechEye wrote:i suspect you need know how to get and set the data too:

local item=itemstack:to_table()
item.metadata -- the meta
itemstack:replace(item) -- set meta and data

but if you need to set more then text you need to use serialize

https://github.com/AiTechEye/bows/blob/ ... ua#L77-L92
Thanks, very explicative!!!

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Add metadata to tool

by rubenwardy » Post

AiTechEye wrote:i suspect you need know how to get and set the data too:

local item=itemstack:to_table()
item.metadata -- the meta
itemstack:replace(item) -- set meta and data

but if you need to set more then text you need to use serialize

https://github.com/AiTechEye/bows/blob/ ... ua#L77-L92
This is completely the wrong way to do it. metadata is deprecated and shouldn't be used

Do this instead:

Code: Select all

local meta = itemstack:get_meta()

-- Use just like with node meta
meta:set_string("foo", "bar")
meta:set_string("counter", 1)

-- Then make sure to save somehow, typically by returning it if on_use
return itemstack
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: Add metadata to tool

by runs » Post

I've done as you say, cos I read on the wiki for developers. And finally I saved with 'set_stack'.
rubenwardy wrote:
AiTechEye wrote:i suspect you need know how to get and set the data too:

local item=itemstack:to_table()
item.metadata -- the meta
itemstack:replace(item) -- set meta and data

but if you need to set more then text you need to use serialize

https://github.com/AiTechEye/bows/blob/ ... ua#L77-L92
This is completely the wrong way to do it. metadata is deprecated and shouldn't be used

Do this instead:

Code: Select all

local meta = itemstack:get_meta()

-- Use just like with node meta
meta:set_string("foo", "bar")
meta:set_string("counter", 1)

-- Then make sure to save somehow, typically by returning it if on_use
return itemstack

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests