Jump to content

Distance from player to (next) checkpoint etc.


koragg

Recommended Posts

Got it to show like this but if on some cps it indeed shows close to 0 meters when I'm infront of them, on larger ones it still shows noticably bigger number :S

--http://lua-users.org/wiki/FormattingNumbers
function round(val, decimal)
  if (decimal) then
    return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  else
    return math.floor(val+0.5)
  end
end

local sx_, sy_ = guiGetScreenSize ( )
local sx, sy = sx_/1440, sy_/900

function distanceFromPlayerToCheckpoint(player, i)
    g_Checkpoints = getAll("checkpoint")
	local checkpoint = g_Checkpoints[getElementData(player, "race.checkpoint")]
	if checkpoint == nil then
		return false
	end
	local x, y, z = getElementPosition(player)
	for i, v in ipairs(checkpoint) do
		outputChatBox(unpack(v))
	end
	return getDistanceBetweenPoints3D(x, y, z, unpack(checkpoint.position)), unpack(checkpoint.size)
end

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 size = {
					tonumber(getElementData(element, "size"))
				}
		result[i].position = position;
		result[i].size = size;
 	end
	return result
end

function draw() 
	local distance, size = distanceFromPlayerToCheckpoint(localPlayer)
	if distance and size then
    dxDrawText(distance-size.." m to next checkpoint", sx_ * 0.8734, sy_ * 0.2009, sx_ * 0.9510, sy_ * 0.2259, tocolor(254, 254, 34, 255), 1*sy, "default-bold", "center", "top", false, false, false, false, false)
	end
end
addEventHandler("onClientRender", root, draw)

Also I can't seem to round the number.

Link to comment
  On 18/10/2016 at 17:29, aka Blue said:

math.floor( distance-size ) (?

Expand  

xD thanks, never knew I can use that instead of the 'round' function :v

On normal sized cps (not huge) it says 4m when I'm infront of them (that's ok) but on bigger cps it says 20m, when I am literally infront of it. It is better than before though so thanks people :)

Edited by koragg
ok......some huge cps are fine, on other big ones it says not 4 but 20m when i am infront of it hmm
Link to comment
  On 18/10/2016 at 17:37, koragg said:

xD thanks, never knew I can use that instead of the 'round' function :v

On normal sized cps (not huge) it says 4m when I'm infront of them (that's ok) but on bigger cps it says 20m, when I am literally infront of it. It is better than before though so thanks people :)

Expand  

20 m when you're infront of it? Can you send me a screenshot? It works perfectly for me whether the size is big or small.

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