Jump to content

[SOLVED]Creating synced traffic lights results in chaos


Recommended Posts

EDIT:

I have realized I am an idiot, and discovered the "setTrafficLightsLocked" function.

Problem solved.

END EDIT

Hey all, so I was attempting to make it so the lights updated at an almost realistic time.

After 20 seconds of being green, I want them to turn yellow, after 6 seconds yellow, turn red, 1 second of red, turn back to green but for the other direction, and so on and so forth.

Unfortunately this has just resulted in chaos, the lights are not updating even within the 20 seconds and after about 30 seconds or so they just start flashing like crazy.

greenTime = 20000
yellowTime = 6000
redTime = 1000
gTimer = nil
yTimer = nil
rTimer = nil
lState = nil

function startTrafficLights()
    setTrafficLightState(0)
    lState = 0
    gTimer = setTimer(updateLights,greenTime,1)
end
addEventHandler("onResourceStart",getRootElement(getThisResource()),startTrafficLights)

function updateLights()
    if(lState == 0)then
        setTrafficLightState(1)
        killTimer(gTimer)
        lState = 1
        yTimer = setTimer(updateLights,yellowTime,1)
    elseif(lState == 1)then
        setTrafficLightState(2)
        killTimer(yTimer)
        lState = 2
        rTimer = setTimer(updateLights,redTime,1)
    elseif(lState == 2)then
        setTrafficLightState(3)
        killTimer(rTimer)
        lState = 3
        gTimer = setTimer(updateLights,greenTime,1)
    elseif(lState == 3)then
        setTrafficLightState(4)
        killTimer(gTimer)
        lState = 4
        yTimer = setTimer(updateLights,yellowTime,1)
    elseif(lState == 4)then
        setTrafficLightState(2)
        killTimer(yTimer)
        lState = 5
        rTimer = setTimer(updateLights,redTime,1)
    elseif(lState == 5)then
        setTrafficLightState(0)
        killTimer(rTimer)
        lState = 0
        gTimer = setTimer(updateLights,greenTime,1)
    end
end

I'm unsure what could be causing this, or even if I'm approaching this correctly. Any help is always appreciated!

EDIT: I added in debugstrings to each timer section, and it is calling the debugstring accordingly, but the traffic light states are not holding.

Edited by Stealthy Serval
Additional debugging performed, added detail in post.
  • Haha 1
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...