Jump to content

[HELP] Making a marker invisible? Unknown problem.


koragg

Recommended Posts

Hey guys, I have a problem with setting alpha on markers. I create the markers on map start like this:

local checkpoints = {}
local CPMarker = {}
local CPBlip = {}

addEvent("onClientMapStarting", true)
function onClientMapStarting()
	checkpoints = getAll("checkpoint")
	if checkpoints and #checkpoints > 0 then
		for i = 1, #checkpoints do
			local color = { 0, 0, 255, 200 }
			local CPposX, CPposY, CPposZ = unpack(checkpoints[i].position)
			CPMarker[i] = createMarker(CPposX, CPposY, CPposZ, "checkpoint", checkpoints[i].size, color[1], color[2], color[3], color[4])
			CPBlip[i] = createBlip(CPposX, CPposY, CPposZ, 0, 2, color[1], color[2], color[3], color[4])
			setElementAlpha(CPMarker[i], 128)
		end
	end
end
addEventHandler("onClientMapStarting", root, onClientMapStarting)

function getAll(name)
	local result = {}
	for i,element in ipairs(getElementsByType(name)) do
		result[i] = {}
		result[i].id = getElementID(element) or i
		local position = { tonumber(getElementData(element,"posX")), tonumber(getElementData(element,"posY")), tonumber(getElementData(element,"posZ")) }
		local rotation = 0
		if getElementData(element,"rotation") then
		    rotation = tonumber(getElementData(element,"rotation"))
		elseif getElementData(element,"rotZ") then
		    rotation = tonumber(getElementData(element,"rotZ"))
		end
		local vehicle = tonumber(getElementData(element,"vehicle"))
		local size = tonumber(getElementData(element,"size"))
		result[i].position = position;
		result[i].rotation = rotation;
		result[i].vehicle = vehicle;
		result[i].size = size;
	end
	return result
end

So for each checkpoint that the map has a marker gets created. Now I want to make those created markers invisible when a certain condition is met but setElementAlpha doesn't seem to be working. Note that when setting the alpha on map start, it works. But the following doesn't work:

function checkAndFixTabData()
	local isSpectatingGhost = getElementData(localPlayer, "isSpectatingGhost")
	for i = 1, #CPMarker do
		if isElement(CPMarker[i]) then
			if isSpectatingGhost == true then
				setElementAlpha(CPMarker[i], 128)
			elseif isSpectatingGhost == false then
				setElementAlpha(CPMarker[i], 0)
			end
		end
	end
end
addEventHandler("onClientRender", root, checkAndFixTabData)

Basically I want to make it invisible when I am not spectating the ghost vehicle but make the markers visible when I am spectating the ghost. So what's the problem? :|

Edited by koragg
  • Haha 1
Link to comment
  • Moderators
local r, g, b, alpha = getMarkerColor ( CPMarker[i])
setMarkerColor ( CPMarker[i], r, g, b, 128)

 

local r, g, b, alpha = getMarkerColor ( CPMarker[i])
setMarkerColor ( CPMarker[i], r, g, b, 0)

 

setElementAlpha does not work for markers because they have their own method to set the alpha. (which is actually not really consistent to be honest :( )

 

bool setMarkerColor ( marker theMarker, int r, int g, int b, int a )

 

Required Arguments

  • theMarker: The marker that you wish to set the color of.
  • r: The amount of red in the final color (0 to 255).
  • g: The amount of green in the final color (0 to 255).
  • b: The amount of blue in the final color (0 to 255).
  • a: The amount of alpha in the final color (0 to 255).

 

https://wiki.multitheftauto.com/wiki/SetMarkerColor

 

Edited by IIYAMA
  • Thanks 1
Link to comment
if isElement(CPMarker[i]) then
	local r, g, b, alpha = getMarkerColor(CPMarker[i])
	if isSpectatingGhost == true then
		setMarkerColor(CPMarker[i], r, g, b, 128)
	end
	if isSpectatingGhost == false then
		setMarkerColor(CPMarker[i], 0, 0, 255, 0)
	end
end

If I change every other color value it works. Changing the alpha value does nothing, it stays the same no matter the value...just my luck -_-

OK, I found this here:

This article needs checking.

Reason(s): Client side only the alpha value defaults to 255

Any (other) way to make markers invisible/visible client-side?

Edited by koragg
Link to comment
1 hour ago, IIYAMA said:

This might be a solution, yet maybe not what you want.

https://wiki.multitheftauto.com/wiki/SetElementVisibleTo (serverside)

Yeah, I saw that but it's server-side, whereas my whole code is client-side. I managed to fix the problem like this:

1. Do not create the markers on map start.

2. If the isSpectatingGhost is 'false' - do nothing, but if it's 'true' - create 2 markers (and 2 radar blips) ahead of the ghost's current position.

3. If the ghost's position is in a radius of 45m to the position of a marker - destroy the marker (and the radar blip).

4. If I stop spectating the ghost (isSpectatingGhost == false) and markers (and blips) have been made - destroy all markers (and blips) that have been created (max can be 2).

5. All this in a for loop and onClientRender.

Didn't want to do it like this, it would have been easier and more efficient to just set alpha of markers (and blips), but oh well, if no other way then it's fine. Thanks for the help anyway :)

Edited by koragg
Markers are on the same spot as race checkpoints^ that's their purpose - to show where the normal CPs would be while I'm spectating the ghost vehicle.
  • Sad 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...