inv:contains_item

Rochambeau
Member
 
Posts: 68
Joined: Tue Sep 23, 2014 11:37

inv:contains_item

by Rochambeau » Sun Jul 26, 2015 19:11

Hi there,

currently I'm working on a little mod that places a mts schem when the player reaches a certain location and has a specific item in his inventory.

I read the inventory in the following section:

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_globalstep(function(dtime)
   timer = timer + dtime;
   if timer >= 5 and built == 0 then
      local players = minetest.get_connected_players()

      for i, player in ipairs(players) do
         inv = player:get_inventory()
         if inv:contains_item("main", build_item) then
            minetest.chat_send_all("yep")

            local pos = player:getpos()
            minetest.chat_send_all(minetest.pos_to_string(pos))
                 local dist = ((pos.x-target.x)^2 + (pos.z-target.z)^2)^0.5
            minetest.chat_send_all(dist)
            if dist < 10 then
               spawn_cave(pos)
               set_built()
               display_message(player)

            end
         else
                  minetest.chat_send_all("nope")
   
         end
      end

      timer = 0
   end
end)


My problem is, that even if the build_item is removed from inventory ("itemstack:take_item();return itemstack"), the
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", build_item)

still returns "true".
What am I doing wrong?

Meanwhile I have a workaround by checking "itemstack == 0" but what if the player has more then one stack?
 

Rochambeau
Member
 
Posts: 68
Joined: Tue Sep 23, 2014 11:37

Re: inv:contains_item

by Rochambeau » Tue Jul 28, 2015 05:21

Well, after some research I solved the problem myself. :-)

My mod consists of two lua files; init.lua (of course) and build.lua (which contains the code from OP).

build_item is defined in init.lua as local and is obviously not accessible from build.lua. So the variable build_item in build.lua is nil and the method inv:contains_item returns true as long as the player inventory has free spaces.

As a solution I see four options:

1. pass build_item to build.lua
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
assert(loadfile("file.lua"))(args)

2. define build_item in init.lua as global
3. define local build_item in build.lua with same content
4. substitute build_item with actual string "testmod:testitem" (it's only used once in build.lua)

What would be the most elegant/runtime efficient option?
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: inv:contains_item

by Hybrid Dog » Tue Jul 28, 2015 17:37

 

Sokomine
Member
 
Posts: 2980
Joined: Sun Sep 09, 2012 17:31

Re: inv:contains_item

by Sokomine » Wed Jul 29, 2015 23:55

Rochambeau wrote:build_item is defined in init.lua as local and is obviously not accessible from build.lua. So the variable build_item in build.lua is nil and the method inv:contains_item returns true as long as the player inventory has free spaces.

The usual approach is to create a global variable (an empty table) named the same way the mod is named and store all that's needed in there. If you want to, you can set it back to nil afterwards.
A list of my mods can be found here.
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 5 guests

cron