Jump to content
kills686

How to count objects using LUA script

Recommended Posts

Hello everyone,

 

I am trying to get a count of how many crates i have but am having a hard time figuring out how to refer to them directly to get a proper count. I want to do this to be able to track crates as i get rid of them and to add in new ones as they are generated.

 

Thanks for any help!

Share this post


Link to post

You could perform a count of world entities and then cycle through confirming name, but i would assume that would be too expensive and might cause a noticeable pause in the game when adding or removing. One way might be to add a script to the crate model that checks for a global table (or creates the table if it doesn't exist) that adds the crates entities to it. Then all you would have to do is count the number of items in the table. When the crate is removed from the scene then the script can use the function Script:Detach() to decrement the table.

Share this post


Link to post

@macklebee I think that would work. Is there anyway i could get an example of the table creating function and the referencing of the specific object? Sorry I'm still pretty green behind the ears.

 

Thanks again for the help!

Share this post


Link to post

I was looping through all entities in the world each update in my map script and didn't notice any fps drops, so I think it's good enough.

Something like this:


for i=0,App.world:CountEntities()-1 do

local entity = App.world:GetEntity(i)

if entity:GetKeyValue("name") == "Crate" then

--this is your crate entity, can count it or delete

end

end

Strange that CountEntities and GetEntity aren't in documentation, they are very important.

Share this post


Link to post

Thank you both for the help! These are great ideas and should ease my struggles tongue.png Also @Genebris I searched the documentation and was looking for those exact ideas but ended up trying count child which of course did not work lol

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...