Jump to content

onMarkerHit triggers multiple times


Peti

Recommended Posts

Hi, I'm learning how to script and I've this problem:

When this code is running, it triggers "cargarRecorrido()" 4 times

    function recargarPizzas()
        local X = 201.400390625 local Y = -178.943359375 local Z = 1.174932718277 local RADIO_RECARGA = 2
        local zonaRecarga = createMarker(X, Y, Z, 'checkpoint', RADIO_RECARGA, 255, 0, 0, 255, player)
        local zonaRecargaIcono = createBlip(X, Y, 0, player)
        addEventHandler('onMarkerHit', zonaRecarga, function()
            destroyElement(zonaRecarga)
            destroyElement(zonaRecargaIcono)
            cargarRecorrido()
        end)

cargarRecorrido():

    local seleccionarSitio = 'SELECT * FROM coords_pizzero ORDER BY random() LIMIT 1' -- CAMBIAR
    function cargarRecorrido()
        local tablaRecorrido = { }
        dbQuery(function(queryHandle)
            local resultados = dbPoll(queryHandle, 0)
            for indice, puntoEntrega in pairs(resultados) do
                table.insert(tablaRecorrido, resultados[indice])
                crearPuntoEntrega(tablaRecorrido)
            end
        end, db, seleccionarSitio)
    end

I don't know why, but when I try to destroy markers, 'onMarkerHit' triggers a lot of times (at least 2).

b33261c984fc390176b67e3ebd6943c5.png

Why is this happening?

Thank you in advance.

Edited by Peti
Link to comment
  • Moderators

Those warnings are all happening on different lines. I only see two lines that destroy elements, which means that you are showing only half of the places where the warning occur.

 

The thing that is going wrong is that the code is trying to destroy elements that are destroyed.

Can I see the other warning lines as well? (Don't show everything, just show the important code as you did before)

 

 

Link to comment
11 hours ago, DNL291 said:

Where are you calling the function 'recargarPizzas'?

    function crearPuntoEntrega(tabla)
        local RADIO_ENTREGA = 1
        local X = tabla[1].x
        local Y = tabla[1].y
        local Z = tabla[1].z
        local zonaEntrega = createMarker(X, Y, Z, 'checkpoint', RADIO_ENTREGA, 255, 0, 0, 255, player)
        local zonaEntregaIcono = createBlip(X, Y, 0, player)
        addEventHandler('onMarkerHit', zonaEntrega, function()
            destroyElement(zonaEntrega)
            destroyElement(zonaEntregaIcono)
            table.remove(tabla, 1)
            local next = next
            if next(tabla) == nil then
                recargarPizzas()
            else
                crearPuntoEntrega(tabla)
            end
        end)
    end

As you can see, "crearPuntoEntrega" gets a table with coordinates, then it creates checkpoints according to those coordinates. If the table is empty, the script triggers "recargarPizzas". I don't know if this is correct, I've done some improvisation using my previous knowledge about languages.

 

7 hours ago, IIYAMA said:

Those warnings are all happening on different lines. I only see two lines that destroy elements, which means that you are showing only half of the places where the warning occur.

 

The thing that is going wrong is that the code is trying to destroy elements that are destroyed.

Can I see the other warning lines as well? (Don't show everything, just show the important code as you did before)

Of course, here you go:

    function crearPuntoEntrega(tabla)
        local RADIO_ENTREGA = 1
        local X = tabla[1].x
        local Y = tabla[1].y
        local Z = tabla[1].z
        local zonaEntrega = createMarker(X, Y, Z, 'checkpoint', RADIO_ENTREGA, 255, 0, 0, 255, player)
        local zonaEntregaIcono = createBlip(X, Y, 0, player)
        addEventHandler('onMarkerHit', zonaEntrega, function()
            destroyElement(zonaEntrega)
            destroyElement(zonaEntregaIcono)
            table.remove(tabla, 1)
            local next = next
            if next(tabla) == nil then
                recargarPizzas()
            else
                crearPuntoEntrega(tabla)
            end
        end)
    end

 

And here you have an example:

ddea0869b60b1a5c95ff3082be211e1a.png

After hitting that marker, it triggers "crearPuntoEntrega()" that it has the event "onMarkerHit".

Link to comment

Try by checking the 'hitElement' parameter, also, you need to pass the player to the function.

Edit: Another thing you need to debug is the table.remove and the condition at the line 17. Probably the 'destroyElement' message is due the function "createPuntoDelivery" being always called, causing this error.

Try this:


function crearPuntoEntrega(tabla)
	local RADIO_ENTREGA = 1
	local X = tabla[1].x
	local Y = tabla[1].y
	local Z = tabla[1].z
	local zonaEntrega = createMarker(X, Y, Z, 'checkpoint', RADIO_ENTREGA, 255, 0, 0, 255, player)
	-- local zonaEntregaIcono = createBlip(X, Y, 0, player) 4th argument is the blip id
	local zonaEntregaIcono = createBlip( X, Y, Z, 0, 2, 255, 0, 0, 255, 0, 16383, player )
	
	addEventHandler('onMarkerHit', zonaEntrega, function( hitElement, md )
		if getElementType(hitElement) == "player" then
			if isElement(zonaEntrega) then destroyElement(zonaEntrega) end -- isElement: to avoid annoying error messages
			if isElement(zonaEntrega) then destroyElement(zonaEntregaIcono) end
			table.remove(tabla, 1)
			local next = next
			if next(tabla) == nil then
				recargarPizzas()
			else
				crearPuntoEntrega(tabla)
			end
		end
	end)
end

 

Edited by DNL291
  • Like 1
Link to comment
10 minutes ago, DNL291 said:

Try by checking the 'hitElement' parameter, also, you need to pass the player to the function.

Try this:


function crearPuntoEntrega(tabla)
	local RADIO_ENTREGA = 1
	local X = tabla[1].x
	local Y = tabla[1].y
	local Z = tabla[1].z
	local zonaEntrega = createMarker(X, Y, Z, 'checkpoint', RADIO_ENTREGA, 255, 0, 0, 255, player)
	-- local zonaEntregaIcono = createBlip(X, Y, 0, player) 4th argument is the blip id
	local zonaEntregaIcono = createBlip( X, Y, Z, 0, 2, 255, 0, 0, 255, 0, 16383, player )
	
	addEventHandler('onMarkerHit', zonaEntrega, function( hitElement, md )
		if getElementType(hitElement) == "player" then
			if isElement(zonaEntrega) then destroyElement(zonaEntrega) end -- isElement: to avoid annoying error messages
			if isElement(zonaEntrega) then destroyElement(zonaEntregaIcono) end
			table.remove(tabla, 1)
			local next = next
			if next(tabla) == nil then
				recargarPizzas()
			else
				crearPuntoEntrega(tabla)
			end
		end
	end)
end

 

Well I don't have the warnings anymore, but it keeps triggering 4 times. Thank you nonetheless!

BTW, how do you guys check if a table is empty?

Link to comment

I just edited my post, talking about the possible error at line 17:

Quote

Another thing you need to debug is the table.remove and the condition at the line 17. Probably the 'destroyElement' message is due the function "createPuntoDelivery" being always called, causing this error.

 

49 minutes ago, Peti said:

BTW, how do you guys check if a table is empty?

#value returns the length of a table or a string, example:

local myTable = { "one", 2, "three" }
print( #myTable )
-- output: 3

By the way, I think I understand what you want to do, here is a small code that creates sequentially routes defined in a table:

local locations = {
	{ X, Y, Z },
	{ X, Y, Z },
	{ X, Y, Z },
}

local index = 1

-- test --
addCommandHandler( "createloc",
	function ( player )
		crearPuntoEntrega( locations, player )
	end
)
--

function crearPuntoEntrega(tabla, thePlayer)
	local tabla = tabla
	local RADIO_ENTREGA = 1
	local X,Y,Z = unpack( tabla[index] )
	local zonaEntrega = createMarker(X, Y, Z, 'checkpoint', RADIO_ENTREGA, 255, 0, 0, 255, thePlayer)
	local zonaEntregaIcono = createBlip( X, Y, Z, 0, 2, 255, 0, 0, 255, 0, 16383, thePlayer )
	
	addEventHandler('onMarkerHit', zonaEntrega, function( hitElement, md )
		if getElementType(hitElement) == "player" and md then
			if isElement(zonaEntrega) then destroyElement(zonaEntrega) end -- isElement: to avoid annoying error messages
			if isElement(zonaEntrega) then destroyElement(zonaEntregaIcono) end
			onMarkerHitFunc( tabla, hitElement )
		end
	end)
end

function onMarkerHitFunc( t, p )
	if p and t and #t <= (index + 1) then
		index = index + 1
		crearPuntoEntrega( t, p )
	elseif p and #t > (index + 1) then
		index = 1
		crearPuntoEntrega( t, p )
	else
		-- invalid table or player element
	end
end

The table 'locations' represents the table with all coordinates, just pass the table you want for the function along with the player element.

Don't call the function inside a loop, from what I saw above, you did it here:

        dbQuery(function(queryHandle)
            local resultados = dbPoll(queryHandle, 0)
            for indice, puntoEntrega in pairs(resultados) do
                table.insert(tablaRecorrido, resultados[indice])
                crearPuntoEntrega(tablaRecorrido)
            end
        end, db, seleccionarSitio)

Firstly, get the query result, and then use the table 'tableRecorded' for that (this table must be structured in the same way as the table 'locations' from my code).

It will look like this:

local tablaRecorrido = { }

function cargarRecorrido( thePlayer )
	if not (isElement(thePlayer)) then return end
	--
	dbQuery(function(queryHandle)
		local resultados = dbPoll(queryHandle, 0)
		for indice, puntoEntrega in pairs(resultados) do
			table.insert(tablaRecorrido, resultados[indice])
		end
	end, db, seleccionarSitio)
	crearPuntoEntrega( tablaRecorrido, thePlayer )
end

 

Edited by DNL291
  • Thanks 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...