Page 1 of 1

Check node group

PostPosted: Sun Apr 08, 2012 09:49
by neko259
How do I check if some node is in a specific group? I can't find such example in the API reference.

PostPosted: Sun Apr 08, 2012 13:18
by celeron55
You need to check:
1) if the node is registered (otherwise you will fail miserably if the node is unknown)
2) if the node has the group set
3) if the node has the group set to something else than 0 (because 0 is still "not set")

Something like:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local function get_node_group(name, group)
    if not minetest.registered_nodes[name] or not minetest.registered_nodes[name].groups[group] then
        return 0
    end
    return minetest.registered_nodes[name].groups[group]
end


If it returns 0, the node is not in the group (same as rating=0); otherwise the returned value is the rating of the group.

Also one should remember that in Lua the only values that are false are false and nil. 0 is true. 8)

I'll include something like this in the API in the future.

Fun fact: the leaf decay code fails to check if a node is registered and causes crashes for people with unknown nodes. Fixed already in github though.

PostPosted: Sun Apr 08, 2012 13:41
by neko259
celeron55 wrote:You need to check:
1) if the node is registered (otherwise you will fail miserably if the node is unknown)
2) if the node has the group set
3) if the node has the group set to something else than 0 (because 0 is still "not set")

Something like:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local function get_node_group(name, group)
    if not minetest.registered_nodes[name] or not minetest.registered_nodes[name].groups[group] then
        return 0
    end
    return minetest.registered_nodes[name].groups[group]
end


If it returns 0, the node is not in the group (same as rating=0); otherwise the returned value is the rating of the group.

Also one should remember that in Lua the only values that are false are false and nil. 0 is true. 8)

I'll include something like this in the API in the future.

Fun fact: the leaf decay code fails to check if a node is registered and causes crashes for people with unknown nodes. Fixed already in github though.

Thank you. I'll try to use that to check nodes flammability.

PostPosted: Sun Apr 08, 2012 13:54
by neko259
Or maybe I won't. I see you already add fire to minetest_game, so my mod is useless now :)