Lloyd Logan Posted January 6, 2013 Share Posted January 6, 2013 Hey, How would i create a script so that math.random(<--- I think its that) chooses from an array of Co-ordinates? I know all the onPlayerWasted and stuff, but how would i make it the math.random chooses from a list of Co-ordinates? addEventHandler( "onPlayerWasted", getRootElement( ), function() setTimer( spawnPlayer, 2000, 1, source, 2745.5830078125, -1608.7109375, 290.84722900391 ) --How would I make math.random choose from a list of co-ordinates? end ) Link to comment
abu5lf Posted January 6, 2013 Share Posted January 6, 2013 (edited) aPos = { { 0, 0, 0 }; { 10, 10, 10 }; } local aX, aY, aZ = unpack( aPos[ math.random( 1, #aPos ) ] ) Edited January 6, 2013 by Guest Link to comment
Lloyd Logan Posted January 6, 2013 Author Share Posted January 6, 2013 aPos = { { 0, 0, 0 }; { 10, 10, 10 }; } local x, y, z = unpack( aPos[ math.random( 1, #aPos ) ] ) Thanks, could you explain to me what the "#" does before aPos? Thanks Lloyd Link to comment
abu5lf Posted January 6, 2013 Share Posted January 6, 2013 # - get length of table. You're welcome. Link to comment
Anderl Posted January 6, 2013 Share Posted January 6, 2013 You can also use # for getting the length of a string. Link to comment
Lloyd Logan Posted January 7, 2013 Author Share Posted January 7, 2013 You can also use # for getting the length of a string. So Anderl, in one of my previous topics, you said "Array" would the aPos be an array? Is that what it is? And would the script work without the #? Thanks again guys Link to comment
GhostXoP Posted January 7, 2013 Share Posted January 7, 2013 In the script, the length of the array is needed. So # was used to reach that goal. aPos is an array. Array = {5, 5, 5} #String OR #array makes lua replace that value at its point in the script with the current size of the box of data its next to. (In C id just call it an area of memory) Since Lua is Dynamically typed, # can determine for it self what kind of data types are in the variable its next to. if h = string then return amount of bytes. if its an array, return amount of elements or objects. Arrays can be ever growing as well. Array[4] = 5 would now add [4] = 5 to the array as well. They can easily be deleted too by doing Array[4] = nil. Arrays are objects themselves too. In C++ you have classes, in Lua you can substitute that with Arrays. For more information http://lua-users.org/wiki/TutorialDirectory Link to comment
Lloyd Logan Posted January 7, 2013 Author Share Posted January 7, 2013 In the script, the length of the array is needed. So # was used to reach that goal. aPos is an array. Array = {5, 5, 5} Arrays can be ever growing as well. Array[4] = 5 would now add [4] = 5 to the array as well. They can easily be deleted too by doing Array[4] = nil. For more information http://lua-users.org/wiki/TutorialDirectory Ahh thank you! Link to comment
GhostXoP Posted January 7, 2013 Share Posted January 7, 2013 Please, Give that link a good skim. Twice even, i tell students to do this and the ones who do excel in the language. Link to comment
Lloyd Logan Posted January 7, 2013 Author Share Posted January 7, 2013 Please, Give that link a good skim. Twice even, i tell students to do this and the ones who do excel in the language. Well Ghost, I can honestly say that I have only read it once, (it's too late for me to read it again, I also read the in depth tutorials) but I honestly feel I understand so much more about Lua, and when I was confused about the Arrays, I feel I know them inside out, bu lt once again the Link has improved me so much in the last hour or so and that's me read it for the first time! Thank you! Link to comment
GhostXoP Posted January 7, 2013 Share Posted January 7, 2013 No problem, my job here is to help teach others "how" to learn. Not, teach them what they want to learn. This helps users learn on their own, and saves them time asking questions when they can figure it out on their own by experience. Link to comment
myonlake Posted January 7, 2013 Share Posted January 7, 2013 No problem, my job here is to help teach others "how" to learn. Not, teach them what they want to learn. This helps users learn on their own, and saves them time asking questions when they can figure it out on their own by experience. Or actually, it's not a job of yours, it's just a thing/hobby of yours. Unless you actually get paid Link to comment
GhostXoP Posted January 7, 2013 Share Posted January 7, 2013 Jealousy is a bitter taste, isn't it?. Your miss the idea of what a job is, a job is not always something you get paid for, my gain from this (or what you would call, pay) is the idea that others are learning. Please bomb a different thread with your off topic suggestions to the definition of what a word is. Link to comment
50p Posted January 7, 2013 Share Posted January 7, 2013 Stop arguing or solve the problems via PM. Keep the forum clean please. Question has been answered so no need to go offtopic. 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