Jump to content

outputChatBox


Drakath

Recommended Posts

Posted

What is more efficient?

This:

for _,v in ipairs(getElementsByType("player")) do 
outputChatBox("Hello",v) 
end 

or this:

outputChatBox("Hello",root) 

Posted
What is more efficient?

This:

for _,v in ipairs(getElementsByType("player")) do 
outputChatBox("Hello",v) 
end 

or this:

outputChatBox("Hello",root) 

This one, I guess:

for _,v in ipairs(getElementsByType("player")) do 
outputChatBox("Hello",v) 
end 

Another one is 30% slower than this one.

Posted

The element tree from wiki explains why ;)

Tre.png

The chat message are only shown to players but with root as element it has to go through all elements, even those who's not goanna view the chat.

  • MTA Team
Posted

Even faster:

local players = getElementsByType("player") 
for i=1, #players do 
    outputChatBox("Hello", players[i]) 
end 

Posted

So when should I use pairs and when ipairs? Or should I always use pairs instead?

Is it more efficient to use Necktrox's code or:

for _,v in pairs(getElementsByType("player")) do 
outputChatBox("Hello",v) 
end 

?

Posted

as i told you ipairs is for indexed tables, it means every item <<<<>>>> in the table has it's own number: 1 is the index of the first value 2 is the index ot the second's and so on:

local table1 = { 1, 2, 3, 4 } -- First type 
local table2 = { [1] = 1,  [2] = 2, [3] = 3,[4] = 4} -- Second type 
  
  
for i, v in ipairs( table1 ) do --both pairs and ipairs will work in those tables: 
for i, v in pairs( table1 ) do 

but if you have a table like this

local table = { ["Yes"] = 1, [1] = "Table", ["example"] = 2014, [2] = "me", } 
-- ipairs won't work in this case. 

So try to use pairs all the time.

Posted

Alright, thanks everyone.

So as I understood,

local players = getElementsByType("player") 
for i=1, #players do 
    outputChatBox("Hello", players[i]) 
end 

is the most efficient way possible.

Posted

you must read the conclusion :) :

Conclusion:

Don't use pairs() or ipairs() in critical code! Try to save the table-size somewhere and use for i=1,x do!

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