JuniorMelo Posted February 11, 2016 Share Posted February 11, 2016 hello, I have a problem in this function, math.random in spawnPlayer I've never heard it, ??? Vector3 ??? Sorry my english function spawn(player) xml = xmlLoadFile("data.xml") if xml then children = xmlFindChild ( xml, "Local", 0 ) if children then for i,v in ipairs(xmlNodeGetChildren(children)) do posX = xmlNodeGetAttribute(v,"posX") posY = xmlNodeGetAttribute(v,"posY") posZ = xmlNodeGetAttribute(v,"posZ") local spawns = {posX,posY,posZ} local rnd = math.random( #spawns ) spawnPlayer( player, spawns[rnd].posX, spawns[rnd].posY, spawns[rnd].posZ, 90 ) fadeCamera(player, true) setCameraTarget(player, player) end xmlUnloadFile(xml) end end end Link to comment
Mr_Moose Posted February 12, 2016 Share Posted February 12, 2016 It means that the spawnPlayer function expects a player and then x,y and z positions as arguments. Now this may seem a bit confusing since you did provided variables for all those arguments, but your variables are empty and therefore nil. Assuming you store a list of coordinates in your data.xml file you may consider to load all that into a table and then spawn the player, what you're doing now is to create a empty table inside a loop, then generate a random number between 1 and 3 (since #spawns = 3) and then spawn the player using coordinates that doesn't exist in your table. Start by defining spawns as a multidimensional table at line 2: local spawns = {{ }} Replace line 10 with: spawns[i] = { } Then fill the table there somehow and skip the rest. Finally place the spawn function and everything below it at the bottom of the function and use the random number generator there to load the coordinates to your spawn function. Link to comment
tosfera Posted February 12, 2016 Share Posted February 12, 2016 To go a bit more into detail, a vector3 position is a three dimensional coordinate which indicates a location in the game. As far as the current games are being built, you got 3 of these vectors in a three dimensional game; x-axis - goes for north - south y-axis - goes for east - west z-axis - goes for the height In 2D games you got.. indeed, 2 vectors. These can be compared to the y-axis and z-axis in a 3D game. Link to comment
JuniorMelo Posted February 12, 2016 Author Share Posted February 12, 2016 It means that the spawnPlayer function expects a player and then x,y and z positions as arguments. Now this may seem a bit confusing since you did provided variables for all those arguments, but your variables are empty and therefore nil. Assuming you store a list of coordinates in your data.xml file you may consider to load all that into a table and then spawn the player, what you're doing now is to create a empty table inside a loop, then generate a random number between 1 and 3 (since #spawns = 3) and then spawn the player using coordinates that doesn't exist in your table.Start by defining spawns as a multidimensional table at line 2: local spawns = {{ }} Replace line 10 with: spawns[i] = { } Then fill the table there somehow and skip the rest. Finally place the spawn function and everything below it at the bottom of the function and use the random number generator there to load the coordinates to your spawn function. you mean like this table is null function spawn(player) local xml = xmlLoadFile("data.xml") spawns = {{}} if xml then for k,v in ipairs (xmlNodeGetChildren(xmlFindChild(xml,"Local",0)))do local posX,posY,posZ = xmlNodeGetAttribute(v,"posX"),xmlNodeGetAttribute(v,"posY"),xmlNodeGetAttribute(v,"posZ") table.insert(spawns, {posX,posY,posZ}) spawns[i] = { posX,posY,posZ } local rnd = math.random(#spawns) spawnPlayer( player, spawns[rnd].posX, spawns[rnd].posY, spawns[rnd].posZ, 90 ) fadeCamera(player, true) setCameraTarget(player, player) end end end Link to comment
Mr_Moose Posted February 12, 2016 Share Posted February 12, 2016 For the first part, yes. But if you intend to use variables in your table you have to assign the data this way: spawns[i] = { posX=posX, posY=posY, posZ=posZ } On line 8. After that you have to move line 9-12 to line 15, right before the last end or your player will spawn in a loop. Link to comment
JuniorMelo Posted February 12, 2016 Author Share Posted February 12, 2016 Thank you all, it helped a lot.. 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