Jump to content

warnings


Burak5312

Recommended Posts

hello my code working good but giving warning like that

thank you

and i have questions

1- possible this function export as textlibrary?

2- and this code correct?

mta-screen-2019-08-21-12-45-32.png

 

text_c.Lua

 

local x,y = guiGetScreenSize()
local a = 255
function drawText()
dxDrawText("LEVEL UP!", x/2, y/2, x/2, y/2, tocolor(255,255,255,255), 2, "bankgothic", "center", "center")
fadetimer = setTimer(function()
removeEventHandler("onClientRender", root, drawText)
if(isTimer(fadetimer)) then
killTimer(fadetimer)
end
addEventHandler("onClientRender", root,fadeText)
end, 5000, 1)
function fadeText()
if not (a == 0) then
dxDrawText("LEVEL UP!", x/2, y/2, x/2, y/2, tocolor(255,255,255,a), 2, "bankgothic", "center", "center")
a = a - 5
destroytimer = setTimer(function()
removeEventHandler("onClientRender", root, fadeText)
if(isTimer(destroyTimer)) then
killTimer(destroytimer)
end
end, 8000, 1)
end
end
end
addEventHandler("onClientRender", root, drawText)

 

Edited by Burak5312
Link to comment

The errors appear because you're constantly attempting to bind the onClientRender event to drawText or fadeText when it's already bound to it. Specifically, line 5 creates a new timer every single frame, and after 5 seconds all those timers elapse and attempt to bind the event to fadeText, at the same rate (e.g. 30 times a second if you run on 30 FPS and hence create 30 timers per second). The easiest way to solve this would be an isTimer check before creating a new timer to make sure it doesn't exist yet. However, the whole code design is messy and not ideal. You should think about another way of writing that code, from scratch. Using only one event handler.

Edited by MrTasty
Link to comment

Well, I'm not sure if there's any significant performance loss, but definitely no timer is better than with timer. Timers, however little performance impact they have, do add up, and too many timers may contribute to slowdowns.

A better approach would be like this

local x,y = guiGetScreenSize()

local duration = 8000 -- duration of full alpha, in miliseconds
local fadeTime = 700 -- duration of fading from 255 to 0

local startTick = getTickCount() -- do this whenever you want the "LEVEL UP!" text to appear for duration + fadeTime

function drawText()
  local ticksPassed = getTickCount() - startTick -- compare the current tick count to the starting tick count to calculate the number of ticks passed
  local alpha = 255 -- set the alpha to full 255 at the beginning (this'll be used as the alpha value until ticksPassed is greater than duration)
  if ticksPassed > duration then -- if number of ticks passed exceeds threshold, start reducing alpha
    alpha = math.max(0, alpha - ((ticksPassed - duration) / fadeTime)) -- subtract from 255 a progressively larger number as the ticks past duration approach duration + fadeTime, clamping at 0 so it doesn't go into negatives
  end
  if alpha > 0 then -- if alpha is greater than 0, queue the text draw (a little optimisation as there's no need to queue it if it's completely invisible, while at a cost of branching evaluation, it's still faster than calling hardcoded functions)
    dxDrawText("LEVEL UP!", x/2, y/2, x/2, y/2, tocolor(255,255,255,alpha), 2, "bankgothic", "center", "center")
  end
end
addEventHandler("onClientRender", root, drawText) -- an additional improvement would be to only bind the event to a handler when necessary, unbinding it once the text doesn't need to appear. After all, this function will be triggered every frame simply for it to make some math and draw no text if getTickCount() - startTick > duration + fadeTime

This is a lot more maintainable, and uses only one function to do one thing. Your code uses to functions to do what one could do. This code only has one dxDrawText call, meaning if you want to change some parameters, you only need to do it in one place, not two.

Edited by MrTasty
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...