Jump to content

random marker


Recommended Posts

Posted

can any of you brainics tell me why this script doesn't give me the coordinates of a random marker??

    local markers = getElementsByType("marker") 
    local x,y,z = getElementPosition(markers[math.random(#markers)]) 

debug tells me that the argument passed to random is nil. There are definitely 27 markers on the map (created with map editor). Maybe I am calling getElementsByType before the map objects have loaded??

Posted

It's a table all what you need is

for i , v in pairs ( markers) do 
     local x,y,z = getElementPosition(v) 
     -- You code here 
end  

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted

that would give me the coordinates of each marker. It still doesn't solve how to pick one of those markers to spawn at or whatever.

I solved the problem. I included a minimum random argument along with a maximum argument. I believe my original code above would sometimes randomly throw out a zero and cause the code to break. I replaced the code with this:

    local markers = getElementsByType("marker") 
    local x,y,z = getElementPosition(markers[math.random(1,#markers)]) 

all is fine! I randomly spawn at any one of my 27 markers!

Thanks for trying to help. Big love.

Posted
spoke too soon! Problem still happens sporadically. #markers is sometimes throwing out nil when I restart my resource.
if markers then  
  
end  

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted

so I should enter a while loop until "markers" are true then build the table. Why are the markers I have placed on the map in the map editor not showing up when i start my server? I had this working yesterday!! LOL

Posted

It appears that my problem was due to the fact that I was loading an old version of my map file that did not have any markers placed inside it! I need to work on my version control.

My function now works properly and reliably. It is a bit clunkier than I would like so any tips or comments as to how I may be able to clean it up a little would be most greatly appreciated.

I still couldn't access a table of markers by indexing directly with a math.random call within square brackets

(i.e. randomMarker = marker[math.random(1,#marker)]). I ended up using a for loop to iterate through ipairs as usual.

function warper() 
     
    local player = getPlayerName(source) 
    local vehicleID = 0 
    local x,y,z = 0 
     
    if player == "pilot" then 
        --pilot gets a police maverick helicopter 
        vehicleID = 497 
    elseif player == "patrol1" or player == "patrol2" then 
        --patrolmen get a police car 
        vehicleID = 596 
    else 
        --bandits get a taxi 
        vehicleID = 438 
    end 
     
    local markers = getElementsByType("marker") 
    local randomIndex = math.random(1,#markers) 
    for i, marker in ipairs (markers) do 
        if player == "pilot" and i == 26 then 
            x,y,z = getElementPosition(marker) 
        elseif player ~= "pilot" and i == randomIndex then 
            x,y,z = getElementPosition(marker) 
        end 
        --make all markers invisible 
        setElementVisibleTo(marker,root,false) 
    end 
    z = z + 0.5 --make sure vehicle is created above ground 
    local newVehicle = createVehicle ( vehicleID, x, y, z) 
        --only bandits can be damaged 
    if player ~= "bandit" then 
    setVehicleDamageProof(newVehicle,true) 
    end 
     
    spawnPlayer(source,x,y,z+20) 
    warpPedIntoVehicle(source,newVehicle) 
    fadeCamera(source,true) 
end 
  
addEvent("onWarp",true) 
addEventHandler("onWarp",getRootElement(),warper) 
addEventHandler("onPlayerJoin",getRootElement(),warper) 

Posted

I guess:

Server files load first, so the markers may not be created at first.

You may want to use loadMapData at starting of the resource.

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