Jump to content

Find entity anywhere (including inventories)?


Recommended Posts

Do c_gonext() find the prefab if it is in some inventory, say a backpack on a player or ground, chest on the ground, lureplant inventory etc.? I understand it doesn't look for entities in other shards so I should try it in each of them separately.

Link to comment
Share on other sites

Well, those entitiesin inventory don't have own positions, but they share the position with the owner, which is called 'local' in the code. Those entities which REALLY don't have positions are special entities like network things.

If you want to find entities in a mod, you can do a for loop on 'Ents', which is a global variable, and find what you want with v.prefab.

Link to comment
Share on other sites

Ah, ok! Is there a documentation of the API of the game, such as all the global variables and helper functions? I did dig into the lua scripts enough to navigate my ways around, but not enough to reason about it without looking at those scripts.

Link to comment
Share on other sites

Sadly, no.

You can find some useful functions about entities in simutil.lua, and special functions used for mods in modutil.lua. The consolecommands.lua can give you some hints on how cheating commands like c_godmode() work.

But if you want to want to write a mod, it will be helpful to begin from reading the code in similar mods. Then you can see what functions they have used and look up for their sourse code by searching the entire 'scripts' folder.

Link to comment
Share on other sites

You can c_gonext to items in dropped backpacks.

Items in players' inventories are off the charts using FindEntities.

You can loop through the global Ents table though (example for axe, and you being the player 1):

for k, v in pairs(Ents) do if v.prefab == "axe" then UserToPlayer(1).Transform:SetPosition(v:GetPosition():Get()) end end

 

Link to comment
Share on other sites

5 hours ago, cezarica said:

And what if I want to see only the stuff that isn't part of some inventory, be it player inventory, his backpack, lureplant, chest, and so on?

check inst.components.inventoryitem==nil or inst.components.inventoryitem.owner==nil ?

Link to comment
Share on other sites

With == nil doesn't seem to be working but dose with ~= nil.

local items = { spoiled_food = true, twigs = true, cutgrass = true, boneshard = true }
for _,v in pairs(Ents) do
    if items[v.prefab] then
        if v.components.inventoryitem ~= nil and v.components.inventoryitem.owner ~= nil then
            print(v.prefab)
        end
    end
end

This is the code I'm using and want to do something to clean the world of junk.

Link to comment
Share on other sites

I want to filter out and get a list of things that aren't part of any container, I mean that are on the ground. From here I want to get the amount of said prefab and eliminate them if are beyond a certain amount specified for each prefab in particular.

From what I read on "Relational Operators" the ~= is exactly what i needed as it " Checks if the value of two operands are equal or not, if values are not equal then condition becomes true."

In my case I want to check if v.components.inventoryitem is nil and v.components.inventoryitem.owner is nil as well.

Now, how do I count them? Apart adding the prefabs I want in a local table and loop trough it and increment a number that is. :)

Edited by cezarica
Link to comment
Share on other sites

But in your code, "v.components.inventoryitem ~= nil" means it can be put into the inventory and "v.components.inventoryitem.owner ~= nil" means it is inside some container or inventory.

Count with some increment directly in the Ents loop? I don't get your idea.

Link to comment
Share on other sites

I tried with:

local items = { spoiled_food = true }
for k, v in pairs(Ents) do
    if items[v.prefab] then
        if v.components.inventoryitem ~= nil and v.components.inventoryitem.owner ~= nil then
            print(string.format("prefab: %s | count: %s", v.prefab, #v))
        end
    end
end

or a condensed command to try in console:

local items = { spoiled_food = true }; for k, v in pairs(Ents) do if items[v.prefab] then if v.components.inventoryitem ~= nil and v.components.inventoryitem.owner ~= nil then print(string.format("prefab: %s | count: %s", v.prefab, #v)); end; end; end;

and the result is:

[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0
[05:47:49]: prefab: spoiled_food | count: 0

What I'm missing? :?

Is there a way to tell if there's a stack or just one?

Edit: Decided to run a little experiment with v:GetPosition():Get() and results are:

[05:52:23]: prefab: spoiled_food | pos: 494
[05:52:23]: prefab: spoiled_food | pos: 494
[05:52:23]: prefab: spoiled_food | pos: 494
[05:52:23]: prefab: spoiled_food | pos: 494
[05:52:23]: prefab: spoiled_food | pos: 494
[05:52:23]: prefab: spoiled_food | pos: 494
[05:52:23]: prefab: spoiled_food | pos: 494
[05:52:23]: prefab: spoiled_food | pos: 819
[05:52:23]: prefab: spoiled_food | pos: 819
[05:52:23]: prefab: spoiled_food | pos: 819
[05:52:23]: prefab: spoiled_food | pos: 819
[05:52:23]: prefab: spoiled_food | pos: 819
[05:52:23]: prefab: spoiled_food | pos: 819
[05:52:23]: prefab: spoiled_food | pos: 800
[05:52:23]: prefab: spoiled_food | pos: 800
[05:52:23]: prefab: spoiled_food | pos: 800
[05:52:23]: prefab: spoiled_food | pos: 800
[05:52:23]: prefab: spoiled_food | pos: 800

Dose this mean that basically i got just 3 stacks somewhere? Sounds a bit impossible to say the least...

I use Point(v.Transform:GetWorldPosition()) to get the exact X, Y and Z coordinates of portal in one of my custom commands but can't get it to work in here for some reason. :S

Edited by cezarica
Link to comment
Share on other sites

There will be only one prefab if they are stacked. You can check prefab.components.stackable.stacksize for number.

BTW, I still don't get the idea of your code

local items = {}
for _, v in pairs(Ents) do
    if v.prefab and (v.components.inventoryitem == nil or v.components.inventoryitem.owner == nil) then
         if not items[v.prefab] then items[v.prefab]={} end
		 table.insert(items[v.prefab],v)
    end
end
for k, v in pairs(items) do
	print(string.format("prefab: %s | count: %s", k, #v))
end

 

Link to comment
Share on other sites

Using the prefab.components.stackable.stacksize seems to do the trick as I wanted.

[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 16
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 40
[08:07:01]: prefab: spoiled_food | count: 30

Needs more testing I guess.

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
 Share

×
  • Create New...