Jump to content

Random element and loops


Recommended Posts

1. a table?? if so:

unpack(theTable[math.random(#theTable)]) 

2. ok

let's say you want to loop 5 times

for i=0, 5 do 
--code 
end 

let's say you want to loop through a table

for i, v in ipairs(theTable) do 
--code 
end 

looping through connected players:

for i, v in ipairs(getElementsByType('player')) do 
--code 
end 

Link to comment

That's no helping him, is it? He asked for an explanation, not stock unusable code.

To your spawnpoints: Depends on what the parent is of the positions. Is it a table? Then you can use the above code in something like setElementPosition. That function will return all values in the table separated by a comma. So if you only got XYZ positions in your table, the following code should fit:

setElementPosition ( element, unpack(theTable[math.random(#theTable)]) ) 

Loops might be a bit tricky for new users indeed. The most used loops are loops for tables and loops for numbers. Lets start with numbered tables.

  
for i=1, 10 do 
    -- block 
end  

In that code we want to loop all numbers from 1 to 10. This will be done in steps by one, and the var 'i' will store the current number. Now this shouldn't be hard :P

You might also want to use other steps then by one. In the code below we added one extra argument to the loop, indicating how big our steps are. In this case we count from 10 to 1.

for i=10, 1, -1 do 
    -- block 
end 

Now we've had the easy part, we will loop some tables. These are quite different compared to regular tables, as they return two variables in the loop.

Lets begin with the numbered tables. We all know them, table[1] = something. A function like getElementsByType will return such a table. We can loop this table using 'ipairs'.

local allPlayers = getElementsByType ( "player" ) 
for i, v in ipairs ( allPlayers ) do 
    -- block 
end 

As you see we have two variables now: i and v. 'i' will contain the current number being looped, so that's the 1 in table[1]. 'v' will contain the data of table, which is in this case the player. And yes, v is basically the same as table :P

But theres also a second type of tables: The ones using a string as a key. For example table["somekey"] = something. In this case we can't usw ipairs, as ipairs only supports numbers. 'pairs' however supports string AND numbers. And theres another difference, pairs does not sort your table. So 1,2,3 might be looped as 2,3,1.

for k, v in pairs ( sometable ) do 
    -- block 
end 

We replaced the i variable for 'k' but basically contains the same. And once again, v is the same as table[k] :)

Succes ;)

Link to comment

So you got an element containing spawnpoints? See the wiki for getElementChildren, this function returns a table which you can use to get a random spawnpoint. If thats not what youre looking for, please post an example of your elements containing the spawnpoints.

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