Page 1 of 1

CHeck if node in inventory?

PostPosted: Sat Jun 18, 2016 15:41
by azekill_DIABLO
how to know if an item is in the inventory of the player?

EDIT:888 post!

Re: CHeck if node in inventory?

PostPosted: Sat Jun 18, 2016 18:32
by rubenwardy

Re: CHeck if node in inventory?

PostPosted: Sun Jun 19, 2016 10:36
by azekill_DIABLO
ok thanks! i'm supposed to use

if not inv:contains_item(listname, stack) then
print("Item not in inventory!")
end

but am i supposed to replace something by the name of the item?

Re: CHeck if node in inventory?

PostPosted: Sun Jun 19, 2016 17:19
by azekill_DIABLO
CAN SOMEONE HELP ME?

Re: CHeck if node in inventory?

PostPosted: Sun Jun 19, 2016 17:24
by rubenwardy
This is how to check for default:dirt in the player's main inventory:

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 inv:contains_item("main", ItemStack("default:dirt 99")) then
   print("Inventory contains at least 99 dirt!")
else
   print("Inventory doesn't contain 99 dirt (either less than 99, or no dirt)!")
end


You should read that chapter and the item stack chapter

Re: CHeck if node in inventory?

PostPosted: Sun Jun 19, 2016 18:01
by azekill_DIABLO
ok thanks!i'm not english so i don't understand everything...sry

Re: CHeck if node in inventory?

PostPosted: Sun Jun 19, 2016 18:02
by azekill_DIABLO
it says that inv is a nil value.Why?

Re: CHeck if node in inventory?

PostPosted: Sun Jun 19, 2016 18:53
by everamzah
local inv = player:get_inventory() or perhaps minetest.get_inventory().

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 10:54
by azekill_DIABLO
ok thx i'm dumb!

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 11:07
by azekill_DIABLO
game crashes without any debug :\

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
minetest.register_craftitem("bandage:bandage", {
   description = "Bandage",
   inventory_image = "bandage_bandage.png",

})

local inv = minetest.get_inventory("main")

if inv:contains_item("main", ItemStack("default:dirt 99")) then
   print("Inventory contains at least 99 dirt!")
else
   print("Inventory doesn't contain 99 dirt (either less than 99, or no dirt)!")
end

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 11:29
by azekill_DIABLO
lol

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 11:42
by azekill_DIABLO
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
minetest.register_craftitem("bandage:bandage", {
   description = "Bandage",
   inventory_image = "bandage_bandage.png",
   --i'm doing thing wrong :P
   minetest.register_on_player_hpchange(function(player, hp_change)
      function minetest.get_content_id(...)
         if hp_change>0 then
         user:set_hp(hp + 2)
         itemstack:take_item()
         end
      end
   end
)})


this is not working at all!it does not do anything!

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 11:44
by everamzah
minetest.register_on_hpchange() is not an item definition, and does not belong inside of minetest.register_craftitem().

Additionally, you're checking for hp_change greater than 0, but when it's damage, it's less than zero (a negative number).

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 14:38
by rubenwardy
Wow, you're doing everything wrong :P What are you even trying to do?

The following code will consume a bandage and stop a hp decrease if a bandage is in the inventory:

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
minetest.register_craftitem("bandage:bandage", {
   description = "Bandage",
   inventory_image = "bandage_bandage.png",
})

minetest.register_on_player_hpchange(function(player, hp_change)
    if hp_change >= 0 then
        return hp_change
    end

    local inv = player:get_inventory()
    local itemstack = inv:remove_item("main", ItemStack("bandage:bandage"))
    if itemstack:get_count() == 1 then
        -- cancel hp change
        return 0
    else
        -- no bandages, don't cancel
        return hp_change
    end
end, true)

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 15:08
by azekill_DIABLO
HAHA! an error!!!
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 inv = player:get_inventory()
   local itemstack = inv:remove_item("main", ItemStack("bandage:bandage"))
   if itemstack:get_count() == 1 then

it's ITEMstack!!!

no seriously thanks! i'm really doing things wrong :P

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 15:43
by rubenwardy
Ah, thanks for noticing that

Re: CHeck if node in inventory?

PostPosted: Mon Jun 20, 2016 15:48
by azekill_DIABLO
like that i'm not so nooby