World Championship Posted June 30, 2011 Share Posted June 30, 2011 Two things: Let's say I have a "lobby spawnpoints" element with some spawn positions as children. How can I get a random children from this element? Is there any good guide explaining how loops work? I have already visited http://www.lua.org but I can't really understand how they work. Link to comment
JR10 Posted June 30, 2011 Share Posted June 30, 2011 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
Deltanic Posted July 1, 2011 Share Posted July 1, 2011 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 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 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
World Championship Posted July 1, 2011 Author Share Posted July 1, 2011 Do I really need to get all the elements into an array/table? Can't I just use something like an index in tables to get a random element? Link to comment
Deltanic Posted July 1, 2011 Share Posted July 1, 2011 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
World Championship Posted July 2, 2011 Author Share Posted July 2, 2011 I think I got it, thanks. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now