Jump to content

Making race_delay_indicator show colored nicks


koragg

Recommended Posts

Posted (edited)

Whenever I try to put any kind of dxDrawText in that file it just draws it for less than a second and then text vanishes. Why? There are no errors, I replaced all needed things to dxDrawText lines and still it only draws it for half a second or less... Below is the original code, no idea what I gotta do so that it leaves dxDrawText stuff stay on-screen for longer.

local g_Root = getRootElement()
local g_ResRoot = getResourceRootElement(getThisResource())
local g_Me = getLocalPlayer()

local DISTANCE_FRONT_BEHIND = 0.03
local SCALE = 1.5
local TIME_TO_DISPLAY = 2000

local frontTick
local behindTick
local delayDisplayFront = dxText:create("", 0.5, 0.37, true, "default", SCALE)
local delayDisplayBehind = dxText:create("", 0.5, 0.43, true, "default", SCALE)
delayDisplayFront:color(255, 0, 0)
delayDisplayBehind:color(0, 255, 0)

addEvent("showDelay", true)
addEventHandler("showDelay", g_Root,
	function(delayTime, optional)
		if tonumber(optional) then
			local cps = getElementData(g_Me, "race.checkpoint") - optional
			if cps < 2 then
				cps = ""
			else
				cps = "(-"..cps.."CPs) "
			end
			delayDisplayBehind:text("-"..msToTimeStr(delayTime).." "..cps..string.gsub(getPlayerName(source), "#%x%x%x%x%x%x", ""))
			delayDisplayBehind:visible(true)
			behindTick = getTickCount()
			setTimer(hideDelayDisplay, TIME_TO_DISPLAY, 1, false)
		elseif type(optional) == "table" then
			if delayTime < 0 then
				-- outputChatBox("-"..msToTimeStr(-delayTime).." current record")
				delayDisplayFront:text("+"..msToTimeStr(-delayTime).." record #"..optional[1])
				delayDisplayFront:color(255, 255, 0)
			elseif delayTime > 0 then
				-- outputChatBox("+"..msToTimeStr(delayTime).." current record")
				delayDisplayFront:text("-"..msToTimeStr(delayTime).." record #"..optional[1])
				delayDisplayFront:color(0, 255, 255)
			end
			delayDisplayFront:visible(true)
			frontTick = getTickCount()
			setTimer(hideDelayDisplay, TIME_TO_DISPLAY, 1, true)
		else
            local cps = getElementData(source, "race.checkpoint") - getElementData(g_Me, "race.checkpoint")
			if cps < 2 then
				cps = ""
			else
				cps = "(+"..cps.."CPs) "
			end
			delayDisplayFront:text("+"..msToTimeStr(delayTime).." "..cps..string.gsub(getPlayerName(source), "#%x%x%x%x%x%x", ""))
			delayDisplayFront:color(255, 0, 0)
			delayDisplayFront:visible(true)
			frontTick = getTickCount()
			setTimer(hideDelayDisplay, TIME_TO_DISPLAY, 1, true)
		end
	end
)

function hideDelayDisplay(front)
	if front == "both" then
		delayDisplayFront:visible(false)
		delayDisplayBehind:visible(false)
	elseif front then
		local pastTime = getTickCount() - frontTick
		if pastTime >= TIME_TO_DISPLAY then
			delayDisplayFront:visible(false)
		else
			if pastTime < 50 then pastTime = 50 end
			setTimer(hideDelayDisplay, pastTime, 1, true)
			-- outputChatBox("front dalassen")
		end
	else
		local pastTime = getTickCount() - behindTick
		if pastTime >= TIME_TO_DISPLAY then
			delayDisplayBehind:visible(false)
		else
			if pastTime < 50 then pastTime = 50 end
			setTimer(hideDelayDisplay, pastTime, 1, false)
			-- outputChatBox("behind dalassen")
		end
	end
end

addEventHandler('onClientResourceStart', g_ResRoot,
	function()
		local settingsFile = xmlLoadFile("settings.xml")
		if settingsFile then
			local pos = xmlNodeGetAttributes(settingsFile)
			delayDisplayFront:position(pos.x, pos.y - DISTANCE_FRONT_BEHIND)
			delayDisplayBehind:position(pos.x, pos.y + DISTANCE_FRONT_BEHIND)
		else
			settingsFile = xmlCreateFile("settings.xml","settings")
			xmlNodeSetAttribute(settingsFile,"x",0.5)
			xmlNodeSetAttribute(settingsFile,"y",0.4)
		end
		xmlSaveFile(settingsFile)
		xmlUnloadFile(settingsFile)
	end
)

addCommandHandler("setdelaypos",
	function(cmd,x,y)
		if x and y then
			if tonumber(x) and tonumber(y) then
				delayDisplayFront:position(x, y - DISTANCE_FRONT_BEHIND)
				delayDisplayBehind:position(x, y + DISTANCE_FRONT_BEHIND)
				delayDisplayFront:text("FRONT")
				delayDisplayBehind:text("BEHIND")
				delayDisplayFront:color(255, 0, 0)
				delayDisplayBehind:color(0, 255, 0)
				delayDisplayFront:visible(true)
				delayDisplayBehind:visible(true)
				setTimer(hideDelayDisplay, TIME_TO_DISPLAY, 1, "both")
				local settingsFile = xmlLoadFile("settings.xml")
				if settingsFile then
					xmlNodeSetAttribute(settingsFile,"x",x)
					xmlNodeSetAttribute(settingsFile,"y",y)
				end
				xmlSaveFile(settingsFile)
				xmlUnloadFile(settingsFile)
			else
				outputChatBox("WRONG PARAMETERS! Syntax is /setdelaypos x y", 255, 0, 0)
			end
		else
			outputChatBox("WRONG PARAMETERS! Syntax is /setdelaypos x y", 255, 0, 0)
		end
	end
)

function msToTimeStr(ms)
	if not ms then
		return ''
	end
	local centiseconds = tostring(math.floor(math.fmod(ms, 1000)))
	if #centiseconds == 2 then
		centiseconds = '0'..centiseconds
	elseif #centiseconds == 1 then
		centiseconds = '00'..centiseconds
	end
	local s = math.floor(ms / 1000)
	local seconds = tostring(math.fmod(s, 60))
	if #seconds == 1 then
		seconds = '0' .. seconds
	end
	local minutes = tostring(math.floor(s / 60))
	return minutes .. ':' .. seconds .. ':' .. centiseconds
end


-- Exported function for settings menu, KaliBwoy

function showCPDelays()
	triggerServerEvent( "onClientShowCPDelays", resourceRoot )
end

function hideCPDelays()
	triggerServerEvent( "onClientHideCPDelays", resourceRoot )
end

 

Edited by koragg
and wtf is this (dxText:create)? Why not the normal dxDrawText -.-
Posted
15 minutes ago, dugasz1 said:

When dxDrawText called then it just draw to the current frame. So you have to call it on every frame whit onClientRender. (Or onClientPreRender it depends on what do you want to do: Game Processing Order)

Thanks, I tried adding "onClientRender" to the function that draws it but still same result.

Posted (edited)
26 minutes ago, dugasz1 said:

Show us how did you do it or when do you want to draw it?

I just replaced line 33 and line 37 with dxDrawText and deleted lines 34, 38 and 40.  I got rid of " delayDisplayFront " and replaced all lines in which text was being drawn with dxDrawText lines.

Edited by koragg
look just lines 30-43, if that gets fixed, the others are the same.
Posted

I don't really know OOP in Lua but if  showDelay called only once it doesn't good.

function drawText ( )
  	--draw what you want
    dxDrawText ( text, 44, 43 ) 
end

--Call this if you want it to be visible:
addEventHandler ( "onClientRender", root, drawText )

--Call this if you want it to disapear
removeEventHandler ( "onClientRender", root, drawText )

 

Posted

Did this and still same, half a second and rip text.

local screenW, screenH = guiGetScreenSize()

addEvent("showDelay", true)
function drawText(delayTime, optional)
		if tonumber(optional) then
			local cps = getElementData(g_Me, "race.checkpoint") - optional
			if cps < 2 then
				cps = ""
			else
				cps = "(-"..cps.."CPs) "
			end
			delayDisplayBehind:text("-"..msToTimeStr(delayTime).." "..cps..string.gsub(getPlayerName(source), "#%x%x%x%x%x%x", ""))
			delayDisplayBehind:visible(true)
			behindTick = getTickCount()
			setTimer(hideDelayDisplay, TIME_TO_DISPLAY, 1, false)
		elseif type(optional) == "table" then
			if delayTime < 0 then
				--delayDisplayFront:text("+"..msToTimeStr(-delayTime).." record #"..optional[1])
				dxDrawText("#FEFE22+"..msToTimeStr(-delayTime).." record #"..optional[1], screenW * 0.0000, screenH * 0.3657, screenW * 1.0000, screenH * 0.3972, tocolor(255, 255, 255, 255), 1.5, "default", "center", "center", false, false, false, true, false)
			elseif delayTime > 0 then
				--delayDisplayFront:text("-"..msToTimeStr(delayTime).." record #"..optional[1])
				dxDrawText("#00FFFF-"..msToTimeStr(delayTime).." record #"..optional[1], screenW * 0.0000, screenH * 0.3657, screenW * 1.0000, screenH * 0.3972, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, true, false)
			end
			--delayDisplayFront:visible(true)
			frontTick = getTickCount()
			setTimer(hideDelayDisplay, TIME_TO_DISPLAY, 1, true)
		else
            --[[local cps = getElementData(source, "race.checkpoint") - getElementData(g_Me, "race.checkpoint")
			if cps < 2 then
				cps = ""
			else
				cps = "(+"..cps.."CPs) "
			end
			delayDisplayFront:text("+"..msToTimeStr(delayTime).." "..cps..string.gsub(getPlayerName(source), "#%x%x%x%x%x%x", ""))
			delayDisplayFront:color(255, 0, 0)
			delayDisplayFront:visible(true)
			frontTick = getTickCount()
			setTimer(hideDelayDisplay, TIME_TO_DISPLAY, 1, true)--]]
		end
end
addEventHandler("showDelay", g_Root, drawText)
addEventHandler("onClientRender", root, drawText)

 

Posted

This have no sense.
The  showDelay should add the onClienRender event handler. (I guess you want to do that)
Now it have to create a lot of error (or just don't work). Because if the onClientRender call the function the  delayTime, optional variables will be nill. So in 17 it will fail because nill<0 have no sense. (Or maybe nill<0 result will be false but i think it will be an error)

addEvent("showDelay", true)
addEventHandler("showDelay", g_Root,
	function(delayTime, optional)
		if tonumber(optional) then
			local cps = getElementData(g_Me, "race.checkpoint") - optional
			if cps < 2 then
				cps = ""
			else
    	--Etc....
        addEventHandler("onClientRender", root, drawText)
end)
    
function drawText ( )
  	--draw what you want
    dxDrawText ( text, 44, 43 ) 
end

Then remove it when needed.

Posted

Yea I know that would work normally but here I actually need to draw the " delayTime" and the "optional" variables, which are only accessible in the function tied to " showDelay ". So in other words, I can't draw it in another function and just add/remove event handlers :P So, really, no idea what to do to make this work...

Posted
local lastDelayTime = false
local lastOptional = false

addEvent("showDelay", true)
addEventHandler("showDelay", g_Root,
	function(delayTime, optional)
		lastDelayTime = delayTime --"Save" it
		lastOptional = optional
    
		if tonumber(optional) then
			local cps = getElementData(g_Me, "race.checkpoint") - optional
			if cps < 2 then
				cps = ""
			else
    	--Etc....
        addEventHandler("onClientRender", root, drawText)
end)
    
function drawText ( )
  	--draw what you want
    dxDrawText ( text, 44, 43 ) 
end

You can do it like that.

(Sorry for late respone)

  • Like 1
Posted (edited)

Full working code: (delayindicator_client.lua)

local screenW, screenH = guiGetScreenSize()
local x, y = screenW/1440, screenH/900

local lastDelayTime = false
local lastOptional = false
local coloredName = false
local blackName = false
local sourcecp = false
-------------------------------------------------------------------------------------------------------------------------
addEvent("showDelay", true)
addEventHandler("showDelay", root,
	function(delayTime, optional)
	
		lastDelayTime = delayTime
		lastOptional = optional
		coloredName = addTeamColor(source)
		blackName = getPlayerName(source):gsub('#%x%x%x%x%x%x', '')
		sourcecp = getElementData(source, "race.checkpoint")
		
		if tonumber(optional) then
			if isEventHandlerAdded("onClientRender", root, drawMinusPlayerText) == false and 
			isEventHandlerAdded("onClientRender", root, drawPlusTopText) == false and
			isEventHandlerAdded("onClientRender", root, drawMinusTopText) == false then
				addEventHandler("onClientRender", root, drawMinusPlayerText)
			end
			setTimer(function() removeEventHandler("onClientRender", root, drawMinusPlayerText) end, 2000, 1, source)
		elseif type(optional) == "table" then
			if delayTime < 0 then
				if isEventHandlerAdded("onClientRender", root, drawPlusTopText) == false then
					addEventHandler("onClientRender", root, drawPlusTopText)
				end
				setTimer(function() removeEventHandler("onClientRender", root, drawPlusTopText) end, 2000, 1, source)
			elseif delayTime > 0 then
				if isEventHandlerAdded("onClientRender", root, drawMinusTopText) == false then 
					addEventHandler("onClientRender", root, drawMinusTopText)
				end
				setTimer(function() removeEventHandler("onClientRender", root, drawMinusTopText) end, 2000, 1, source)
			end
		else
			if isEventHandlerAdded("onClientRender", root, drawPlusPlayerText) == false then 
				addEventHandler("onClientRender", root, drawPlusPlayerText)
			end
			setTimer(function() removeEventHandler("onClientRender", root, drawPlusPlayerText) end, 2000, 1, source)
		end
	end
)
-------------------------------------------------------------------------------------------------------------------------
function drawPlusTopText()
	if type(lastOptional) == "table" then
		dxDrawText("#000000+"..msToTimeStr(-lastDelayTime).." record #"..lastOptional[1], screenW * 0.0000 + 1, screenH * 0.3667 + 1, screenW * 1.0000 + 1, screenH * 0.3954 + 1, tocolor(255, 255, 255, 255), 1.2*y, "default-bold", "center", "center", false, false, false, true, false)
		dxDrawText("#FFFF00+"..msToTimeStr(-lastDelayTime).." record #"..lastOptional[1], screenW * 0.0000, screenH * 0.3667, screenW * 1.0000, screenH * 0.3954, tocolor(255, 255, 255, 255), 1.2*y, "default-bold", "center", "center", false, false, false, true, false)
	end
end
-------------------------------------------------------------------------------------------------------------------------
function drawMinusTopText()
	if type(lastOptional) == "table" then
		dxDrawText("#000000-"..msToTimeStr(lastDelayTime).." record #"..lastOptional[1], screenW * 0.0000 + 1, screenH * 0.3667 + 1, screenW * 1.0000 + 1, screenH * 0.3954 + 1, tocolor(255, 255, 255, 255), 1.2*y, "default-bold", "center", "center", false, false, false, true, false)
		dxDrawText("#00FFFF-"..msToTimeStr(lastDelayTime).." record #"..lastOptional[1], screenW * 0.0000, screenH * 0.3667, screenW * 1.0000, screenH * 0.3954, tocolor(255, 255, 255, 255), 1.2*y, "default-bold", "center", "center", false, false, false, true, false)
	end
end
-------------------------------------------------------------------------------------------------------------------------
function drawMinusPlayerText()
	if tonumber(lastOptional) then
		local cps = getElementData(localPlayer, "race.checkpoint") - lastOptional
			if cps < 2 then
				cps = ""
			else
				cps = "(-"..cps.." CPs) "
			end
		dxDrawText("#000000-"..msToTimeStr(lastDelayTime).." "..cps..blackName, screenW * 0.0000 + 1, screenH * 0.3667 + 1, screenW * 1.0000 + 1, screenH * 0.3954 + 1, tocolor(255, 255, 255, 255), 1.2*y, "default-bold", "center", "center", false, false, false, true, false)
		dxDrawText("#00FF00-"..msToTimeStr(lastDelayTime).." "..cps.."#FFFFFF"..coloredName, screenW * 0.0000, screenH * 0.3667, screenW * 1.0000, screenH * 0.3954, tocolor(255, 255, 255, 255), 1.2*y, "default-bold", "center", "center", false, false, false, true, false)
	end
end
-------------------------------------------------------------------------------------------------------------------------
function drawPlusPlayerText()
	local cps = sourcecp - getElementData(localPlayer, "race.checkpoint")
		if cps < 2 then
			cps = ""
		else
			cps = "(+"..cps.." CPs) "
		end
	dxDrawText("#000000+"..msToTimeStr(lastDelayTime).." "..cps..blackName, screenW * 0.0000 + 1, screenH * 0.3667 + 1, screenW * 1.0000 + 1, screenH * 0.3954 + 1, tocolor(255, 255, 255, 255), 1.2*y, "default-bold", "center", "center", false, false, false, true, false)
	dxDrawText("#FF0000+"..msToTimeStr(lastDelayTime).." "..cps.."#FFFFFF"..coloredName, screenW * 0.0000, screenH * 0.3667, screenW * 1.0000, screenH * 0.3954, tocolor(255, 255, 255, 255), 1.2*y, "default-bold", "center", "center", false, false, false, true, false)
end
-------------------------------------------------------------------------------------------------------------------------
function isEventHandlerAdded(sEventName, pElementAttachedTo, func)
 if type(sEventName) == 'string' and 
  isElement(pElementAttachedTo) and 
  type(func) == 'function' 
 then
  local aAttachedFunctions = getEventHandlers(sEventName, pElementAttachedTo)
  if type(aAttachedFunctions) == 'table' and #aAttachedFunctions > 0 then
   for i, v in ipairs(aAttachedFunctions) do
    if v == func then
     return true
    end
   end
  end
 end
 return false
end
-------------------------------------------------------------------------------------------------------------------------
function msToTimeStr(ms)
	if not ms then
		return ''
	end
	local centiseconds = tostring(math.floor(math.fmod(ms, 1000)))
	if #centiseconds == 2 then
		centiseconds = '0'..centiseconds
	elseif #centiseconds == 1 then
		centiseconds = '00'..centiseconds
	end
	local s = math.floor(ms / 1000)
	local seconds = tostring(math.fmod(s, 60))
	if #seconds == 1 then
		seconds = '0' .. seconds
	end
	local minutes = tostring(math.floor(s / 60))
	return minutes .. ':' .. seconds .. ':' .. centiseconds
end
-------------------------------------------------------------------------------------------------------------------------
-- Exported function for settings menu, KaliBwoy
function showCPDelays()
	triggerServerEvent( "onClientShowCPDelays", resourceRoot )
end
-------------------------------------------------------------------------------------------------------------------------
function hideCPDelays()
	triggerServerEvent( "onClientHideCPDelays", resourceRoot )
end
-------------------------------------------------------------------------------------------------------------------------
function addTeamColor(player)
	local playerTeam = getPlayerTeam ( player ) 
	if ( playerTeam ) then
		local r,g,b = getTeamColor ( playerTeam )
		local n1 = toHex(r)
		local n2 = toHex(g)
		local n3 = toHex(b)
		if r <= 16 then n1 = "0"..n1 end
		if g <= 16 then n2 = "0"..n2 end
		if b <= 16 then n3 = "0"..n3 end
		return "#"..n1..""..n2..""..n3..""..getPlayerNametagText(player)
	else
		return getPlayerNametagText(player)
	end
end
-------------------------------------------------------------------------------------------------------------------------
function toHex(n)
    local hexnums = {"0","1","2","3","4","5","6","7",
                     "8","9","A","B","C","D","E","F"}
    local str,r = "",n%16
    if n-r == 0 then str = hexnums[r+1]
    else str = toHex((n-r)/16)..hexnums[r+1] end
    return str
end

 

Edited by koragg

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