Add a ProgrammaticTimer
class, and generalize Timer
functionalities
#5671
fenix-hub
started this conversation in
Engine Core
Replies: 1 comment 3 replies
-
I feel this adds a lot of complexity for something that won't be used often. Replacing |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Before making an issue out of this, since I'm not that much social and I don't have a real implementation for this yet, even though I'd really like to contribute and provide one (and I will, if this goes well), I'd like to open a discussion about this topic:
ProgrammaticTimer
s.Timer
s are one of the most used and useful functionalities in programming languages and environments, and I'm personally using tons of them in several projects, from little games to generalist apps and even utility softwares.They allow to run custom schedules in a very simple way thanks to the Godot Engine signaling system, which feels more clean to other programming/scripting languages like
Timer
s andTimerTask
s in Java or NodeJS timers/schedules.However, I've always felt like Timers in Godot don't have that much elasticity and usability they deserve to have, moreover in a scripting language like GDScript, when one needs to setup and use them programmatically. The reason is pretty evident and understandable, since Timers must be handled from the SceneTree gameloop, so the only two options are:
Either way, we would always end up with a one-shot or an infinite timer. But why not having an intermediate (and, imho, a more theoretically correct one) option? This could be a
Scheduled
timer, orFixed
, orRated
timer, as you may prefer, which is:It would mean to have a
Timer
which can be configured to emit thetimeout
signal a fixed, configured amount of times, and then just stops.Eventually, the
repeat
index could be returned in thesignal
already fired by aTimer
:So, the current
Timer
could be generalized to suchTimer
that:a) for
one-shot
, the.repeat
property is1
by defaultb) for
infinite
orlooping
, the.repeat
property could be-1
(if we want to create an exclusion condition through a check statement after each increment of therepeat
parameter) or either0
(if we want to create an exclusion condition through a check statement before an increment of therepeat
parameter, keeping it0
)Yes but the difference is pretty evident ( a. less code to write, less variables to handle, b. the repeat is a property of the timer itself, and should be treated as such - not accessible/editable directly during execution as any other variable in the same scope). Furthermore...
I was previously referring to a
ProgrammaticTimer
, and the nature of a new possible timer to beprogrammatic
.Allow me to be not-so precise on the semantics: by
programmatic
I don't mean to make theTimer
class something that could be created/instantiated programmatically (which is something that is almost already possible, but could be further improved with aTimerService
class which exposes some methods to return different type of configured timers, in order to call the method once and add the returned timer in the scene); but I mean aTimer
that could be configured and scheduled further more than the currentTimer
, still having theTimer
as a class (inheriting fromProgrammaticTimer
eventually).The
Fixed/Scheduled/Rated
functionality I illustrated before was just one case of possible generalization of theTimer
s that could easily - with very little effort on the C++ side - already improve the usability.But some others could be:
autodisposable: bool
property, which is when aTimer
will automatically disposed (aka.queue_free()
d) when no longer needed (one-shot and multi-shot cases). Would bedefault: false
forTimer
delay: float
property, which is the amount of time in seconds after which theTimer
will start counting down, properly affecting the "one-shot" case before the first and onlytimeout
, and the "multi-shot"/"scheduled" and "infinite"/"looping" cases after each repetition. Would bedefault: .0
forTimer
Also, some more methods of the
ProgrammaticTimer
, or even better aTimerService
exposing some methods as aFactory
pattern:schedule(wait_time: float, repeats: int, delay: float)
, will return a newProgrammaticTimer
instance with those property properly configured, in order to add it to the scene and connect to its signal.from_iso(iso8601_string: String)
will return a newProgrammaticTimer
instance, properly configured as the previous method, but parsing an ISO8601 String with Repeating Intervals, since it is a standardized way of representing Date+Time intervals with repetitions. If we want to keep the Timer simple, Years, Months, Days, Hours and Minutes could be converted to seconds, precisely accounting the memory limitsSo, hoping that the functionalities are clearer, about the naming:
a. A
ScheduledTimer
sounds wrong to name, since a Timer is already Scheduled by nature, and any other reference to a "Schedule" (which is - an activity executed in a timed environment and under precise rules) could be misleading.b. A
FixedTimer
could be a correct naming, except for the fact that being "fixed" is only one of the functionalities of such timer, and does not define the timer itself completelyc. Just
Timer
, accepting the fact that theTimer
class should expose such new functionalities, and eventually have aTimerService
or theSceneTree
itself exposing factory methods to create timers "programmatically".I'm open for further explainations and discussions.
Thanks everyone.
Beta Was this translation helpful? Give feedback.
All reactions