DarkLink Posted July 6, 2011 Share Posted July 6, 2011 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 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
JR10 Posted July 6, 2011 Share Posted July 6, 2011 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
DarkLink Posted July 6, 2011 Author Share Posted July 6, 2011 Hm thanks for your help, I understand it About the code, there was an error, one end missing on the function inside the timer But ur code now doesnt even flash Ty Link to comment
JR10 Posted July 6, 2011 Share Posted July 6, 2011 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
Aibo Posted July 6, 2011 Share Posted July 6, 2011 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
DarkLink Posted July 6, 2011 Author Share Posted July 6, 2011 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.. Thanks in advance! Link to comment
Aibo Posted July 6, 2011 Share Posted July 6, 2011 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
DarkLink Posted July 6, 2011 Author Share Posted July 6, 2011 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
falseprophet Posted July 7, 2011 Share Posted July 7, 2011 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
qaisjp Posted July 7, 2011 Share Posted July 7, 2011 yes I think it has a NOT operator. lala = false if not lala then print(tostring(lala)) end == > false Link to comment
falseprophet Posted July 7, 2011 Share Posted July 7, 2011 yes I think it has a NOT operator. lala = false if not lala then print(tostring(lala)) end == > false NOT is NOT a bitwise NOT Link to comment
Deltanic Posted July 7, 2011 Share Posted July 7, 2011 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
DarkLink Posted July 7, 2011 Author Share Posted July 7, 2011 Thanks for all the replys guys, I will keep Aibo solution is working fine. I know I can optimize the code, but dont need it , I guess Thanks all!! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now