CHeck if node in inventory?

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

CHeck if node in inventory?

by azekill_DIABLO » Sat Jun 18, 2016 15:41

how to know if an item is in the inventory of the player?

EDIT:888 post!
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: CHeck if node in inventory?

by rubenwardy » Sat Jun 18, 2016 18:32

 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Sun Jun 19, 2016 10:36

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?
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Sun Jun 19, 2016 17:19

CAN SOMEONE HELP ME?
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: CHeck if node in inventory?

by rubenwardy » Sun Jun 19, 2016 17:24

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
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Sun Jun 19, 2016 18:01

ok thanks!i'm not english so i don't understand everything...sry
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Sun Jun 19, 2016 18:02

it says that inv is a nil value.Why?
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
everamzah
Member
 
Posts: 490
Joined: Thu Jan 29, 2015 00:47
GitHub: everamzah
IRC: everamzah
In-game: everamzah

Re: CHeck if node in inventory?

by everamzah » Sun Jun 19, 2016 18:53

local inv = player:get_inventory() or perhaps minetest.get_inventory().
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Mon Jun 20, 2016 10:54

ok thx i'm dumb!
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Mon Jun 20, 2016 11:07

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
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Mon Jun 20, 2016 11:29

lol
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Mon Jun 20, 2016 11:42

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!
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
everamzah
Member
 
Posts: 490
Joined: Thu Jan 29, 2015 00:47
GitHub: everamzah
IRC: everamzah
In-game: everamzah

Re: CHeck if node in inventory?

by everamzah » Mon Jun 20, 2016 11:44

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

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: CHeck if node in inventory?

by rubenwardy » Mon Jun 20, 2016 14:38

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)
Last edited by rubenwardy on Mon Jun 20, 2016 15:42, edited 1 time in total.
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Mon Jun 20, 2016 15:08

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
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: CHeck if node in inventory?

by rubenwardy » Mon Jun 20, 2016 15:43

Ah, thanks for noticing that
 

User avatar
azekill_DIABLO
Member
 
Posts: 3458
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO

Re: CHeck if node in inventory?

by azekill_DIABLO » Mon Jun 20, 2016 15:48

like that i'm not so nooby
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
Hi, my username is azekill_DIABLO and i'm an exelent bug-maker(yeah...i know...i have a bad reputation)

azekill_DIABLO said: Mineyoshi+ABJ+Baggins= TOPIC HIJACKED.
My Mods and Stuff | Voxellar | VoxBox on GITHUB | M.I.L.A Monster engine
WEIRD MODDING CONTEST !!!
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron