From a076ba07be6464dc19417f361a6e20f65b04ac56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mikrut?= <41945903+qarmin@users.noreply.github.com> Date: Thu, 27 May 2021 09:14:04 +0200 Subject: [PATCH] Add time singleton (#51) --- Autoload/Autoload.gd | 15 +++++++++++++-- Start.gd | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Autoload/Autoload.gd b/Autoload/Autoload.gd index 2461dbf..4fd08f3 100644 --- a/Autoload/Autoload.gd +++ b/Autoload/Autoload.gd @@ -13,6 +13,7 @@ var time_to_show: int = 25 * 1000 # How long test works in miliseconds var time_for_each_step : int = -1 var os +var time # Each scene runs alone const alone_steps : Array = [ @@ -29,7 +30,14 @@ func _init(): else: os = ClassDB.instance("_Platform") - start_time = os.get_ticks_msec() + if ClassDB.class_exists("Time"): + time = ClassDB.instance("Time") + elif ClassDB.class_exists("_OS"): + time = ClassDB.instance("_OS") + else: + time = ClassDB.instance("_Platform") + + start_time = time.get_ticks_msec() # In case when user doesn't provide time time_for_each_step = time_to_show / (alone_steps.size()) @@ -43,7 +51,7 @@ func _init(): func _process(delta: float) -> void: - var current_run_time : int = os.get_ticks_msec() - start_time + var current_run_time : int = time.get_ticks_msec() - start_time # While loop instead if, will allow to properly flush results under heavy operations(e.g. Thread sanitizer) while current_run_time > time_to_print_next_time: @@ -56,3 +64,6 @@ func _process(delta: float) -> void: func _exit_tree(): os.free() + if !ClassDB.class_exists("Time"): + time.free() + diff --git a/Start.gd b/Start.gd index 6e6b203..cff5f04 100644 --- a/Start.gd +++ b/Start.gd @@ -9,11 +9,11 @@ var array_with_time_to_change: Array = [] func _ready(): for i in Autoload.alone_steps.size() + 1: - array_with_time_to_change.append(Autoload.os.get_ticks_msec() + i * Autoload.time_for_each_step) + array_with_time_to_change.append(Autoload.time.get_ticks_msec() + i * Autoload.time_for_each_step) func _process(_delta): - if current_scene < Autoload.alone_steps.size() - 1 && Autoload.os.get_ticks_msec() > array_with_time_to_change[current_scene + 1]: + if current_scene < Autoload.alone_steps.size() - 1 && Autoload.time.get_ticks_msec() > array_with_time_to_change[current_scene + 1]: current_scene += 1 for child in get_children():