Jump to content

Detecting Inventories/Containers


That Homeless Guy

Recommended Posts

I was wondering how you detect an inventory such as a cabinet or fridge on the ground.
I want to be able to test to see if there is an inventory on the tile I have clicked on.

Edit: Assume I have the grid co-ordinates in world space and I want to derive from the grid co-ordinates if there are any containers in that space.

Link to comment
Share on other sites

I think this can be done with the OnFillWorldObjectContextMenu event. You should be able to iterate through the worldObjects parameter and check if one of the objects is a container.

Here is how the lockpicking mod checks for doors on right-clicks:

https://github.com/cyberbobjr/pz-lockpicking-mod/blob/master/RMLockpickingMod/media/lua/client/UI/UIDoorMenu.lua

Also search through the vanilla .lua files and look for the "WorldContextMenu" (or something like that - can't remember the full name atm).

Link to comment
Share on other sites

I think this can be done with the OnFillWorldObjectContextMenu event. You should be able to iterate through the worldObjects parameter and check if one of the objects is a container.

Here is how the lockpicking mod checks for doors on right-clicks:

https://github.com/cyberbobjr/pz-lockpicking-mod/blob/master/RMLockpickingMod/media/lua/client/UI/UIDoorMenu.lua

Also search through the vanilla .lua files and look for the "WorldContextMenu" (or something like that - can't remember the full name atm).

Thanks

I see how it does this but that is on right click and doesn't actually look for containers. I am using left click to test a tile with and I am specifically looking for containers. Neither of which this does, I do not wish to create a context menu either. It's a step in the right direction but not quite what I'm looking for.

Link to comment
Share on other sites

I that case you'll need a reference to the world / cell and then can do something like getGridSquare(sx, sy, sz). That should work. Look through the Lua-sources - I'm sure you'll find an example for something like this (the world context menu code is a good place to start).

Link to comment
Share on other sites

I that case you'll need a reference to the world / cell and then can do something like getGridSquare(sx, sy, sz). That should work. Look through the Lua-sources - I'm sure you'll find an example for something like this (the world context menu code is a good place to start).

This is actually how I gather the grid position but I am wondering is there a function like IsContainer(obj) or IsContainerHere(x,y,z) and so forth?

Link to comment
Share on other sites

I don't think so, but I am not 100% sure. You could look through the javadocs.

I'd try something like this:

local objects = square:getObjects()for i = 0, #objects - 1 do    if instanceof(objects[i], 'ItemContainer') then -- Or was it InventoryContainer?? :S        return true;    endend
Link to comment
Share on other sites

I don't think so, but I am not 100% sure. You could look through the javadocs.

I'd try something like this:

local objects = square:getObjects()for i = 0, #objects - 1 do    if instanceof(objects[i], 'ItemContainer') then -- Or was it InventoryContainer?? :S        return true;    endend

Perfect I'll try it out and see what we get.

Link to comment
Share on other sites

I don't think so, but I am not 100% sure. You could look through the javadocs.

I'd try something like this:

local objects = square:getObjects()for i = 0, #objects - 1 do    if instanceof(objects[i], 'ItemContainer') then -- Or was it InventoryContainer?? :S        return true;    endend

It keeps throwing attempt to call nil here no matter how I try and iterate a loop

for i = 0, #objects - 1 do

I have tried

for i = 0,table.getn(objects)-1 do

I can't seem to get this to run.

I'd kill for a real debugger and break points

Link to comment
Share on other sites

Try getWorldObjects?

 

Also check this out: http://projectzomboid.com/modding/zombie/iso/IsoGridSquare.html

 

It's a bit outdated, but you should be able to find what you need there.

Thank you I don't know if that helped but I'm using it anyway I explored the game files for getWorldObjects() and found that the devs use obj:size() -1 to iterate through tables.

Like so.

TTG_Click.isInventory = function(square)    local objects = square:getWorldObjects();    if objects ~= nil then        for i = 0,objects:size()-1 do            if instanceof(objects[i], 'ItemContainer') then -- Or was it InventoryContainer?? :S                return true;            end        end    endend

I have found that neither ItemContainer nor InventoryContainer yield a true response but I'll continue to search.

Edit: I have done some searching and found that InventoryContainer appears to be the correct one. (As per the code that spawns loot into containers) I cannot seem to get a true though and the conditional for testing is the same as far as I can tell. More research needed methinks.

Link to comment
Share on other sites

Final solution. Worked it out by trawling the game code.

TTG_Click.isInventory = function(square)    for i = 0,square:getObjects():size()-1 do        local obj = square:getObjects():get(i);        if obj and obj:getContainer() then            return true;        end    end    end

Thank you RoboMat for all your help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...