Skip to content

Commit

Permalink
P13 Game Over Score and Highscore
Browse files Browse the repository at this point in the history
Video 13 in series https://www.youtube.com/playlist?list=PL9FzW-m48fn09w6j8NowI_pSBVcsb3V78

Was getting errors after restarting after death and couldn't figure it out.  I realized thanks to this post:
godotengine/godot#71032
that I was not removing the event!  Removed it at the scene unloading and it worked!
  • Loading branch information
aaronmsimon committed Mar 31, 2024
1 parent d25e80f commit 6fb5521
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Galaxy Defiance/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public override void _Ready()
};
}

public override void _ExitTree()
{
gameStats.ScoreChanged -= UpdateScoreLabel;
}

private void UpdateScoreLabel(int newScore)
{
scoreLabel.Text = "Score: " + newScore;
Expand Down
2 changes: 2 additions & 0 deletions Galaxy Defiance/components/GameStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public int Score
EmitSignal(SignalName.ScoreChanged, score);
}
}

public int HighScore { get; set; }
}
20 changes: 20 additions & 0 deletions Galaxy Defiance/menus/GameOver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@

public partial class GameOver : Control
{
[Export] private GameStats gameStats;

private Label scoreValue;
private Label highScoreValue;

public override void _Ready()
{
scoreValue = GetNode<Label>("%ScoreValue");
highScoreValue = GetNode<Label>("%HighScoreValue");

if (gameStats.Score > gameStats.HighScore)
{
gameStats.HighScore = gameStats.Score;
}

scoreValue.Text = gameStats.Score.ToString();
highScoreValue.Text = gameStats.HighScore.ToString();
}

public override void _Process(double delta)
{
if (Input.IsActionJustPressed("ui_accept"))
{
gameStats.Score = 0;
GetTree().ChangeSceneToFile("res://menus/menu.tscn");
}
}
Expand Down
38 changes: 37 additions & 1 deletion Galaxy Defiance/menus/game_over.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[gd_scene load_steps=5 format=3 uid="uid://e5rphks4e2or"]
[gd_scene load_steps=6 format=3 uid="uid://e5rphks4e2or"]

[ext_resource type="Script" path="res://menus/GameOver.cs" id="1_uk1xw"]
[ext_resource type="PackedScene" uid="uid://2y3orfbvdywa" path="res://effects/space_background.tscn" id="2_pj3hb"]
[ext_resource type="Resource" uid="uid://b5dcuimd51boy" path="res://GameStats.tres" id="2_twq68"]
[ext_resource type="LabelSettings" uid="uid://bgxd55k13rvp7" path="res://fonts/title_label_settings.tres" id="3_yj8t1"]
[ext_resource type="LabelSettings" uid="uid://qtag76bu4ccg" path="res://fonts/default_label_settings.tres" id="4_to565"]

Expand All @@ -13,6 +14,7 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_uk1xw")
gameStats = ExtResource("2_twq68")

[node name="SpaceBackground" parent="." instance=ExtResource("2_pj3hb")]

Expand All @@ -33,6 +35,40 @@ text = "Game Over"
label_settings = ExtResource("3_yj8t1")
horizontal_alignment = 1

[node name="ScoreHBoxContainer" type="HBoxContainer" parent="CenterContainer/VBoxContainer"]
layout_mode = 2

[node name="ScoreLabel" type="Label" parent="CenterContainer/VBoxContainer/ScoreHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Score:"
label_settings = ExtResource("4_to565")

[node name="ScoreValue" type="Label" parent="CenterContainer/VBoxContainer/ScoreHBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "0"
label_settings = ExtResource("4_to565")

[node name="HighScoreHBoxContainer" type="HBoxContainer" parent="CenterContainer/VBoxContainer"]
layout_mode = 2

[node name="HighScoreLabel" type="Label" parent="CenterContainer/VBoxContainer/HighScoreHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Highscore:"
label_settings = ExtResource("4_to565")

[node name="HighScoreValue" type="Label" parent="CenterContainer/VBoxContainer/HighScoreHBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "0"
label_settings = ExtResource("4_to565")

[node name="Spacer" type="Control" parent="CenterContainer/VBoxContainer"]
custom_minimum_size = Vector2(0, 16)
layout_mode = 2

[node name="MenuLabel" type="Label" parent="CenterContainer/VBoxContainer"]
layout_mode = 2
text = "Press 'space' to Return
Expand Down

0 comments on commit 6fb5521

Please sign in to comment.