Jump to content

Lua doubt and DxDrawText Flashing


Recommended Posts

Okay first I am need to clarify my doubt about Lua. Something that I didnt get it yet, so if someone could explain me, I would be really grateful :)

So.. if we have a function like this:

  
function blabla() 
      setTimer(heei, 1000,3) 
      local x = 5 
end 
  
end 
  

My question is: the line 4 "local x = 5" its only executed after the 3 loops of the timer?? or its readed right when the timer starts?

Thanks!

My second problem, I was trying to make a DxDrawText flashing, and I made it, but the flashing isnt so good, seems bugged or bad sync :S

And then it doesnt stop flashing, and I put the removeEventHandler onClientRender.. so?

This is the code:

addEvent("alertRadar",true) 
function showAlertAdvice() 
  
     
    addEventHandler ( "onClientRender", getRootElement(), firstAppear) 
     
end 
addEventHandler("alertRadar", getRootElement(), showAlertAdvice) 
  
function firstAppear () 
        X,Y = guiGetScreenSize () 
                X = X * (1/5.5) 
                Y = Y * (1/3) 
         
        setTimer(function(X,Y) 
        local alpha = "255" 
        dxDrawText ( "Slaineeed!", X, Y, X, Y, tocolor(255, 64, 64, alpha), 5) 
        alpha = 0 end, 500, 3, X,Y) 
        seTimer(hideAlert, 1500, 1) 
  
end 
  
function hideAlert() 
    removeEventHandler("onClientRender", getRootElement(), firstAppear) 
end 
  

If someone could help me here..

Thanks in advance!!

Link to comment

For your first question no it's executed as soon as the timer start.

addEvent("alertRadar",true) 
function showAlertAdvice() 
  
    
    addEventHandler ( "onClientRender", getRootElement(), firstAppear) 
    
end 
addEventHandler("alertRadar", getRootElement(), showAlertAdvice) 
  
function firstAppear () 
        X,Y = guiGetScreenSize () 
                X = X * (1/5.5) 
                Y = Y * (1/3) 
        local alpha = 255 
       dxDrawText ( "Slaineeed!", X, Y, X, Y, tocolor(255, 64, 64, alpha), 5) 
        setTimer(function() 
        if alpha == 0 then 
             alpha = 255 
        else 
             alpha = 0 
       end, 500, 3) 
        seTimer(hideAlert, 1500, 1) 
  
end 
  
function hideAlert() 
    removeEventHandler("onClientRender", getRootElement(), firstAppear) 
end 

Not tested.

Link to comment

Maybe:

addEvent("alertRadar",true) 
function showAlertAdvice() 
  
    
    addEventHandler ( "onClientRender", getRootElement(), firstAppear) 
    
end 
addEventHandler("alertRadar", getRootElement(), showAlertAdvice) 
  
function firstAppear () 
        X,Y = guiGetScreenSize () 
                X = X * (1/5.5) 
                Y = Y * (1/3) 
        local alpha = 255 
       dxDrawText ( "Slaineeed!", X, Y, X, Y, tocolor(255, 64, 64, alpha), 5) 
         
        seTimer(hideAlert, 1500, 1) 
  
end 
  
setTimer(function() 
        if alpha == 0 then 
             alpha = 255 
        else 
             alpha = 0 
       end, 500, 3) 
  
function hideAlert() 
    removeEventHandler("onClientRender", getRootElement(), firstAppear) 
end 

Link to comment

this is overkill, you are creating new timers every frame.

X,Y = guiGetScreenSize() 
X = X * (1/5.5) 
Y = Y * (1/3) 
alpha = 255 
  
function firstAppear() 
  dxDrawText("Slaineeed!", X, Y, X, Y, tocolor(255, 64, 64, alpha), 5) 
end 
  
function hideAlert() 
  removeEventHandler("onClientRender", getRootElement(), firstAppear) 
end 
  
addEvent("alertRadar",true) 
function showAlertAdvice() 
  addEventHandler("onClientRender", getRootElement(), firstAppear) 
  setTimer(function() alpha = alpha == 255 and 0 or 255 end, 500, 3) 
  setTimer(hideAlert, 1500, 1) 
end 
addEventHandler("alertRadar", getRootElement(), showAlertAdvice) 

edited*

Link to comment

Didnt worked JH10, but thanks for ur help!

Aibo didnt test yet, but can you please explain me this line:

setTimer(function() alpha = alpha == 255 and 0 or 255 end, 500, 3) ?

I cant get it, u assign the variable alpha to 255 and 0 ? or 255? I didnt get it.. :S

Thanks in advance!

Link to comment

alpha = alpha == 255 and 0 or 255

basically if statement (alpha == 255) is true, it sets alpha = 0 (and 0) else it sets alpha = 255 (or 255)

is pretty much the same as:

if alpha == 255 then

alpha = 0

else

alpha = 255

end

Link to comment
alpha = alpha == 255 and 0 or 255

basically if expression (alpha == 255) is true, it sets alpha = 0 (and 0) else it sets alpha = 255 (or 255)

is pretty much the same as:

if alpha == 255 then

alpha = 0

else

alpha = 255

end

AHHH I understand it very well!! Didnt know that type of scripting :)

Thanks bro!

Thanks JH10 too!!

I tested and it works Aibo!

Ty again

Link to comment

Here is a much simpler approach. If lua or MTA has a bitwise NOT operator, then the code can be chopped down by like 90%.

x = 0 
timer = -1 
function guiDrawRoutine() 
    if (x==0) then 
        dxDrawRectangle(250, 250, 125, 150, tocolor(25, 25, 25, 150)) 
    end 
    if (timer == -1) then 
        timer = setTimer(changeX, 500, 0) -- change 500 to however miliseconds/seconds you want your stuff to be displayed for. 
    end 
end 
  
function changeX() 
    -- Does Lua have a bitwise NOT operator? 
    if (x==0) then 
        x = 1 
    else 
        x = 0 
    end 
    if (timer ~= 0) then 
        killTimer(timer) 
        timer = -1 
    end 
end 
  

Link to comment

But you can make 'x' a bool in that script.

x = false 
function changeX ( ) 
    x = not x 
    ... 
end 

Same with the timer.

By the way, from your script:

if (timer ~= 0) then 
timer = -1 

So, the if condition will always pass, as a timer doesn't return zero, and you assign -1 to the variable.

There's a problem though. We are creating and destroying a timer every second, so why would we make the 'loop' argument infinite in the first place? Also, we can't cancel this animation, as the timer recreates itself.

Aibo's script should work neat, though it uses two functions. I'm not sure if that's needed, as you said too. Aibo is also creating two timers. That could be done in one. Let's mix those scripts.

  
X,Y = guiGetScreenSize() 
X = X * (1/5.5) 
Y = Y * (1/3) 
show,timer = false,nil 
  
function draw ( ) 
    if show then -- Only show the text when we want to 
        dxDrawText("Slaineeed!", X, Y, X, Y, tocolor(255, 64, 64, 255), 5) 
    end 
end 
  
function toggleAlertAdvice ( ) -- Call this function to put the flashing on or off 
    if isTimer ( timer ) then killTimer ( timer ) else timer = setTimer ( function() show=not show end, 500, 0 ) end 
    -- Function embedded in the timer 
end 
addEvent ( "alertRadar", true ) -- Or call the event 
addEventHandler ( "alertRadar", root, toggleAlertAdvice ) 
  
addEventHandler ( "onClientRender", root, draw ) -- 'root' is a global variable, the same as getRootElement(). 

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