Jump to content

element count


DakiLLa

Recommended Posts

Posted

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?

Posted
#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") 

Do NOT PM ME for help unless invited. - New MTA Script Editor

Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.

Posted

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 ?

Posted
#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.

Projects:

Slothbot | Maximap

Posted

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.

Posted

I already wondered what that for loop was for. :P

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.

Projects:

Slothbot | Maximap

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...