Jump to content

Storing and looping


franku

Recommended Posts

Wassup guys... I've got a cmdHandler.. My question is, is it possible to store whoever executes the commnd in a table and loop through them?

Idea of what I wanna do; I wanna loop through the players that execute a command (like, /spawnMe) and spawn them somewhere else in the map. If i use ipairs, it spawns everybody on the server not only those used the cmd.

Another thing i'd like a brief explanation of is, whats the difference between ipairs and pairs, return and break? Thanks in advance.

Edited by Guest
Link to comment

i think it should be like this im not very sure tho

  
local players_Spawn = {} 
function spawn(source) 
       table.insert(players_Spawn,source) 
       spawnThePlayer() 
end 
addCommandHandler("spawnme",spawn) 
function spawnThePlayer() 
      for _,p in pairs(players_Spawn) do 
            spawnPlayer(...) 
      end 
end 
  

NOT TESTED

Link to comment
i think it should be like this im not very sure tho
  
local players_Spawn = {} 
function spawn(source) 
       table.insert(players_Spawn,source) 
       spawnThePlayer() 
end 
addCommandHandler("spawnme",spawn) 
function spawnThePlayer() 
      for _,p in pairs(players_Spawn) do 
            spawnPlayer(...) 
      end 
end 
  

NOT TESTED

Gonna give it a try tmrw. Will get back with the errors (in case I couldn't fix them) if there were any.

Link to comment

pairs goes through a table without sorting, so you can use it with every table/index.

ipairs first sorts the table by index (only integer), then goes through.

pairs is more safe and fast, you can go through every table with that

ipairs only goes from 1 to the highest index before a index with nil value.

ipairs is something like this:

local table = { ... } 
local i =1 
while table[i] do 
    --- 
    i = i + 1 
end 

Index NOT integer(number) -> pairs

Index integer, but dont want to sort it -> pairs

Index integer, but with nil values between other values -> pairs

Index integer and you want to sort it -> for i=1, #table do

To be honest:

I don't really know why so many guys still use ipairs.

for i=1, #table do is much faster, you just have to check before using if the "table" is really a table and if the table got atleast one value:

local thetable = {} 
if thetable and thetable[1] then 
    for i=1, #thetable do 
  
    end 
end 

I only know 1 only usage for ipairs:

When you want to go through the table from 1 to n and want to remove from the table.

You could still remove from the table, but only when you go through the table from n to 1.

return stops a function and returns something.

Example:

function getPlayerRotation ( player ) 
    local _, _, zr = getElementRotation ( player ) 
    if zr then 
        return zr 
    end 
    return false   -- return false if zr wasn't already returned 
    outputChatBox ( "This outputChatBox will never get executed" ) 
end 
  
local rot = getPlayerRotation ( player ) 

But break just stops a loop (while, for, repeat)

local name = "Bonus" 
local players = getElementsByType ( "player" ) 
for i=1, #players do    -- the loop 
    if getPlayerName ( players ) == name then 
        outputChatBox ( "Player-Name was found" ) 
        break   -- stops the loop 
    end 
end 

Link to comment

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