DakiLLa Posted March 15, 2009 Share Posted March 15, 2009 hey all how can i count elements of a specified type on server ? something like this: i have a number of objects but i dont know how many of them i have. How to count them? Link to comment
Gamesnert Posted March 15, 2009 Share Posted March 15, 2009 #amountOfObjects=getElementsByType("object") The getElementsByType function should return a table. And # counts how many indexes a table has. Link to comment
robhol Posted March 15, 2009 Share Posted March 15, 2009 #amountOfObjects=getElementsByType("object") The getElementsByType function should return a table. And # counts how many indexes a table has. Your example doesn't make any sense. It's amountOfObjects=#getElementsByType("object") Link to comment
DakiLLa Posted March 15, 2009 Author Share Posted March 15, 2009 ehm guys..one problem here.. i'm getting something other than amount of objects addCommandHandler ( "test", function ( ) local objects = getElementsByType ( "object" ) objAmount = #objects for k,object in ipairs (objects) do if getObjectModel (object) == 649 then outputChatBox ( "We have "..objAmount.." objects" ) end end end ) if it's (for example) 4 objects i have next: We have 805 objects We have 805 objects We have 805 objects We have 805 objects can you fix my mistakes ? Link to comment
Gamesnert Posted March 15, 2009 Share Posted March 15, 2009 #amountOfObjects=getElementsByType("object") The getElementsByType function should return a table. And # counts how many indexes a table has. Your example doesn't make any sense. It's amountOfObjects=#getElementsByType("object") Kinda bad mistake of mine indeed lol... Anyway, DaK, put "break" (without the quotes) in the line after outputChatBox. Should help. Link to comment
DakiLLa Posted March 15, 2009 Author Share Posted March 15, 2009 ok, thx, it outputs only one message now. But another problem: my example counts all objects on server (thats why i have ~800 objects ) , not only objects with ID 649. How to count objects only with current id? i have getObjectModel line but it placed in wrong place i think. Link to comment
Gamesnert Posted March 15, 2009 Share Posted March 15, 2009 I already wondered what that for loop was for. addCommandHandler ( "test", function ( ) local objects = getElementsByType ( "object" ) local i=0 for k,object in ipairs (objects) do if getObjectModel (object) == 649 then i=i+1 end end outputChatBox ( "We have "..i.." objects with ID 649" ) end ) This should output the amount of objects with ID 649. Link to comment
DakiLLa Posted March 15, 2009 Author Share Posted March 15, 2009 thx a lot, gamesnert, for all help works perfect Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now