DarkLink Posted June 21, 2011 Share Posted June 21, 2011 Hi guys, I want to put a timer using dxDrawText. I could do it, when I was using for client.. But there would be a timer different for each player that connects server, and thats not what I need I need a timer to be set by server when first player log in, then wait 30 secs . ( this timer would appear on that client 30..29..28...) And if another player would log in while the timer was decreasing, his timer (another player), would also continue decreasing, not starting from 30 again. U guys can understand? If dxDrawText worked via server side, would work .. but no My friend told me to use triggerClientEvent and setTimer, communicating server with client . So I managed to do this, but I dont understand much the addEvent and his trigger, if u guys could give me some hints server: local sec = "30" function onPlayerJoinBind () bindKey ( source, "l", "down", VehicleLights ) setTimer(triggerClientEvent, 1000, 30, "mostrarSegundos", root, sec) end addEventHandler ( "onPlayerJoin", getRootElement(), onPlayerJoinBind ) client: function createText() if (tonumber(segundos)==0) then return end X,Y = guiGetScreenSize () X = X * (2.5/3) Y = Y * (1/4) dxDrawText ( segundos, X, Y, X, Y, tocolor(255, 255, 255, 255), 5) end addEvent( "mostrarSegundos", true ) addEventHandler( "mostrarSegundos", getRootElement(), mostrarSegundos ) function mostrarSegundos (sec) segundos = sec addEventHandler("onClientRender",rootElement, createText) end btw I know I have to decrease the sec to print it on dxDrawText, but I dont know where Thanks guys, and sorry for asking Link to comment
CowTurbo Posted June 21, 2011 Share Posted June 21, 2011 function onJoin () if not( timerValue == true )then timerValue = true getTimerValue () end addEventHandler ( "onPlayerJoin", getRootElement(), onJoin ) function getTimerValue () if ( timerValue == true ) then triggerClientEvent ( getRootElement(), "showDX", getRootElement() ) setTimer ( setValue, 30000,1 ) elseif (timerValue == false) then triggerClientEvent ( getRootElement(), "hideDX", getRootElement() ) end end function setValue () timerValue = false getTimerValue () end function onStart () setTimer ( getTimerValuea(), 50,0) end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onStart ) function showDX () addEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "showDX", true ) addEventHandler ( "showDX", getRootElement(), showDX ) function drawDX () X,Y = guiGetScreenSize () X = X * (2.5/3) Y = Y * (1/4) dxDrawText ( segundos, X, Y, X, Y, tocolor(255, 255, 255, 255), 5) end function hideDX () removeEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "hideDX ", true ) addEventHandler ( "hideDX ", getRootElement(), hideDX ) Client side timers will not work for all players. So, do all things in server side, and create text in client side. Good luck, hope it work =) Link to comment
DarkLink Posted June 21, 2011 Author Share Posted June 21, 2011 function onJoin () if not( timerValue == true )then timerValue = true getTimerValue () end addEventHandler ( "onPlayerJoin", getRootElement(), onJoin ) function getTimerValue () if ( timerValue == true ) then triggerClientEvent ( getRootElement(), "showDX", getRootElement() ) setTimer ( setValue, 30000,1 ) elseif (timerValue == false) then triggerClientEvent ( getRootElement(), "hideDX", getRootElement() ) end end function setValue () timerValue = false getTimerValue () end function onStart () setTimer ( getTimerValuea(), 50,0) end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onStart ) function showDX () addEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "showDX", true ) addEventHandler ( "showDX", getRootElement(), showDX ) function drawDX () X,Y = guiGetScreenSize () X = X * (2.5/3) Y = Y * (1/4) dxDrawText ( segundos, X, Y, X, Y, tocolor(255, 255, 255, 255), 5) end function hideDX () removeEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "hideDX ", true ) addEventHandler ( "hideDX ", getRootElement(), hideDX ) Client side timers will not work for all players. So, do all things in server side, and create text in client side. Good luck, hope it work =) Thanks for ur reply, but where is the segundos decreasing ? I mean the segundos-- ? so will go drawing 30, then 29 .. to 0 ? ty again Link to comment
DarkLink Posted June 21, 2011 Author Share Posted June 21, 2011 Hey CowTurbo I have been working on these but isnt working.. Your solution, doesnt work too CowTurbo. I made it showing numbers from 30 to ( - infinite ), but the numbers are going to fast, not in each second . So I made the corrections to ur code: CLIENT function showDX (segundos) sec = segundos addEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "showDX", true ) addEventHandler ( "showDX", getRootElement(), showDX ) function drawDX () X,Y = guiGetScreenSize () X = X * (2.5/3) Y = Y * (1/4) dxDrawText ( sec, X, Y, X, Y, tocolor(255, 255, 255, 255), 5) end function hideDX () removeEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "hideDX ", true ) addEventHandler ( "hideDX ", getRootElement(), hideDX ) SERVER segundos = "30" function onJoin () if not( timerValue == true )then timerValue = true getTimerValue () end end addEventHandler ( "onPlayerJoin", getRootElement(), onJoin ) function getTimerValue () if ( timerValue == true ) then setTimer(escreve, 1000, 30) setTimer ( setValue, 30000,1 ) elseif (timerValue == false) then triggerClientEvent ( getRootElement(), "hideDX", getRootElement() ) end end function escreve () triggerClientEvent ( getRootElement(), "showDX", getRootElement(), segundos ) newnumber = (tonumber(segundos)-1) segundos = tostring(newnumber) --triggerClientEvent ( getRootElement(), "hideDX", getRootElement(), segundos ) end function setValue () timerValue = false getTimerValue () end function onStart () timerValue = false setTimer ( getTimerValue, 50,0) end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onStart ) This is the hardest things of MTA Scripting that I cant understand much. Handlers, Events, and Sync beetween client and server functions. So I would appreciate some help from pros Thanks alot guys ! I really want to know how to this , I want to learn it. Thanks Link to comment
Castillo Posted June 21, 2011 Share Posted June 21, 2011 Ok, look, I think the best way you could do this would be using element data, which is synced between client and server. --server side segundos = 30 timerEnabled = false function onJoin () if timerEnabled then setElementData(source,"secondsRemaining",tostring(segundos)) triggerClientEvent ( source, "showDX", source) end end addEventHandler ( "onPlayerJoin", getRootElement(), onJoin ) function escreve () timerEnabled = true for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) triggerClientEvent ( v, "showDX", v ) end setTimer(countDown,1000,30) end addCommandHandler("inciarcontador",escreve) function countDown() segundos = segundos -1 for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) end if segundos == 0 then triggerClientEvent ( "hideDX", getRootElement() ) timerEnabled = false end end function onStart () timerEnabled = false end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onStart ) --client side function drawDX () X,Y = guiGetScreenSize () X = X * (2.5/3) Y = Y * (1/4) dxDrawText ( getElementData(getLocalPlayer(),"secondsRemaining"), X, Y, X, Y, tocolor(255, 255, 255, 255), 5) end function showDX () addEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "showDX", true ) addEventHandler ( "showDX", getRootElement(), showDX ) function hideDX () removeEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "hideDX ", true ) addEventHandler ( "hideDX ", getRootElement(), hideDX ) When you type /iniciarcontador it will start the count down, when it reaches 0 it will stop and hide the DX element. P.S: CowTurbo, your code makes no sense for me. Link to comment
DarkLink Posted June 21, 2011 Author Share Posted June 21, 2011 Ok, look, I think the best way you could do this would be using element data, which is synced between client and server.--server side segundos = 30 timerEnabled = false function onJoin () if timerEnabled then setElementData(source,"secondsRemaining",tostring(segundos)) triggerClientEvent ( source, "showDX", source) end end addEventHandler ( "onPlayerJoin", getRootElement(), onJoin ) function escreve () timerEnabled = true for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) triggerClientEvent ( v, "showDX", v ) end setTimer(countDown,1000,30) end addCommandHandler("inciarcontador",escreve) function countDown() segundos = segundos -1 for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) end if segundos == 0 then triggerClientEvent ( "hideDX", getRootElement() ) timerEnabled = false end end function onStart () timerEnabled = false end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onStart ) --client side function drawDX () X,Y = guiGetScreenSize () X = X * (2.5/3) Y = Y * (1/4) dxDrawText ( getElementData(getLocalPlayer(),"secondsRemaining"), X, Y, X, Y, tocolor(255, 255, 255, 255), 5) end function showDX () addEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "showDX", true ) addEventHandler ( "showDX", getRootElement(), showDX ) function hideDX () removeEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "hideDX ", true ) addEventHandler ( "hideDX ", getRootElement(), hideDX ) When you type /iniciarcontador it will start the count down, when it reaches 0 it will stop and hide the DX element. P.S: CowTurbo, your code makes no sense for me. Thanks SolidSnake, it does the job I guess, didnt tryed yet. But in this case I had to write that command every time I want to run this gamemode. Is there a way to start it write after 2 players online ? Thanks. Link to comment
Castillo Posted June 21, 2011 Share Posted June 21, 2011 Yes, it is possible, use this: segundos = 30 timerEnabled = false function onJoin () if timerEnabled then setElementData(source,"secondsRemaining",tostring(segundos)) triggerClientEvent ( source, "showDX", source) end if getPlayerCount ( ) >= 2 then escreve() end end addEventHandler ( "onPlayerJoin", getRootElement(), onJoin ) function escreve () timerEnabled = true for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) triggerClientEvent ( v, "showDX", v ) end setTimer(countDown,1000,30) end function countDown() segundos = segundos -1 for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) end if segundos == 0 then triggerClientEvent ( "hideDX", getRootElement() ) timerEnabled = false end end function onStart () timerEnabled = false end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onStart ) Link to comment
DarkLink Posted June 22, 2011 Author Share Posted June 22, 2011 Yes, it is possible, use this: segundos = 30 timerEnabled = false function onJoin () if timerEnabled then setElementData(source,"secondsRemaining",tostring(segundos)) triggerClientEvent ( source, "showDX", source) end if getPlayerCount ( ) >= 2 then escreve() end end addEventHandler ( "onPlayerJoin", getRootElement(), onJoin ) function escreve () timerEnabled = true for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) triggerClientEvent ( v, "showDX", v ) end setTimer(countDown,1000,30) end function countDown() segundos = segundos -1 for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) end if segundos == 0 then triggerClientEvent ( "hideDX", getRootElement() ) timerEnabled = false end end function onStart () timerEnabled = false end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onStart ) Thanks alot solid Its working with the two options, in the second option just had to change from .. if getPlayerCount ( ) >= 2 then escreve() end end .. to .. if getPlayerCount ( ) >= 2 then setTimer(escreve, 50,1) end end .. but there is only a little problem, the removeEventHandler is not working segundos will reach 0 but it doesnt trigger the event hideDX, I guess.. and dont know why the zero keeps there on the screen.. Thanks bro Link to comment
Castillo Posted June 22, 2011 Share Posted June 22, 2011 (edited) segundos = 30 timerEnabled = false function onJoin () if timerEnabled then setElementData(source,"secondsRemaining",tostring(segundos)) triggerClientEvent ( source, "showDX", source) end if getPlayerCount ( ) >= 2 then setTimer(escreve, 50,1) end end addEventHandler ( "onPlayerJoin", getRootElement(), onJoin ) function escreve () timerEnabled = true for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) triggerClientEvent ( v, "showDX", v ) end setTimer(countDown,1000,30) end function countDown() segundos = segundos -1 for i,v in pairs(getElementsByType("player")) do setElementData(v,"secondsRemaining",tostring(segundos)) end if segundos <= 0 then triggerClientEvent ( "hideDX", getRootElement() ) timerEnabled = false end end function onStart () timerEnabled = false end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onStart ) Edited June 22, 2011 by Guest Link to comment
DarkLink Posted June 22, 2011 Author Share Posted June 22, 2011 Thanks for ur reply, but still not working. And I dont get it, why u do this if segundos >= 0 then triggerClientEvent ( "hideDX", getRootElement() ) timerEnabled = false end So would hide it before reach zero. the condition on IF is >= 0 . So why? Thanks, and sorry about ur time . Link to comment
Castillo Posted June 22, 2011 Share Posted June 22, 2011 My bad, I wanted to do: <= 0 , copy code again. Link to comment
DarkLink Posted June 22, 2011 Author Share Posted June 22, 2011 My bad, I wanted to do: <= 0 , copy code again. I tryed that too when u told me, cos it wasnt making sense to me. But also didnt worked.. Any clue? The zero keeps there in the monitor.. :c Link to comment
Wojak Posted June 22, 2011 Share Posted June 22, 2011 if you have any doubt that the part of the code is not running, add some output lines, you can also output variables to check it they have corect values when they ate neede't. If you can not make removeEventHandler to work, set the segundos as empty string after 0 segundos = "" Link to comment
DarkLink Posted June 22, 2011 Author Share Posted June 22, 2011 if you have any doubt that the part of the code is not running, add some output lines, you can also output variables to check it they have corect values when they ate neede't.If you can not make removeEventHandler to work, set the segundos as empty string after 0 segundos = "" Thanks for ur reply I though that too, but It will cause some lag I thing. because the event will always triggered.. onClientRender, causing some lags ? Thanks!! EDIT: It should work removeEventHandler, and I dont know why is not working Link to comment
Wojak Posted June 22, 2011 Share Posted June 22, 2011 no one should notice the FPS drop with one text, but try to debug your code anyway Link to comment
DarkLink Posted June 22, 2011 Author Share Posted June 22, 2011 no one should notice the FPS drop with one text, but try to debug your code anyway Thanks . Bro I did what u recommend, and I guess this DxDrawText is bugged When I put this : outputChatBox("okay working") at here function hideDX () outputChatBox("okay working", getRootElement()) removeEventHandler ( "onClientRender", getRootElement(), drawDX) end addEvent ( "hideDX ", true ) addEventHandler ( "hideDX ", getRootElement(), hideDX ) I dont even can see the timer starting.. from 30 to 0.. I restart gamemode, I restart server.. I restart gta.. Nothing.. Can someone tell me the problems of this event? onClientRender? Thanks! Link to comment
Castillo Posted June 22, 2011 Share Posted June 22, 2011 The event is wrong, check this: "hideDX " it has an SPACE. function drawDX () X,Y = guiGetScreenSize () X = X * (2.5/3) Y = Y * (1/4) dxDrawText ( getElementData(getLocalPlayer(),"secondsRemaining"), X, Y, X, Y, tocolor(255, 255, 255, 255), 5) end function showDX () addEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "showDX", true ) addEventHandler ( "showDX", getRootElement(), showDX ) function hideDX () removeEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "hideDX", true ) addEventHandler ( "hideDX", getRootElement(), hideDX ) Now it's removing the text. Link to comment
DarkLink Posted June 22, 2011 Author Share Posted June 22, 2011 The event is wrong, check this: "hideDX " it has an SPACE. function drawDX () X,Y = guiGetScreenSize () X = X * (2.5/3) Y = Y * (1/4) dxDrawText ( getElementData(getLocalPlayer(),"secondsRemaining"), X, Y, X, Y, tocolor(255, 255, 255, 255), 5) end function showDX () addEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "showDX", true ) addEventHandler ( "showDX", getRootElement(), showDX ) function hideDX () removeEventHandler ( "onClientRender", getRootElement(), drawDX ) end addEvent ( "hideDX", true ) addEventHandler ( "hideDX", getRootElement(), hideDX ) Now it's removing the text. LOOOL!! I failed so much, a little thing like a space changes everything Thanks alot bro! Working ;D 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