Jump to content

createMarker table loop


Noki

Recommended Posts

smallAircraftMarkerTable = { 
    --[[{ mX = 1984.7, mY = -2382, mZ = 12.5, mRZ = 90 }, 
    { mX = -1246.24, mY = -96.19, mZ = 13, mRZ = 135 }, 
    { mX = -1203.11, mY = -139.7, mZ = 13, mRZ = 135 },]] -- Tried and failed 
    { 1984.7, -2315, 12.5, 90 }, 
    { -1246.2402, -96.1916, 13, 135 }, 
    { -1203.1144, -139.7044, 13, 135 } 
} 
  
largeAircraftMarkerTable = { 
    {  2112.5559, -2440.1431, 12.5, 180 }, 
} 
  
for i, v in pairs ( smallAircraftMarkerTable ) do 
    smallAircraftMarkers = createMarker( v[1], v[2], v[3], "cylinder", 2, 255, 255, 0 , 125 ) 
    -- smallAircraftMarkers = createMarker( v.mX, v.mY, v.mZ, "cylinder", 2, 255, 255, 0 , 125 ) -- Fail 
    SAspawnRotation = v[4] 
end 
  
for i, v in pairs ( largeAircraftMarkerTable ) do 
    largeAircraftMarkers = createMarker( v[1], v[2], v[3], "cylinder", 2, 255, 255, 0, 125 ) 
    LAspawnRotation = v[4] 
end 
  
smallAircraftID = {  
    { "Beagle", 511 },  
    { "Cropduster", 512 },  
    { "Dodo", 593 },  
    { "Rustler", 476 },  
    { "Stuntplane", 513 },  
    { "Shamal", 519 } 
} 
  
largeAircraftID = { 
    { "AT-400", 577 }, 
    { "Andromada", 592 }, 
    { "Nevada", 553 }, 
} 
  
spawnWindow = guiCreateWindow( 553, 179, 278, 336, "", false ) 
guiWindowSetSizable( spawnWindow, false ) 
guiSetVisible( spawnWindow, false ) 
  
spawnBtn = guiCreateButton( 19, 287, 108, 39, "Spawn", false, spawnWindow ) 
closeBtn = guiCreateButton( 150, 287, 108, 39, "Close", false, spawnWindow ) 
spawnGridlist = guiCreateGridList( 19, 31, 239, 246, false, spawnWindow ) 
spawnColumn = guiGridListAddColumn( spawnGridlist, "Aircraft", 0.9 )     
  
addEventHandler( "onClientMarkerHit", smallAircraftMarkers, 
    function () 
        outputDebugString( "successfully hit" ) 
        if isPedInVehicle( localPlayer ) then return end 
        if not isPedOnGround( localPlayer ) then return end 
        --setElementFrozen( localPlayer, true ) 
         
        showCursor( true ) 
        guiSetVisible( spawnWindow, true ) 
        SAgridlist() 
    end 
) 
  
addEventHandler( "onClientMarkerHit", largeAircraftMarkers, 
    function () 
        if isPedInVehicle( localPlayer ) then return end 
        if not isPedOnGround( localPlayer ) then return end 
        --setElementFrozen( localPlayer, true ) 
         
        showCursor( true ) 
        guiSetVisible( spawnWindow, true ) 
        LAgridlist() 
    end 
) 
  
function SAgridlist() 
    guiSetText( spawnWindow, "Small Aircraft Spawn" ) 
    guiGridListClear( spawnGridlist ) 
    for i, v in pairs ( smallAircraftID ) do 
        local row = guiGridListAddRow( spawnGridlist ) 
        if row ~= nil and row ~= false and row ~= -1 then 
            guiGridListSetItemText( spawnGridlist, row, spawnColumn, v[1], false, false ) 
            guiGridListSetItemData( spawnGridlist, row, spawnColumn, v[2] ) 
        end 
    end 
end 
  
function LAgridlist() 
    guiSetText( spawnWindow, "Large Aircraft Spawn" ) 
    guiGridListClear( spawnGridlist ) 
    for i, v in ipairs ( largeAircraftID ) do 
        local row = guiGridListAddRow( spawnGridlist ) 
        if row ~= nil and row ~= false and row ~= -1 then 
            guiGridListSetItemText( spawnGridlist, row, spawnColumn, v[1], false, false ) 
            guiGridListSetItemData( spawnGridlist, row, spawnColumn, v[2] ) 
        end 
    end 
end 
  
addEventHandler( "onClientGUIClick", guiRoot, 
    function () 
        if ( source == spawnBtn ) then 
            local row = guiGridListGetSelectedItem( spawnGridlist ) 
            if row ~= nil and row ~= false and row ~= -1 then 
                local vehID = guiGridListGetItemData( spawnGridlist, row, spawnColumn ) 
                 
                if ( guiGetText( spawnWindow ) == "Small Aircraft Spawn" ) then 
                    triggerServerEvent( "spawnAircraft", localPlayer, vehID, SAspawnRotation ) 
                elseif ( guiGetText( spawnWindow ) == "Large Aircraft Spawn" ) then 
                    triggerServerEvent( "spawnAircraft", localPlayer, vehID, LAspawnRotation ) 
                else 
                    exports.dx:createNewDxMessage( "Something went wrong", 255, 255, 0 ) 
                end 
            else 
                exports.dx:createNewDxMessage( "You did not select a vehicle from the list", 255, 255, 0 ) 
            end 
        elseif ( source == closeBtn ) then 
            --setElementFrozen( localPlayer, false ) 
            guiSetVisible( spawnWindow, false ) 
            showCursor( false ) 
        end 
    end 
) 
  

Only the last marker coordinate in the tables work. It will create the markers, but I'm not able to use the rest of the markers.

Don't worry about the server-side as it has no effect on this issue and the script works fine besides the marker loop.

Thanks.

Link to comment

Obviously, you can only use the last markers because they're the ones the event handlers are attached to. They are the ones whose values are assigned to the variables last, therefore all previous values are overwritten. You could call 'addEventHandler' in the same loop they're created. Or you could create some abstract element with 'createElement', attach event handler to them and make this abstract element the parent of marker elements using 'setElementMarker'. Or you could create a set of markers (the table where markers are keys and boolean values 'true' are values), attach the event handler to root and check for presence of marker in the set from within the event handler. As you can see, there are many things you could do to achieve the result.

Link to comment
  • 5 years later...

I have the exact same issue and i am unable to understand how to avoid overwriting. i have just started scripting someone please help! Can you please edit the code and make the right changes so i can learn? I'd be highly thankful for the help. 

 

t ={ 
	{2450, -1670, 12},
	{2455, -1670, 12},
	{2460, -1670, 12}
}
for i, v in pairs(t) do
	a = createMarker(v[1], v[2], v[3], "cylinder", 2.5, 255, 255, 255)
end
addEventHandler('onMarkerHit', a, 

function (player)
	if source == a and getElementType(player) == "player" then
		if isPedInVehicle (player) == true then
			outputChatBox('Vehicles not allowed.', player)
		else
			setElementPosition(player, 2400, -1650, 12)
			outputChatBox("You're spawned.", player)
		end
	end
end)

 

Edited by Ice_Cool
Link to comment

t ={ 
	{2450, -1670, 12},
	{2455, -1670, 12},
	{2460, -1670, 12}
}
for i, v in pairs(t) do
	a = createMarker(v[1], v[2], v[3], "cylinder", 2.5, 255, 255, 255)
    addEventHandler('onMarkerHit', a, function (player)
	    if source == a and getElementType(player) == "player" then
		    if isPedInVehicle (player) == true then
			    outputChatBox('Vehicles not allowed.', player)
		    else
			    setElementPosition(player, 2400, -1650, 12)
			    outputChatBox("You're spawned.", player)
		    end
	    end
    end)
end

All you have to do is to move the event handler within the for loop.

 

Edited by Tekken
Link to comment
12 hours ago, Tekken said:


t ={ 
	{2450, -1670, 12},
	{2455, -1670, 12},
	{2460, -1670, 12}
}
for i, v in pairs(t) do
	a = createMarker(v[1], v[2], v[3], "cylinder", 2.5, 255, 255, 255)
    addEventHandler('onMarkerHit', a, function (player)
	    if source == a and getElementType(player) == "player" then
		    if isPedInVehicle (player) == true then
			    outputChatBox('Vehicles not allowed.', player)
		    else
			    setElementPosition(player, 2400, -1650, 12)
			    outputChatBox("You're spawned.", player)
		    end
	    end
    end)
end

All you have to do is to move the event handler within the for loop.

 

Thank you for the reply. But its still not working, only the last marker is teleporting the player to the defined coordinates in setElementPosition function. it still giving me the same error! Any other error or possible way to fix it?

Link to comment

Try this one:

 

local t ={ 
	{2450, -1670, 12},
	{2455, -1670, 12},
	{2460, -1670, 12}
};
local markers = {};

addEventHandler("onResourceStart", reosurceRoot, function()
	for i,v in pairs(t) do
		markers[i] = createMarker(v[1], v[2], v[3], "cylinder", 2.5, 255, 255, 255);
		addEventHandler('onMarkerHit', markers[i], function (hitElement)
			if getElementType(hitElement) == "player" then
				if isPedInVehicle(hitElement) then
					outputChatBox('Vehicles not allowed.', hitElement);
				else
					setElementPosition(hitElement, 2400, -1650, 12);
					outputChatBox("You're spawned.", hitElement);
				end
			end
		end);
	end
end);

 

Link to comment
3 hours ago, Tekken said:

Try this one:

 


local t ={ 
	{2450, -1670, 12},
	{2455, -1670, 12},
	{2460, -1670, 12}
};
local markers = {};

addEventHandler("onResourceStart", reosurceRoot, function()
	for i,v in pairs(t) do
		markers[i] = createMarker(v[1], v[2], v[3], "cylinder", 2.5, 255, 255, 255);
		addEventHandler('onMarkerHit', markers[i], function (hitElement)
			if getElementType(hitElement) == "player" then
				if isPedInVehicle(hitElement) then
					outputChatBox('Vehicles not allowed.', hitElement);
				else
					setElementPosition(hitElement, 2400, -1650, 12);
					outputChatBox("You're spawned.", hitElement);
				end
			end
		end);
	end
end);

 

For better performance, create an element and set it as the parent for the markers instead of adding an event for every single marker.

local markersParent = createElement("markersParent")
local t = { 
	{2450, -1670, 12},
	{2455, -1670, 12},
	{2460, -1670, 12}
}

addEventHandler("onResourceStart", resourceRoot, function()
    for i = 1, #t do
        local v = t[i]
        local marker = createMarker(v[1], v[2], v[3], "cylinder", 2.5, 255, 255, 255)
        setElementParent(marker, markersParent)
    end
end)

addEventHandler("onMarkerHit", markersParent, function(hitElement)
    if getElementType(hitElement) == "player" then
        if isPedInVehicle(hitElement) then
            outputChatBox("Vehicles not allowed.", hitElement)
        else
            setElementPosition(hitElement, 2400, -1650, 12)
            outputChatBox("You're spawned.", hitElement)
        end
    end
end)

 

  • Like 1
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...