How do I set item's "tool_capabilities" from item meta?

User avatar
davidthecreator
Member
 
Posts: 319
Joined: Mon Aug 18, 2014 19:48
Location: Lithuania
GitHub: daviddoesminetest
In-game: DavidDoesMinetest

How do I set item's "tool_capabilities" from item meta?

by davidthecreator » Mon Jul 15, 2019 17:21

When I tried to write
Code: Select all
itemstack:get_meta():set_string("tool_capabilities", "{}")

It does seem to work and completely remove all tool capabilities from the item...
But when I actually write some sort of capability I want to edit, it dosen't work...
For example, if I try to change the full_punch_interval:
Code: Select all
itemstack:get_meta():set_string("tool_capabilities", "{full_punch_interval=1.0}")

It dosen't work... And says: "Missing '}' or object member name" ...

So how do I make it work?
 

User avatar
orwell
Member
 
Posts: 788
Joined: Wed Jun 24, 2015 18:45
Location: Raxacoricofallapatorius
GitHub: orwell96
IRC: orwell96_mt
In-game: orwell

Re: How do I set item's "tool_capabilities" from item meta?

by orwell » Mon Jul 15, 2019 18:46

From what I know, the item meta string expects a string in the JSON format. You can generate this by using
Code: Select all
local meta = itemstack:get_meta()
meta:set_string("tool_capabilities", minetest.json_encode({
        full_punch_interval=1.0
}))


I'm not sure, however.
Lua is great!
List of my mods
I like singing. I like dancing. I like ... niyummm...
 

User avatar
davidthecreator
Member
 
Posts: 319
Joined: Mon Aug 18, 2014 19:48
Location: Lithuania
GitHub: daviddoesminetest
In-game: DavidDoesMinetest

Re: How do I set item's "tool_capabilities" from item meta?

by davidthecreator » Mon Jul 15, 2019 19:22

orwell wrote:From what I know, the item meta string expects a string in the JSON format. You can generate this by using
Code: Select all
local meta = itemstack:get_meta()
meta:set_string("tool_capabilities", minetest.json_encode({
        full_punch_interval=1.0
}))


I'm not sure, however.

Didn't work... But thanks...
 

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

Re: How do I set item's "tool_capabilities" from item meta?

by rubenwardy » Mon Jul 15, 2019 19:39

You should use the set_tool_capabilities(tool_capabilities) method

Code: Select all
local caps = stack:get_tool_capabilities()
stack:get_meta():set_tool_capabilities(caps)


Also remember that stacks are a copy, not a reference. You need to update the slot in the inventory
 

User avatar
davidthecreator
Member
 
Posts: 319
Joined: Mon Aug 18, 2014 19:48
Location: Lithuania
GitHub: daviddoesminetest
In-game: DavidDoesMinetest

Re: How do I set item's "tool_capabilities" from item meta?

by davidthecreator » Tue Jul 16, 2019 15:56

rubenwardy wrote:You should use the set_tool_capabilities(tool_capabilities) method

Code: Select all
local caps = stack:get_tool_capabilities()
stack:get_meta():set_tool_capabilities(caps)


Also remember that stacks are a copy, not a reference. You need to update the slot in the inventory


Thanks, it worked.
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 8 guests