SDL3 Oyun Programlama ve Godot Oyun Motoru sayfalarımız yayında...

Ana sayfa > Oyun programlama > Godot game engine > Adding score

Adding score

To create a score counter in the game and display the obtained values ​​on the screen, follow the steps below.

1. While the "game" Scene and "Game" Node are selected, create a normal Node and change its name to "GameManager".

2. Move the "GameManager" Node right under the "Game" Node.

3. While "game" Scene and "GameManager" Node are selected, click on the "Attach a new or existing script to selected node" button.

4. In the window that opens, click on the button at the far right of the "Path" line.

5. In the window that opens, go to the "scripts" directory, select the "game_manager.gd" file and click the "Open" button.

6. When we return to the previous window, click the "Create" button.

7. This process creates a script for the "GameManager" Node. edit the script as follows.


extends Node

var score = 0

func add_point():
	score += 1
	print(score)

8. The add_point() function is not called yet. In order to give a reference to the "GameManager" Node so that this function is called every time the Player collects a Coin and the score value is increased by one, select the "coin.gd" script.

9. To access the "GameManager" Node without having to define a path, right-click the "GameManager" Node while it is selected and select "% Access as Unique Name".

10. Drag and drop the "GameManager" Node onto the line where the _on_body_entered() function is defined. Before releasing, hold down the "Ctrl" key. This will create a variable.

11. Edit the "coin.gd" script as follows.


extends Area2D

@onready var game_manager: Node = %GameManager

func _on_body_entered(_body: Node2D) -> void:
	game_manager.add_point()
	queue_free()

12. When we run the program, every time the Player collects a Coin, the "gameManager.gd" add_point() function is called from "coin.gd", the score variable value is increased by 1 and written to the output screen.

13. Copy two labels (Label4 and Label5). For Label5, in the "Inspector" tab, change the "Horizontal Alignment" value to "Center" and the "Autowrap Mode" value to "Word". The values ​​and settings will be as follows.

14. Change the name "Label5" to "ScoreLabel" and drag and drop it under the "GameManager" Node.

15. Drag and drop the "ScoreLabel" Node onto the line where the add_point() function is defined. hold down the "Ctrl" key before dropping. This process creates a variable.

16. Edit the "game_manager.gd" script as follows.


extends Node

var score = 0
@onready var score_label: Label = $ScoreLabel

func add_point():
	score += 1
	score_label.text = "You collected " + str(score) + " coins."

17. When we run the program, every time the Player collects a Coin, the "gameManager.gd" add_point() function is called from "coin.gd", the score variable value is increased by 1 and the "ScoreLabel" value is updated.

Save all scenes with the Ctrl-S keys.