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) 

s.epicrow.com:22003.png

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 

?

s.epicrow.com:22003.png

Posted

ipairs loop only number indexes "start from 1 index " and only in order but pairs loop all.

Failure is simply an opportunity to begin again more intelligently - Henry Ford

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.

Failure is simply an opportunity to begin again more intelligently - Henry Ford

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.

s.epicrow.com:22003.png

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!

Failure is simply an opportunity to begin again more intelligently - Henry Ford

Posted
I'm not using pairs() or ipairs()...

it's just an advice so yes the last code is the most efficient way possible.

Failure is simply an opportunity to begin again more intelligently - Henry Ford

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