Stealthy Serval Posted December 5, 2018 Share Posted December 5, 2018 (edited) 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 December 5, 2018 by Stealthy Serval Additional debugging performed, added detail in post. 1 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