Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.
The get_tree/root/get_node/"Game/Runner" didn't work.right-cheetahFor some reason using the third way of referencing a sibling node in the course text kept breaking the game, giving me a null instance:
func get_global_player_position() -> Vector2:
return get_tree().root.get_node("Game/Runner").global_position
However I was able to continue along using the get_node("../Runner").global_position approach.
I'm wondering what I may have done wrong to make Godot throw up errors with the former approach.21Nov. 17, 2024
Final challengesPurpleSunriseHello,
Super interesting lessons! Here's how I implemented the two challenges. It's all in the ready function of the game scene. I don't know if the second challenge should be done like this but I thought it was the simplest way to do it and the result is similar to the video:
```gdscript
func _ready() -> void:
_runner.set_physics_process(false)
_bouncer.set_physics_process(false)
_count_down.start_counting()
_count_down.counting_finished.connect(func()->void:
_runner.set_physics_process(true)
await get_tree().create_timer(0.5).timeout
_bouncer.set_physics_process(true)
_bouncer.acceleration = 200.0
# I can switch it back to 1200.0 if I want the bouncer to have slow
# acceleration only at the beginning of the run and not all the time.
# For now, I thought it's nice to always have a bit of advantage on the bouncer.
)
```
Looking forward to the next lesson!30Dec. 12, 2024
Why does this not work for moving bouncer toward runner?◆ LPI removed the stuff we assigned to direction & distance variable in bouncer.gd & just kept their variable declaration with Vector2 & float type.
Then, I added the direction & distance calculation in game.gd, using _bouncer.global_position & _runner.global_position, but I got this error when running:
> Invalid assignment of property or key 'direction' with value of type 'Vector2' on a base object of type 'CharacterBody2D (bouncer.gd)'.
Here is the code:
```gdscript
# bouncer.gd
var direction: Vector2
var distance: float
# game.gd
# Would moving it to _physics_process() work?
func _ready() -> void:
...
var bouncer_position = _bouncer.global_position
var runner_position = _runner.global_position
_bouncer.direction = bouncer_position.direction_to(runner_position)
_bouncer.distance = bouncer_position.distance_to(runner_position)
```30Dec. 12, 2024
This is my second challenge. Did I succeed? It works fine, but I wonder if my code on the speed setting is wrong? thank youhumble-pelican```gdscript
extends CharacterBody2D
@onready var dust: GPUParticles2D = %Dust
@onready var _runner_visual_purple: RunnerVisual = %RunnerVisualPurple
@onready var hit_box: Area2D = %HitBox
@export var max_speed := 600.0
@export var current_max_speed := 300.0
@export var acceleration := 1200.0
@export var deceleration := 1080.0
func _ready() -> void:
hit_box.body_entered.connect(func (body:Node) ->void:
if body is Runner:
get_tree().call_deferred("reload_current_scene")
)
func _physics_process(delta: float) -> void:
var direction := global_position.direction_to(get_global_player_position())
var distance := global_position.distance_to(get_global_player_position())
current_max_speed = move_toward(current_max_speed,max_speed,acceleration * delta)
velocity = direction * current_max_speed
var speed := max_speed if distance >100 else max_speed * distance /100
var desired_velocity := direction * speed
velocity = velocity.move_toward(desired_velocity,acceleration * delta)
dust.emitting = true
move_and_slide()
if velocity.length()>0.0:
_runner_visual_purple.angle = rotate_toward(_runner_visual_purple.angle,direction.orthogonal().angle(),8.0*delta)
var current_speed_percent := velocity.length() / current_max_speed
_runner_visual_purple.animation_name = (
RunnerVisual.Animations.WALK
if current_speed_percent < 0.8
else RunnerVisual.Animations.RUN
)
else :
_runner_visual_purple.animation_name = RunnerVisual.Animations.IDLE
func get_global_player_position() ->Vector2:
return get_tree().root.get_node("Game/Runner").global_position
```10Dec. 09, 2024
My second challengehumble-pelican```gdscript
extends CharacterBody2D
@onready var dust: GPUParticles2D = %Dust
@onready var _runner_visual_purple: RunnerVisual = %RunnerVisualPurple
@onready var hit_box: Area2D = %HitBox
@export var max_speed := 600.0
@export var current_max_speed := 300.0
@export var acceleration := 1200.0
@export var deceleration := 1080.0
func _ready() -> void:
hit_box.body_entered.connect(func (body:Node) ->void:
if body is Runner:
get_tree().call_deferred("reload_current_scene")
)
func _physics_process(delta: float) -> void:
var direction := global_position.direction_to(get_global_player_position())
var distance := global_position.distance_to(get_global_player_position())
current_max_speed = move_toward(current_max_speed,max_speed,acceleration * delta)
velocity = direction * current_max_speed
var speed := max_speed if distance >100 else max_speed * distance /100
var desired_velocity := direction * speed
velocity = velocity.move_toward(desired_velocity,acceleration * delta)
dust.emitting = true
move_and_slide()
if velocity.length()>0.0:
_runner_visual_purple.angle = rotate_toward(_runner_visual_purple.angle,direction.orthogonal().angle(),8.0*delta)
var current_speed_percent := velocity.length() / current_max_speed
_runner_visual_purple.animation_name = (
RunnerVisual.Animations.WALK
if current_speed_percent < 0.8
else RunnerVisual.Animations.RUN
)
else :
_runner_visual_purple.animation_name = RunnerVisual.Animations.IDLE
func get_global_player_position() ->Vector2:
return get_tree().root.get_node("Game/Runner").global_position
```10Dec. 09, 2024
Challenges Solutions (Did I get them right?)Abdul Hannan AhmedHi, Sir Nathan/Jad,
Here is my code for the challenges. Could you give your feedback? Thanks in advance.
Challenge 1:
```gdscript
extends Node2D
@onready var _finish_line: FinishLine = %FinishLine
@onready var _count_down: CountDown = %CountDown
@onready var _runner: Runner = $Runner
@onready var _bouncer: CharacterBody2D = $Bouncer
func _ready() -> void:
_runner.set_physics_process(false)
_bouncer.set_physics_process(false)
_finish_line.body_entered.connect(
func (body: Node2D) -> void:
if body is not Runner:
return
var runner := body as Runner
runner.set_physics_process(false)
var destination_position := (
_finish_line.global_position + Vector2(0, 64)
)
runner.walk_to(destination_position)
runner.walked.connect(_finish_line.pop_confettis)
)
_count_down.counting_finished.connect(
func() -> void:
_runner.set_physics_process(true)
await get_tree().create_timer(0.5).timeout
_bouncer.set_physics_process(true)
)
```
Challenge 2:
```gdscript
extends CharacterBody2D
@export var max_speed := 600.0
@export var acceleration := 1200.0
@export var increasing_factor := 15.0
@export var current_speed := 450.0
@onready var _dust: GPUParticles2D = %Dust
@onready var _runner_visual: RunnerVisual = %RunnerVisualPurple
@onready var _hit_box: Area2D = $HitBox
func _ready() -> void:
_hit_box.body_entered.connect(func(body: Node) -> void:
if body is Runner:
get_tree().call_deferred("reload_current_scene")
)
func _physics_process(delta: float) -> void:
var direction := global_position.direction_to(get_global_player_position())
var distance := global_position.distance_to(get_global_player_position())
var speed := current_speed if distance > 100 else max_speed * distance / 100
var desired_velocity := direction * speed
current_speed = move_toward(current_speed, max_speed, delta * increasing_factor)
velocity = velocity.move_toward(desired_velocity, acceleration * delta)
move_and_slide()
if velocity.length() > 100.0:
_runner_visual.angle = rotate_toward(_runner_visual.angle, direction.orthogonal().angle(), 8.0 * delta)
var current_speed_percent := velocity.length() / max_speed
_runner_visual.animation_name = (
RunnerVisual.Animations.WALK
if current_speed_percent < 0.8
else RunnerVisual.Animations.RUN
)
_dust.emitting = true
else:
_runner_visual.animation_name = RunnerVisual.Animations.IDLE
_dust.emitting = false
func get_global_player_position() -> Vector2:
return get_tree().root.get_node("Game/Runner").global_position
```10Nov. 15, 2024
Challenge Solution CheckMoKTHey Nathan/Jad, I wanted to run my challenge solutions by you and see if you had any suggestions or efficiencies you would recommend?
For the first one I had no issues:
```gdscript
## game.gd
@onready var _timer: Timer = %Timer
func _ready() -> void:
_count_down.counting_finished.connect(
func() -> void:
_runner.set_physics_process(true)
_timer.start()
_timer.timeout.connect(
func() -> void:
_bouncer.set_physics_process(true)
)
)
_count_down.start_counting()
```
For the second, I thought about using lerp() and range(), I was trying to find a step function where I could state a min & max value, and an increment, but after reading the help docs I realized that's not what those are used for, I don't think that exists in gdscript?
I also thought about using a timer that limits the bouncers speed for a duration then ups it, but that would cause to quick a jump in speed.
In the end I used an if loop to increment:
```gdscript
## bouncer.gd
@export var max_speed_limit := 600.0
@export var max_speed := 0.0
@export var speed_increment := 600.0
func _physics_process(delta: float) -> void:
#...
+if max_speed < max_speed_limit:
max_speed += speed_increment * delta
+if max_speed > max_speed_limit:
max_speed = max_speed_limit
var speed := max_speed if distance > 100 else max_speed * distance / 100
#var speed := max_speed if distance > 100 else max_speed * distance / 100
var desired_velocity := direction * speed
```80Nov. 09, 2024
Test question where the correct answer is "Game/Player"ram876In the test question where the correct answer is "Game/Player", we don't actually have a Player node. I was confused for a second when I didn't see this node.10Nov. 06, 2024
Store reference to playerBrain Siege GameworksDoing this lesson, I used a different method of getting the players position. Namely making the function to get the player position get the player node instead and then storing the value of the get_player() function in a global variable in the _ready() function. Then used this reference var for the target position for the Bouncer.
I'm sure it's better to do it this way, considering you eliminate having to get a node twice every frame update.30Oct. 30, 2024
Another cool challenge/feature to addiguessfiveAfter just making it to the finish line and beating the AI, the game does not stop to play the confetti. The AI is not stopping and collides with player causing the game to reset before celebrating the player's win.
A challenge could be making the AI stop when the player enters the finish line.
If you would like to add this feature then wait to look at my approach below.
...
...
```gdscript
# Add in main game script
_finish_line.body_entered.connect(func(body: Node):
if body is Runner:
bouncer.set_physics_process(false)
bouncer.set_animation_to_idle()
)
# Add in `Bouncer` script
func set_animation_to_idle():
_runner_visual.animation_name = RunnerVisual.Animations.IDLE
_dust.emitting = false
```30Oct. 29, 2024
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.