Jump to content

How to count objects using LUA script


kills686
 Share

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!

Link to comment
Share on other sites

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.

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

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.

  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

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.

 Share

×
×
  • Create New...