Hello there,
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
For example, in JavaScript I could implement the publish/subscribe pattern so that each light can be notified when to change state. As far as I know there is no such thing built into C3 and while turning to vanilla JavaScript to implement it is one option, to simplify things, I just used the Every tick event to poll state changes. However, I wonder if there is a better solution maybe? Also, when using Every tick with lots of sprite instances, isn't Every tick going it consume a lot of CPU resources perhaps? Three sprites are not a big deal but what about a hundred?
#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:
szabesz.hu/_dl/c3/SM-TrafficLights.c3p
Side note: my traffic lights work in this sequence: drivingtesttips.biz/traffic-lights-sequence.html
Thanks in advance,
Szabesz