#+HTML_HEAD:
#+HTML_HEAD:
#+title: Section 03 - Lesson 15 | Scoring
* Links
- [[../../toc.org][TOC - Godot Notes]]
- [[https://www.udemy.com/course/jumpstart-to-2d-game-development-godot-4-for-beginners/learn/lecture/45070487#announcements][S03:L26.15 - Scoring]]
* Notes
** add a Label node
- add Label node to Root node of Game
- child of CanvasItem and Control
- NOT a Node2D
- appears in upper left corner of the 2D screen
#+ATTR_HTML: :width 600px
[[file:../../../_share/media/albert/img/section03/S03_L15_EX01.png]]
*** set label text
- go Scene->Label node->Inspector->Label->text
- type in whatever you want, e.g. '0000' for score
*** make the size bigger
- Scene->Label node->Inspector->Control->Theme Override
- set the font and font size
*** move the label
- Scene->Label node->Inspector->Control->Layout->Transform
- change the Position values
*** if using shadow and offsets
- you set the colors in Theme Overrides
- change shadow offset and outline size in
- Inspector->Label->Theme Overrides->Constants
** Displaying the score
*** create a var to hold the score
#+BEGIN_SRC gdscript
var _score: int
#+END_SRC
*** register when the paddle hits a gem
- currently this is a signal on the paddle node
- disconnect paddle from receiving the signal bc we only want game node to receive it
**** in the game scene
- Scene->Paddle->Node->Signals
- Area2D -> ~area_entered~
- set the register to the Game node
*** create a reference the Label node
- purpose: modify what the Label displays from the script
**** using onready
1. type it in
#+BEGIN_SRC gdscript
@onready var score_label: Label = $Label
#+END_SRC
2. drag and drop
- grab the node from the Scene panel
- hold down CMD
- drop it on the script
*** update the score and the label
- update the _score variable in receiving method
- set the text property in the label reference to the score
#+BEGIN_SRC gdscript
func _on_paddle_area_entered(area: Area2D) -> void:
_score +=1
label.text = str(_score)
#+END_SRC
** remove the gem from the scene if it hits the padde
- the area2D node passed into the receiver method is a reference to the gem node
- so you can use the queue_free method to remove it from the scene queue
#+BEGIN_SRC gdscript
area.queue_free()
#+END_SRC