Timer behavior and State Machine questions (traffic lights example)

0 favourites
  • 7 posts
From the Asset Store
State Machine
$10.99 USD
State Machine is a great tool for managing the state of anything in your game.
  • 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

  • does this work for you? lowered total cpu usage from 6% to 3% on my PC.

    drive.google.com/file/d/1Q5-H1ewrMvRhCtd55M4Qs4JyV5zV2yeP/view

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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

    1drv.ms/u/s!AkmrWgxeuxlKhItsZdDTqIM5KA2-DA

  • Wow, thank you for all the replies!

    First, I need to consider all the info and questions than I'll be back soon... :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)