Jump to content

How to create unique marker?


Albert221

Recommended Posts

Hello! I want to create a surprises system. On resource start it will generate x markers with coordinates from list. It have to be unique, no one markers should have the same position. And when player hit this marker, it will destroy itself and create another marker. Sorry for my bad english. My code:

local surprisesCoords = { 
    {-2090.885, 1154.33, 67.205}, 
    {-1705.405, 1012, 17.585}, 
    {-2211.785, 622.69, 44.24}, 
    {-2186.19, 697.065, 53.89}, 
    {-2672.855, 919.92, 87.11} 
} 
local surprisesMarkers = {} 
  
local atOneTimeAmount = 2 
local allAmount = 0 
for _ in pairs(surprisesCoords) do allAmount = allAmount + 1 end 
  
function markerExists(marker) 
    for _, value in pairs(surprisesMarkers) do 
        if marker.position.x == value.position.x and marker.position.y == value.position.y then 
            return true 
        end 
    end 
  
    return false 
end 
  
function createSurprise() 
    local coords = surprisesCoords[math.random(allAmount)] 
    local marker = Marker(coords[1], coords[2], coords[3], 'corona', 1, 255, 255, 0) 
  
    if not markerExists(marker) then 
        table.insert(surprisesMarkers, marker) 
    else 
        marker:destroy() 
        createSurprise() 
    end 
end 
  
addEventHandler('onResourceStart', resourceRoot, function() 
    for i = 1, atOneTimeAmount do 
        createSurprise() 
    end 
end) 
  
addEventHandler('onMarkerHit', resourceRoot, function(element) 
    if not element.type == 'player' then return end 
  
    for index, marker in ipairs(surprisesMarkers) do 
        if marker == source then 
            source:destroy() 
            table.remove(surprises, index) 
            break 
        end 
    end 
  
    createSurprise() 
end) 

Link to comment
  • Moderators

Use the Lua functions repeat until.

An example:

  
local curIndex, randomI 
local marker 
  
addEventHandler('onMarkerHit', resourceRoot, function(element) 
    if not element.type == 'player' then return end 
    curIndex = tonumber(source:getID()) 
    if not curIndex then return end 
  
    source:destroy() 
    repeat 
        randomI = math.random(#surprisesCoords) 
    until randomI ~= curIndex 
  
    local x,y,z = unpack(surprisesCoords[randomI]) 
    marker = Marker(x, y, z, 'corona', 1, 255, 255, 0) 
    marker:setID(tostring(randomI)) 
end) 

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