Page 1 of 1

How do you check the inventory through lua? [Solved]

PostPosted: Sun Sep 02, 2012 13:48
by InfinityProject
I am making a block that when you punch it, something is removed from your inventory and something else is added. But I always get a "attempt to index global 'player' (a nil value)" bug.

My code:

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
if player:get_inventory():contains_item("main", "cyro:cmatter") then
        player:get_inventory():remove_item("main", "cyro:cmatter") do
                player:get_inventory():add_item("main", :cyro:kyro_matter")

PostPosted: Sun Sep 02, 2012 14:12
by jin_xi
if that's in a node definitions on_punch() function use puncher instead of player...

PostPosted: Sun Sep 02, 2012 14:22
by InfinityProject
Tried that but then it says the same thing as before but with puncher instead of player. My complete code:
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
function corruptor (pos, node, puncher)
    if
      node.name == 'cyro:corruptor'
    then
      corrupt (pos, node)
    end
end
minetest.register_on_punchnode(corruptor)

function corrupt (pos, node)
    if puncher:get_inventory():contains_item("main", "cyro:cmatter") then
        puncher:get_inventory():remove_item("main", "cyro:cmatter") do
        puncher:get_inventory():add_item("main", "cyro:kyro_matter")
        end
    end
end

PostPosted: Sun Sep 02, 2012 16:49
by sfan5
InfinityProject wrote:Tried that but then it says the same thing as before but with puncher instead of player. My complete code:
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
function corruptor (pos, node, puncher)
    if
      node.name == 'cyro:corruptor'
    then
      corrupt (pos, node)
    end
end
minetest.register_on_punchnode(corruptor)

function corrupt (pos, node)
    if puncher:get_inventory():contains_item("main", "cyro:cmatter") then
        puncher:get_inventory():remove_item("main", "cyro:cmatter") do
        puncher:get_inventory():add_item("main", "cyro:kyro_matter")
        end
    end
end

Look at this:
function corruptor (pos, node, puncher)
and then at this:
function corrupt (pos, node)
Puncher is missing in the 2nd definition
So, you need to change "function corrupt (pos, node)" and "corrupt (pos, node)" to "function corrupt (pos, node, puncher)" and "corrupt (pos, node, puncher)"

PostPosted: Sun Sep 02, 2012 19:17
by InfinityProject
Thank you so much!