[SOLVED]get_item_group not returning correct value

Post Reply
KCoombes
Member
Posts: 427
Joined: Thu Jun 11, 2015 23:19
In-game: Knatt
Location: SW Florida, USA

[SOLVED]get_item_group not returning correct value

by KCoombes » Post

Line 872-880:

Code: Select all

Example: `minetest.get_item_group(name, group)` has been implemented as:

    function minetest.get_item_group(name, group)
        if not minetest.registered_items[name] or not
                minetest.registered_items[name].groups[group] then
            return 0
        end
        return minetest.registered_items[name].groups[group]
    end
Do I need to copy this function into my script for minetest.get_item_group(name,group) to work? I have tried repeatedly to get the group rating (value) of a custom group, and it consistently returns 0 (zero), when the rating should be 1 or 20 (2 different keys).
Last edited by KCoombes on Tue Apr 24, 2018 00:54, edited 1 time in total.

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: lua_api.txt @ guthub question

by rubenwardy » Post

No, it's just telling you how it should work. What code are you using for that? It should look like this:

Code: Select all

minetest.get_item_group("default:stone", "cracky")
note there's no "group:"
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

KCoombes
Member
Posts: 427
Joined: Thu Jun 11, 2015 23:19
In-game: Knatt
Location: SW Florida, USA

Re: lua_api.txt @ guthub question

by KCoombes » Post

rubenwardy wrote:No, it's just telling you how it should work. What code are you using for that? It should look like this:

Code: Select all

minetest.get_item_group("default:stone", "cracky")
note there's no "group:"
local used = minetest.get_item_group(wname, "tool")
where wname was previously defined as the wielded item's name (ie default:pick_steel)

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: lua_api.txt @ guthub question

by rubenwardy » Post

try placing print(dump(wname)) above the get_item_group and checking that it is actually the right value, a string
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

KCoombes
Member
Posts: 427
Joined: Thu Jun 11, 2015 23:19
In-game: Knatt
Location: SW Florida, USA

Re: lua_api.txt @ guthub question

by KCoombes » Post

rubenwardy wrote:try placing print(dump(wname)) above the get_item_group and checking that it is actually the right value, a string
No, that's the problem - here's my code:

Code: Select all

local used = minetest.get_item_group(wname, "tool")
print(used)
local toollvl = tonumber(minetest.get_item_group(wield, "toollvl"))
print (toollvl)
Results (in terminal window)
0 (should be 1)
0 (should be 20)

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: lua_api.txt @ guthub question

by rubenwardy » Post

please can you do print(dump(wname)) above and make sure it's actually the right string
and also print(dump(minetest.registered_items[wname]))
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

KCoombes
Member
Posts: 427
Joined: Thu Jun 11, 2015 23:19
In-game: Knatt
Location: SW Florida, USA

Re: lua_api.txt @ guthub question

by KCoombes » Post

rubenwardy wrote:please can you do print(dump(wname)) above and make sure it's actually the right string
and also print(dump(minetest.registered_items[wname]))
Ok, copy/pasted both print(dump) lines.

Terminal results:
"default:pick_steel"
<
inventory_image = "default_tool_steelpick.png",
stack_max = 1,
name = "default:pick_steel",
mod_origin = "default",
sound = <
breaks = "default_tool_breaks">,
type = "tool",
tool_capabilities = <
full_punch_interval = 1,
groupscaps = <cracky = <
times = <
4,
1.6,
0.8
>,
uses = 20,
maxlevel = 2
>
>,
groups = <
toollvl = 20,
tool = 1
>,
max_drop_level = 1,
damage_groups = <
fleshy = 4
>
>,
description = "Steel Pickaxe"
>
0
0

The last two results (zeros), should have been the ratings of group 'tool' and 'toollvl', ie 1 and 20 respectively.

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: lua_api.txt @ guthub question

by rubenwardy » Post

Here's your dump correctly indented:

Code: Select all

{
	inventory_image = "default_tool_steelpick.png",
	stack_max = 1,
	name = "default:pick_steel",
	mod_origin = "default",
	sound = {
		breaks = "default_tool_breaks"
	},
	type = "tool",
	tool_capabilities = {
		full_punch_interval = 1,
		groupscaps = {
			cracky = {
				times = {
					4,
					1.6,
					0.8
				},
				uses = 20,
				maxlevel = 2
			}
		},
		groups = {
			toollvl = 20,
			tool = 1
		},
		max_drop_level = 1,
		damage_groups = {
			fleshy = 4
		}
	},
	description = "Steel Pickaxe"
}
You've defined groups in the wrong place. It needs to be a top level key, not part of the tool_capabilities, like so:

Code: Select all

{
	inventory_image = "default_tool_steelpick.png",
	stack_max = 1,
	name = "default:pick_steel",
	mod_origin = "default",
	sound = {
		breaks = "default_tool_breaks"
	},
	type = "tool",
	groups = {
		toollvl = 20,
		tool = 1
	},
	tool_capabilities = {
		full_punch_interval = 1,
		groupscaps = {
			cracky = {
				times = {
					4,
					1.6,
					0.8
				},
				uses = 20,
				maxlevel = 2
			}
		},
		max_drop_level = 1,
		damage_groups = {
			fleshy = 4
		}
	},
	description = "Steel Pickaxe"
}
(DON'T just copy this, but instead move the groups bit to the top level of the definition, like next to description. Also make sure you use correct indentation)
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

KCoombes
Member
Posts: 427
Joined: Thu Jun 11, 2015 23:19
In-game: Knatt
Location: SW Florida, USA

Re: get_item_group not returning correct value

by KCoombes » Post

Sorry about that - the client terminal window doesn't allow copying, so I had to type it out by hand. Looking at where I edited tools.lua, I now see exactly what you mean - once again, Rubenwardy to the rescue!

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests