[SOLVED]get_item_group not returning correct value

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

[SOLVED]get_item_group not returning correct value

by KCoombes » Thu Apr 05, 2018 21:36

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: 5244
Joined: Tue Jun 12, 2012 18:11
Location: United Kingdom
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: lua_api.txt @ guthub question

by rubenwardy » Thu Apr 05, 2018 21:37

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:"
 

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

Re: lua_api.txt @ guthub question

by KCoombes » Thu Apr 05, 2018 22:18

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: 5244
Joined: Tue Jun 12, 2012 18:11
Location: United Kingdom
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: lua_api.txt @ guthub question

by rubenwardy » Thu Apr 05, 2018 22:28

try placing print(dump(wname)) above the get_item_group and checking that it is actually the right value, a string
 

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

Re: lua_api.txt @ guthub question

by KCoombes » Thu Apr 05, 2018 22:57

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: 5244
Joined: Tue Jun 12, 2012 18:11
Location: United Kingdom
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: lua_api.txt @ guthub question

by rubenwardy » Thu Apr 05, 2018 23:39

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]))
 

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

Re: lua_api.txt @ guthub question

by KCoombes » Fri Apr 06, 2018 00:30

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: 5244
Joined: Tue Jun 12, 2012 18:11
Location: United Kingdom
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: lua_api.txt @ guthub question

by rubenwardy » Fri Apr 06, 2018 00:43

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)
 

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

Re: get_item_group not returning correct value

by KCoombes » Fri Apr 06, 2018 00:53

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!
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 1 guest