Jump to content

Never knew how to do a proper setTimer.. :s


DarkLink

Recommended Posts

Okay guys, I always had problems with setTimers, if someone can give me a good explanation to this.. I would be very appreciated!

I am trying to do something like :

but its not working :S

  
setTimer(function() wood = createObject ( 1418, xW, yW, zW, xWR,yWR,zWR, false ) end, 5000,1) 
  

also tryed this way:

  
setTimer(function(xW,yW,zW,xWR,yWR,zWR) wood = createObject ( 1418, xW, yW, zW, xWR,yWR,zWR, false ) end, 5000,1, xW,yW,zW,xWR,yWR,zWR) 
  

I dont know when I need to pass arguments on timer and when I dont need it :S

Thanks alot in advance!!!

Link to comment
What exactly doesn't work? do you get any error? mind posting the other script part?

cant a setTimer be inside a function that is attached to the onClientRender event?

it wont call many timers, because its inside an if condition.. and it enters there only some times..

I remember that I cant have timers on onClientRender events :S

But there must be a solution..

Link to comment
I don't understand, you mean that timers doesn't work when using "onClientRender" as event handler?

Something like, since the setTimer is inside the function that "onClientRender" calls it should not work??

  
function example() 
     
    outputChatBox("wtf3") 
    xW,yW,zW = getElementPosition(wood) 
    xWR, yWR, zWR = getElementRotation(wood) 
     
     
     
    if(expression == true) then 
        outputChatBox("wtf4") 
        destroyElement (wood) 
        setTimer(function(xW,yW,zW,xWR,yWR,zWR) wood = createObject ( 1418, xW, yW, zW, xWR,yWR,zWR, false ) end, 5000, 1, xW,yW,zW,xWR,yWR,zWR) 
    else 
     
        outputChatBox("nothing") 
    end 
     
end 
  
  
addCommandHandler ("line",  
    function () 
        addEventHandler("onClientRender", getRootElement(), example) 
             
     
         
    end 
) 
  

The problem is that I never see outputChatBox saying nothing or wtf4, and I know that expression goes true sometimes..

Do you know ?

thanks in advance mate

Link to comment

You're trying to get position of something that doesn't exists yet?

    xW,yW,zW = getElementPosition(wood) 
    xWR, yWR, zWR = getElementRotation(wood) 

setTimer(function(xW,yW,zW,xWR,yWR,zWR) wood = createObject ( 1418, xW, yW, zW, xWR,yWR,zWR, false ) end, 5000, 1, xW,yW,zW,xWR,yWR,zWR) 

Link to comment

Okay mate the thing I am trying to do is rebuild a object that goes into destroyed state after gets shooted, u see?

I could do it without any timer, it gets immeadiatly rebuild after gets shooted, but I want with some delay.. so I tryed this code:

  
local on = false 
local xW,yW,zW = nil,nil,nil 
local xWR,yWR,zWR = nil,nil,nil 
local wood = nil 
  
function createLine() 
     
     
    xW,yW,zW = getElementPosition(wood) 
    xWR, yWR, zWR = getElementRotation(wood) 
     
    dxDrawLine3D (xW, yW, zW+1, xW, yW, zW-1, tocolor ( 0, 255, 0, 255 ), 10)   -- this is only to help me, to know which line would isLineOfSightClear would use  
     
    if(isLineOfSightClear ( xW, yW,  zW+1,  xW,  yW, zW-1) and getElementData(wood,"nr", false) == 0) then 
        outputChatBox("no obstacle visible") 
        setElementData(wood,"nr", 1) 
         
        setTimer(function(xW,yW,zW,xWR,yWR,zWR, wood) destroyElement (wood) wood = createObject ( 1418, xW, yW, zW, xWR,yWR,zWR, false )  setElementData(wood, "nr",0)  end, 1000, 1, xW,yW,zW,xWR,yWR,zWR, wood) 
    else 
     
        outputChatBox("obstacle visible") 
    end 
     
end 
  
  
  
addCommandHandler ("line",  
    function () 
         
         
        if(on) then 
            on = false 
            removeEventHandler("onClientRender", getRootElement(), createLine) 
        else 
            local x,y,z = getElementPosition(getLocalPlayer()) 
            wood = createObject ( 1418, x + 3, y, z+3, 150.23657226563, 50.000610351563, 150.47875976563, false ) 
            setElementData(wood, "nr", 0) 
                        addEventHandler("onClientRender", getRootElement(), createLine) 
             
            on = true 
        end   
         
    end 
) 
  
  

and the element data on the wood is because I need a mechanism to wait to get the piece rebuild before send any setTimer, u see? if I dont use this.. many setTimers will be called because the the line will be clear.. and onClientRender will call that function many times.. u see?

I guess u understand now :)

Hope u can find the problem, because the code seems fine to me :S

Thanks alot in advance mate!!!

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