Jump to content

random markers .....


Deepu

Recommended Posts

Can you help me with random markers? these markers come altogether

function moon2 () 
      destroyElement(reach) 
      setElementPosition(thePlayer, 660.90002441406, -3290.3999023438, 5) 
      m1 = createMarker(692.7001953125, -3341.599609375, -3, "cylinder", 1, 5, 250, 45) 
      m2 = createMarker(664.09997558594, -3288.8999023438, 4.8000001907349, "cylinder", 0.5, 170, 240, 10) 
      m3 = createMarker(683.90002441406, -3291, 5, "cylinder", 0.5, 170, 240, 10) 
      m4 = createMarker(701.20001220703, -3260.5, 4.0999999046326, "cylinder", 0.5, 170, 240, 10) 
      m5 = createMarker(712.59997558594, -3245.3999023438, 3.7000000476837, "cylinder", 0.5, 170, 240, 10) 
      m6 = createMarker(745.20001220703, -3265.6999511719, 4.8000001907349, "cylinder", 0.5, 170, 240, 10) 
      m7 = createMarker(759.59997558594, -3230.3000488281, 7.0999999046326, "cylinder", 0.5, 170, 240, 10) 
      m8 = createMarker(816.59997558594, -3277.8000488281, 9.1999998092651, "cylinder", 0.5, 170, 240, 10) 
      m9 = createMarker(841, -3328.8999023438, 17.299999237061, "cylinder", 0.5, 170, 240, 10) 
      randomMarker = getElementByID("m"..math.random(2,9)) 
end 
addCommandHandler("moon", moon2) 

the script works but all markers come at the same time and not random

Link to comment

You are not setting the Marker ID's anywhere. You should also create a table with all the markers and loop trough it instead of creating every marker manually.

  
local markers = { 
{692.7001953125,-3341.599609375,-3,"cylinder",1,5,250,45}, 
{664.09997558594,-3288.8999023438,4.8000001907349,"cylinder",0.5,170,240,10}, 
{683.90002441406,-3291,5,"cylinder",0.5,170,240,10}, 
{701.20001220703,-3260.5,4.0999999046326,"cylinder",0.5,170,240,10}, 
{712.59997558594,-3245.3999023438,3.7000000476837,"cylinder",0.5,170,240,10}, 
{745.20001220703,-3265.6999511719,4.8000001907349,"cylinder",0.5,170,240,10}, 
{759.59997558594,-3230.3000488281,7.0999999046326,"cylinder",0.5,170,240,10}, 
{816.59997558594,-3277.8000488281,9.1999998092651,"cylinder",0.5,170,240,10}, 
{841,-3328.8999023438,17.299999237061,"cylinder",0.5,170,240,10}, 
} 
  
for markerid,_ in ipairs (markers) do 
    local m = createMarker(markers[markerid][1],markers[markerid][2],markers[markerid][3],markers[markerid][4],markers[markerid][5],markers[markerid][6],markers[markerid][7],markers[markerid][8]) 
    setElementID(m,"m"..markerid) 
    setElementAlpha(m,0) 
end 
  
local function popUpMarker() 
    local marker = getElementByID("m"..math.random(1,#markers)) 
    setElementAlpha(marker,255) 
end 
setTimer(popUpMarker,2000,0) 
  
addCommandHandler("tp",function(player,_) 
    setElementPosition(player,660.90002441406,-3290.3999023438,5) 
end) 
  

Edited by Guest
Link to comment
  • Moderators
the script works but all markers come at the same time and not random

Ofc, that's what you wrote. by using the createMarker function, it immediately spawn that marker and is visible.

You can just hide them and show the randomMarker with setElementVisible but the event onMarkerHit will still be triggered.

So you must do the random first and then create the marker. I can see that only the position changes for each marker (except the first marker but I think it wasn't intended), so I would suggest using a table of positions so it will be able to modify easily:

local moon2markers = { 
      {692.70, -3341.60, -3.00}, 
      {664.10, -3288.90, 4.80}, 
      {683.90, -3291.00, 5.00}, 
      {701.20, -3260.50, 4.10}, 
      {712.60, -3245.40, 3.70}, 
      {745.20, -3265.70, 4.80}, 
      {759.60, -3230.30, 7.10}, 
      {816.60, -3277.80, 9.20}, 
      {841.00, -3328.90, 17.30} 
} 
  
local reach = nil 
  
function moon2 (thePlayer) 
      if reach and isElement(reach) then 
            destroyElement(reach) 
      end 
      setElementPosition(thePlayer, 660.90002441406, -3341.59, 5) 
      outputChatBox("The table contains "..tostring(#moon2markers).." positions, getting a randome one ...", thePlayer) 
      local ran = math.random(#moon2markers) 
      outputChatBox("The random choosed the position number "..ran, thePlayer) 
      reach = createMarker(moon2markers[ran][1], moon2markers[ran][2], moon2markers[ran][3], "cylinder", 0.5, 170, 240, 10) 
      -- addEventHandler("onMarkerHit", reach, myFunctionWhenHitted) 
end 
addCommandHandler("moon", moon2) 

Btw, let me say you that your code is really really dirty, because where dafuq is thePlayer defined ? You said that your script were working and that you are using global variables everywhere, so I guess thePlayer is defined in all other functions that set this variable, and that you are using the same variable in all your script. THAT'S DIRTY ! It will work fine if you are alone on the server, but when there will be more than one player, it will be totally fucked up and will produce unexpected behaviors ... (if you are using this code on the server side though).

Regards,

Citizen

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