0 votes

Hi, very-new-to-Godot-and-scripting here!

I'm making a 2D platformer.
Short version; I have some variable data in one scene that I want to transfer to another.

More details:
Upon winning the game (critia is to collect 3 coins) I change the scene from Level1.tscn to YouWin.tscn.
In Level1.tscn I have a label for counting how long it took to collect the 3 coins in minutes and seconds, and another label counting how many times the player died before getting the 3 coins.
When the player collects the 3rd coin and gets send to the YouWin.tscn, I would like to display the data from Level1.tscn on how much time was spend and how many times they died.
Just to be clear; I don't want to save this data or store it for some later purpose. I just want to show what it was for that run.

Bonus:
If possible I would like to convert the time (as it is current just text) to a numeric value so I can make some calculation of the score. Like for example the score will be 10,000 minus 'time spend' times 'number of deaths' or something like that.
This is not necessary at all though, so I'm fine with not being able to do it.
(It's just a small project to troll my friends a bit, in case you're wondering why I would make it so weird.)

I have been searching around for a bit and found something about making save/load files but I don't think that will work for me, since the game is going to be uploaded to itch.io. Honestly, I have no idea if this might work for running the game online as well...
I have also attempted some Global singleton things but I haven't managed to make them work as intended.

Here are my scripts;
(I'm a total newbie so these are amalgations of bits and pieces I have found YT tutorials and other QAs)
HUD.gd

extends CanvasLayer

var coins = 0
var deaths = 0
signal game_won

func _ready():
    $Coins.text = String(coins)
    $Deaths.text = String(deaths)


func _on_coin_collected():
    coins = coins + 1
    _ready()
    if coins == 3:
        emit_signal("game_won")
        get_tree().change_scene("res://YouWin.tscn")


func _on_Player1_PlayerDead():
    deaths = deaths + 1
    coins = coins * 0
    _ready()

CounterTime.gd

extends Label

var time = 000
var timer_on = true
var time_passed

func _process(delta):
    if(timer_on):
        time += delta
    var secs = fmod(time,60)
    var mins = fmod(time, 60*60) / 60
    var time_passed = "%02d : %02d" % [mins,secs]
    text = time_passed


func _on_HUD_game_won():
    print(text) #print proves that the timer stoppes correctly on 'game_won'-signal
Godot version Godot v3.4
in Engine by (19 points)

1 Answer

0 votes

A naive solution is to use a singleton as you mentioned. Have a 'score manager' singleton that can store the score while the YouWin scene reads and displays it. A singleton like this will allow you to keep values across scenes.

https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html#doc-singletons-autoload

extends Node

var score = 0
  1. Create a ScoreManager.gd script (like above, or see doc link).
  2. Assign it to 'Auto Load' in project settings.
  3. Make sure it is 'Enabled'.
  4. Assign the score value ScoreManager.score = new_high_score
  5. Read the score value $Label.text = ScoreManager.score
by (418 points)

Thank you for the answer!

Unfortunately I still don't quite understand.
I have done step 1-3 but can't make 4 and 5 work...
Also, I still don't know how to store the actual scores in the ScoreManager. I might be able to figure out the death counter but I'm still confused how to load the final time count.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.