Sign in to follow this  
hyiltiz

Find entity anywhere (including inventories)?

Recommended Posts

hyiltiz    488

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.

Share this post


Link to post
Share on other sites
hyiltiz    488
7 hours ago, ptr said:

It will find them, but you can only goto a entity with a position.

What entities do not have a position? Anything in inventories? 

Share this post


Link to post
Share on other sites
ptr    41

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.

Share this post


Link to post
Share on other sites
hyiltiz    488

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.

Share this post


Link to post
Share on other sites
ptr    41

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.

Share this post


Link to post
Share on other sites
DarkXero    2886

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

 

Share this post


Link to post
Share on other sites
cezarica    513

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?

Share this post


Link to post
Share on other sites
ptr    41
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 ?

Share this post


Link to post
Share on other sites
cezarica    513

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.

Share this post


Link to post
Share on other sites
cezarica    513

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

Share this post


Link to post
Share on other sites
ptr    41

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.

Share this post


Link to post
Share on other sites
cezarica    513

By count I meant to see how many times a certain prefab exists in the world laying on the ground. For instance if there are more than let's say 10 rot I want to remove them all.

Share this post


Link to post
Share on other sites
ptr    41

It will be better to store them in a local table, otherwise you still need to do another Ents loop after counting. And counting table elements does not require loop, simply #tablename.

Share this post


Link to post
Share on other sites
cezarica    513

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

Share this post


Link to post
Share on other sites
ptr    41

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

 

Share this post


Link to post
Share on other sites
cezarica    513

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.

Share this post


Link to post
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
Sign in to follow this