Lethn It's a very spaghettified way of doing it 🙂 with too much repetition and poorly readable code.
You know that famous quote from Fred Brooks:
Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won't usually need your flowcharts; they'll be obvious.
What he meant is that code based on well organized data structures is easy to read and write. So you should always strive to solve a problem by figuring out best data structures to represent it. With well chosen and well named data structures, your code can become short, expressive and self-explanatory.
In this case, you're basically making a scheduler so it'd be a good idea to make each task a separate function and keep them in a dictionary together with times they need to be executed at. Now simply go through this dictionary, check if time has come to execute a task. If yes, call the task's function. To simplify time comparison, your little scheduler's internal time keeping could be in seconds, but you'll perhaps want to input time in nice HH:MM:SS format So you'll need a helper function that converts from human friendly format to seconds.
To put it all together:
extends Node
# helper function to convert HH:MM:SS strings to seconds
func to_seconds(time_string:String):
var tokens = time_string.split(":")
return int(tokens[0]) * 60 * 60 + int(tokens[1]) * 60 + int(tokens[2])
# Scheduled tasks
func eat():
print("eating")
func sleep():
print("sleeping")
# Schedule dictionary with task functions as values and task times (in seconds) as keys
var schedule = {
to_seconds("10:22:12"): eat,
to_seconds("10:22:14"): sleep,
}
# helper function to determine if time has passed
func time_mark_passed(time_mark, time_last_frame, time_now):
return sign(time_last_frame - time_mark) <= 0 and sign(time_now - time_mark) > 0
# current time and previous frame time
var time_now = to_seconds("10:22:10")
var time_last_frame = time_now
# do the scheduling in real time
func _process(delta):
for task_time in schedule.keys():
if time_mark_passed(task_time, time_last_frame, time_now):
schedule[task_time].call()
time_last_frame = time_now
time_now += delta
Note that I used real time here but it'd be exactly the same with some virtual ingame time.