So when you start a level its timed and when you finish it shows how long you took to finish it but it starts timing when you open the game not when you start the level. Also when you finish the level it keep timing it so if you restart or go to another level the time will be continue
EX: in the main menu for 2 seconds so when you start a level it starts at 2 seconds instead of 0. You Finish a level in 18 seconds and go to a next one the time will continue and start at like 19 seconds
#Global timer script:
var time: float = 0.0
var game_started_at: int
func start_game_timing() -> void:
game_started_at = Time.get_ticks_msec()
func get_current_game_time_string() -> String:
var current_timestamp := Time.get_ticks_msec()
var duration = current_timestamp - game_started_at
var timestring = "%02d:%02d.%03d"%[(duration / 60000), (duration / 1000) % 60, duration % 1000]
return timestring
func stop() -> void:
set_process(false)
#Level script
#the func below makes it so if you touch the flag sprite aka checkpoint and your name is plauer than you go to win screen and its supposed to stop the time
func _on_checkpoint_body_entered(body: Node2D) -> void:
if body.name == "player":
get_tree().change_scene_to_file("res://Main Stuff/Scenes/win_screen.tscn")
GlobalTimer.stop()
#the time label shows the time and how long your taking to complete the lecel
func _process(delta: float) -> void: -> void:
$CanvasLayer/TimeLabel.text = GlobalTimer.get_current_game_time_string()
level_began_at = GlobalTimer.get_current_game_time_string()
#if you press f you restart and should restart the time
if Input.is_action_just_pressed("restart"):
get_tree().reload_current_scene()
GlobalTimer.time = 0
#win screen
func _process(delta: float) -> void:
$CanvasLayer/Time.text = GlobalTimer.get_current_game_time_string()
set_process(false)
Sounds like you need to use start_game_timing() somewhere in your code. The function doesn’t run by itself and if you don’t use this function then the integer game_start_at will be zero, aka the game starting
You sure have some code that runs exactly at the moment when the new run starts. Put it where that code is because that’s the moment you want to start measuring time.
hey so i put it in the process function in the level script but now it just switches between 00:006 and 00:00.007 quickly. I put it on the current game string but it did the same thing
the timer during the level displays how long you were in the menu and stuff and not in the level but it doesnt affect the win screentime because that actually shows how long it took. EX: Took 2 seconds in the main menu then when the level starts it will shows 2 seconds and stay like it but at the end of the level if you took 8 seconds it will show 8 seconds. Oh also the time doesnt add up anymore I replaced the level process to ready
That’s not what was asked, it appears there is a lot of things there were useful in _process, only one part shouldn’t be in _process and is incorrect in general.
Keep your old _process
Use GlobalTimer.start_game_timing() inside a new_ready
GlobalTimer.time does nothing, anywhere you set GlobalTimer.time = 0 should probably be replaced by GlobalTimer.start_game_timing()
Make sure to format your code pastes, you did the first one right, use three ticks
so update i made a new _ready function and has #GlobalTimer.start_game_timing*()*
I also changed the old process function that turned into ready back to process the problem is now it will disaply 00:00.006 and 00:00.007 very quickly and at the end of the level it will not show how long it actually took to complete the level
Looks like your main point of confusion stems from not knowing when to call your timing functions. Currently, your GlobalTimer class only has two usable functions: start_game_timing() and get_current_game_time_string(). The function stop() makes no sense implemented like that because time is not actually kept by the GlobalTimer itself, but by engine’s Time object that cannot really be stopped.
Your current setup works sorta like a stopwatch. start_game_timing() represents the reset/start knob while get_current_game_time_string() is equivalent of looking at the stopwatch.
You need to call those functions at the appropriate choke points in the rest of your code:
start_game_timing() - should to be called only once when the level starts
get_current_game_time_string() - should to be called every frame while the level is running and its return value assigned to your time label.
So you need to identify those choke points and put those calls there.
But we can do better. It’d probably be easier for you to think about your GlobalTimer as something you can start and stop. The existing code can be refactored to work like that. We can do even better - let GlobalTimer automatically find and update your label, making it super-convenient to use.