I have put together a simple traffic lights "simulator" in an effort to implement a simple state machine and to test how the Timer behavior works.
I wonder if anyone can help me by suggesting improvements on how to "instruct" the lights to change state with a state machine approach, and also on how to use the Timer behavior without using "too much" CPU resources.
#1 States of the lights
#2 Using Timers
First I tried to pick ONLY ONE light instance for the Start Timer and I tried to pick a different light instance under its On timer condition, but there was no way of picking a different one, not even by UID. In other words, the scope of the "On timer" condition was the one and only instance of the initiating Start timer. To solve this, I simply start a Timer for each light, which works but still.... why running three Timers when only one would do the trick (at least in theory)?
So my questions are: is there anything I'm missing? How could I improve my traffic lights simulator so that in real life projects I can use those improved techniques instead?
You can download the C3 project form here:
Side note: my traffic lights work in this sequence: drivingtesttips.biz/traffic-lights-sequence.html
Thanks in advance,
Develop games in your browser. Powerful, performant & highly capable.
does this work for you? lowered total cpu usage from 6% to 3% on my PC.
there also is a free state machine Addon with conditions like on state change you might want to check out https://www.construct.net/en/make-games/addons/441/finite-state-machine
Is there a reason you are calling the 3 functions for animation changes on every tick? You can just call the functions from your FSM logic when the change happens. Also you can just use a single function and pass a string with the colour to determine which light is picked in the function.
I wouldn't use every tick unless absolutely critical.
Use 1 sprite for all stop lights. Give each light variables, 1 for the build and 1 for the timer. (This way you could just subtract from the timer variable each second and do something. Timer=30 set green, every sec minus 1, Timer = 10 set yellow, Timer = 8 set red. etc. Or whatever)
Pick all, then make a sub condition to pick by the build variable.
(once you use pick by condition, it narrows choices for that group of code and won't check all again unless you reset it with pick all. There's a technical name for it, but I forget what it is.)
Use 1 timer (or don't and just use the timer variable every second).
You could have a groups for East/West and NS, or whatever for group type timer changes.
If you're ever going to use this for something interesting, you might consider incorporating pathfinding and solids, spawning cars at set or random intervals. That way you could run real simulations.
Here is my version of it using picking instead of the for each loop, and using a single function that is called when the state changes.
Wow, thank you for all the replies!
First, I need to consider all the info and questions than I'll be back soon... :)