Jump to content

[Help] ?? Vector3 ?? °_°


JuniorMelo

Recommended Posts

hello, I have a problem in this function, math.random in spawnPlayer

I've never heard it, ??? Vector3 ??? :!::oops:

Sorry my english :|:shock:

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 

5Wzvkp0.png

Link to comment

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

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

iuzai28.png

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

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

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