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

User avatar
InfinityProject
Member
 
Posts: 1009
Joined: Sat Mar 17, 2012 00:52

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

by InfinityProject » Sun Sep 02, 2012 13:48

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")
Last edited by InfinityProject on Sun Sep 02, 2012 19:17, edited 1 time in total.
 

jin_xi
Member
 
Posts: 165
Joined: Mon Jul 02, 2012 18:19

by jin_xi » Sun Sep 02, 2012 14:12

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

User avatar
InfinityProject
Member
 
Posts: 1009
Joined: Sat Mar 17, 2012 00:52

by InfinityProject » Sun Sep 02, 2012 14:22

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
 

User avatar
sfan5
Member
 
Posts: 3636
Joined: Wed Aug 24, 2011 09:44
GitHub: sfan5
IRC: sfan5

by sfan5 » Sun Sep 02, 2012 16:49

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)"
Mods: Mesecons | WorldEdit | Nuke
Minetest builds for Windows (32-bit & 64-bit)
 

User avatar
InfinityProject
Member
 
Posts: 1009
Joined: Sat Mar 17, 2012 00:52

by InfinityProject » Sun Sep 02, 2012 19:17

Thank you so much!
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 9 guests

cron