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.
Health Pack Health VariableHilton184Rather than having the ship know how much it should heal, is it good to have the health pack contain that information instead?
Then you could have different health packs / other healing items, which have different heal values?
I did this and it seemed to work.
However I then tried something similar, where I wanted the health pack to only disappear if the ship needed healing. So I made this code do something similar to see if the body entering the health pack had a "needs_health" method. But then going my ship going over the health pack would result in the health pack not disappearing despite the health bar going up, which made me think this approach is not appropriate?
pasted the code in comments below2511Feb. 19, 2024
L5.P1 Energy goes up on collision but I still get a red errorMartinBThis is causing the last check on the list to turn red. It seems to go ok but the error explanation reads:
The energy bar should update to match the ship's energy when the ship touches the energy pack. The energy bar's value property is 0, and the ship's energy is 20. Did you forget to update the energy bar in the _on_area_entered function?
My code below (Excluding the irrelevant bits.)
`var energy := 20.0`
`func _ready() -> void:`
`get_node("UI/EnergyBar").value = energy`
`area_entered.connect(_on_area_entered)`
`func set_energy(new_energy: float) -> void:`
`get_node("UI/EnergyBar").value = energy`
`energy = new_energy`
`func _on_area_entered(area: Area2D) -> void:`
`set_energy(energy + 20.0)`13Feb. 24, 2024
L5.P1 get_node nested under _ready function?RiskyPixelsHey folks, was it intentional that the `get_node(“UI/EnergyBar”).value = energy` was pre-typed under the ready function in the practice? If so, was the goal of the practice for us to learn to make that work, or was the aim for us to relocate that line of code to the `set_energy` function like we did for the `set_health` function during the lesson? I did the latter to pass the practice, but it felt like I wasn’t supposed to?33Feb. 20, 2024
How do you connect a signal to difference scene?slim-lemurI know that signals can be used to trigger functions when certain events happen. However, the examples so far are all within the same scene. I figured that a benefit of signals is that it keeps different scenes decoupled, such that even if the sender or receiver of a signal don't exists, no errors are thrown because each scene is self-sufficient.
To that end, I wanted to know the "best practices" way of connecting a signal to a different scene's function. Ideally, I would be able to connect to a scene's class, so that I don't have to worry if it has been instanced and where the instance is when getting a reference to it's function. (I bet this is somehow possible because it'd be useful to signal static functions: methods that can be used even when their class isn't instantiated. Also, i read the documentation and Godot mentions signals are an implementation of the observer pattern. Which doesn't seem to specify signal sender/receivers have to be objects.)
Like, ideally I'd have a bunch of different health items, and when consumed, they emit a signal and send out their data in the form of a node. (like area_entered(area: Area2D) Then, the player object notices, and uses the passed parameter to modify it's health, using the connected function.42May. 27, 2024
functions order best practiceChainSOVIs the provided code reference a best practice on the order of functions in the script file:
class function overrides
setters/getters
user defined functions
signals
Or is there another best practice?12Mar. 15, 2024
area_entered signal - selective to specific areasPurpleSunriseJust a quick question. If the signal is just area_entered it means that any area 2D that entered will call the function in the signal. Instead what if I want to select only a specific area2D being detected ?
Can it be done with collision layers and masks or is there something to be changed in the code?
And what if I want to have more options for area_entered signal (for example, an area2D could make the healthpack disappear, while another one doesn't but makes something else happen)?
Maybe it will be explained later but I thought to ask now out of curiousity.
Thank you!11Oct. 13, 2024
Got a little confused about the lessons/practices..leafFor L5.P1 and maybe even a lesson before that, I did not fully comprehend that I was to be doing the lessons within the GDQuest tab inside of Godot (despite that I have done other practices thus far). I think the lack of specific direction to where I was supposed to go confused me. I am doing this course daily, but there must be a lapse in memory from guidance in a previous module, and an assumption that we will remember and know that these lessons are going to be in the GDQuest tab. Might I suggest that these lessons always begin with, or state somewhere, "in the GDQuest tab in Godot, do this or that"? The starting instructions quite literally state "Time to put your new skills to good use. Open the workbook project in Godot to follow along.".
I quite literally coded everything I needed for energy to work within the project itself (second progress bar, energy pack pick up (duplicated and changed the color of health pack to blue through Modulate in properties). By the time I realized this was a task within the lessons tab, I was struggling and I wanted to make this work; I had to learn how to use groups to prevent the `area_entered` signal from activating both the health and energy functions at the same time. I am grateful that I messed this up because I learned and tried to solve a problem on my own (albeit I had to get some help from the Godot discord for using groups, but I learned they existed and how to use them with if statements within in the function call).
Hope this experience is helpful for your course!
31Apr. 23, 2024
How is the signal connection available throughout runtime if code in the _ready function only executes once?StonemonkeyAs I understand it, the ready function runs once, after it enters the scene tree. So the way I interpreted is that the area_entered signal is "initialized" once but after we touch a health pack the signal wouldn't be available again.
I tried to move the signal connection into _process and it seemed to work...
Also, I spent a week on M5.L5 because I didn't understand something and everytime I tried to look up info I went down a rabbit hole of more things I didn't understand. I'm not sure If I should have just moved on. I would have just written down the question's I had but I couldn't even tell what I was having trouble with until I looked up information.
I think I have most things cleared up besides the _ready function question.31Mar. 28, 2024
Changing the max valuetesfalconIn my game, I wanted the required score to go up to make it to the next level. At first, I was stuck with leaving the progress bar's max value at 100 and trying to calculate the percentage myself to set the progress value accurately. All the type conversions between floats and integers was getting to be a real pain. Finally, I discovered that I could change progressbar.max_value to what the new level_score needed to be and just set the progressbar.value = current_score. It calculated the percentage for me. Easy-peazy!11Mar. 09, 2024
get_node vs $banky-dosHi is there a differance to these two ways of getting the child node? I think $UI/EnergyBar looks easier to read.21Feb. 24, 2024
Understanding check: Purpose of new_health? And get_node functionality?MrBright01First, apologies, I am sure this is basic, but two questions.
1) What is the purpose of *new_health* in...
```gdscript
func set_health(new_health: int) -> void:
health = new_health
```
As I understand it (presumably incorrectly), *func set_health* creates a new parameter (*new_health*) when accessed, and then assigns *var health* to equal *new_health*, but what's assigning the value for *new_health*? Is it assigned when you use *seat_health (health + 10)* ?
2) So *get_node* is used to update a property on another node, not to get information from that node, and not to access any information in functions, which would use signals?
Thanks ahead of time, just trying to get the why cemented in my head, as it obviously didn't take.20Oct. 16, 2024
L5P1 - Why do we need to use the set_energy function in the _ready function ? GoatzAs we're already using the set_energy function in the on*_*area_entered function, isn't it redundant to use it on the _ready function as well ? Aren't we already updating the energy value and the EnergyBar when the signal is emitted ? 50Sep. 20, 2024
energy bar resets after practice?cheerful-dugongAfter successfully completing the practice (all green checkmarks), if I fly the ship back into view, the energy bar has reset so that it no longer matches the Reference on the right side. Why does this happen?20Sep. 20, 2024
Small error in the examples?detailed-cheetahHello, I noticed a small mistake:
Add the following code to the script:
```javascript
func set_health(new_health: int) -> void:
# ...
get_node("UI/HealthBar").value = health
```
Here the argument is called new_health but we assign health instead, thats an error right?10Sep. 14, 2024
set_health function redundant?PaulIn the lesson code, we created a function for setting health, then passed it the new value for health as an argument when the _on_area_entered function was called. In the mission I simplified the solution so that the new energy was applied directly within the _on_area_entered function, therefore not requiring the additional function.
Are there some benefits to separating this out into its own function in other contexts? My mission code below:
```gdscript
func _on_area_entered(area: Area2D) -> void:
energy += 20.0
get_node("UI/EnergyBar").value = energy
```10Sep. 10, 2024
What is “new health”?MaziHello!
In set_health function the “new_healt” is just the name of the argument and what is important is the type (Int)? That is the cause why health increased when I take a health_pack what gives example +10 integer? 10Aug. 27, 2024
A way to distingish area2d that enteredmrelectronHi team,
i tried to add a black_hole scene as an enemy to avoid, and gave it a damage value, and i gave the health pack a heal value.
and to distinguish between which area2d has entered the ship, i check
```gdscript
func _on_area_entered(area_that_entered:Area2D) -> void:
if "heal_value" in area_that_entered:
set_health(health+area_that_entered.heal_value)
elif "damage_value" in area_that_entered:
set_health(health - area_that_entered.damage_value)
```
my question, is my approach correct? it seems kind of flimsy to depend on a text value to tell an object type. will be introduced to better ways?
i read that there is something called Groups in godot that can be useful in this case maybe?
below is the rest of my code for reference,
health_pack:
```gdscript
extends Area2D
var heal_value:=50
func _ready() -> void:
area_entered.connect(_on_area_entered)
func _on_area_entered(area_that_entered: Area2D)-> void:
queue_free()
```
black_hole:
```gdscript
extends Area2D
var damage_value:=50
func _ready() -> void:
area_entered.connect(_on_area_entered)
func _on_area_entered(area_that_entered: Area2D)-> void:
queue_free()
```
ship:
```gdscript
extends Area2D
var max_speed := 1200.0
var velocity := Vector2(0, 0)
var steering_factor := 3.0
var health:=10
const MAX_HEALTH = 100
const MIN_HEALTH = 0
func _ready() -> void:
area_entered.connect(_on_area_entered)
set_health(health)
func _process(delta: float) -> void:
var direction := Vector2(0, 0)
direction.x = Input.get_axis("move_left", "move_right")
direction.y = Input.get_axis("move_up", "move_down")
if direction.length() > 1.0:
direction = direction.normalized()
var desired_velocity := max_speed * direction
var steering := desired_velocity - velocity
velocity += steering * steering_factor * delta
position += velocity * delta
if velocity.length() > 0.0:
#so we don't rotate UI elements, we will only rotate the sprite
get_node("Sprite2D").rotation = velocity.angle()
func set_health(new_health: int) -> void:
if new_health < MIN_HEALTH:
health = MIN_HEALTH
else:
health = min(new_health,MAX_HEALTH)
get_node("UI/HealthBar").value=health
func _on_area_entered(area_that_entered:Area2D) -> void:
if "heal_value" in area_that_entered:
set_health(health+area_that_entered.heal_value)
elif "damage_value" in area_that_entered:
set_health(health - area_that_entered.damage_value)
```
20Aug. 19, 2024
Incomplete set_health functionshort-term-gnatHi,
in this lesson there are instructions to add following in our code:
**func set_health(new_health: int) -> void:**
**# ...**
**get_node("UI/HealthBar").value = health**
But then there is no update for this function.
In the Code reference it is this complete and correct but I think someone might find it a little bit confusing.10Aug. 05, 2024
Confirming the green signal arrowfaint-reindeerI just want to make sure I understand, the reason we're not getting the little green arrow on the left is because we're manually connecting in _ready() the signal instead of using the Node tab to connect with the Godot interface. Is this correct or is the little green arrow supposed to be visible too?10Jul. 24, 2024
Increasing Progress Bar Max Value?high-level-eelI was trying to add a bit of code to increase the max value of the health bar when the health goes above the limit.
This is what I have, but it doesn't seem to work. I'm not receiving errors, but I don't see the health bar increase. What am I missing?
```gdscript
var bar_size := 100
#...
func set_bar(_new_bar_size: int) -> void:
if health >= bar_size:
bar_size += 100
get_node("UI/ProgressBar").max_value = bar_size
```20Jul. 10, 2024
L5.P1 is my code wrong or is it only possible in one way?bountiful-manateehey guys - i try in this practice to do everything on my own without any help of the hints or something (just to try it out).
func _on_area_entered(area: Area2D) -> void:
get_node("UI/EnergyBar").value += 20
Is this line of code also correct?10Jul. 06, 2024
L5.P1Why can't I directly set the func argument to the get_node?beno```gdscript
func set_health(new_energy: int) -> void:
energy = new_energy
get_node("UI/EnergyBar").value = energy
```
why do I need to add this line?
```gdscript
energy = new_energy
```
I thought I can do this but the test fails
```gdscript
get_node("UI/EnergyBar").value = new_energy
```
It says that the ship's energy pack changed to 0 instead when I dod this.30Jun. 23, 2024
Different solution to player's healthGrichaczHey, I tried to code the player's health before progressing with the lesson to test myself. I came up with the solution below:
Ship code (I paste only the necessary parts):
```gdscript
var health := 10
var max_health := 100
func _ready() -> void:
$UI/ProgressBar.value = health
func update_health(value: int):
health += value
if health > max_health:
health = max_health
$UI/ProgressBar.value = health
```
Health Pack code:
```gdscript
@export var heal_value := 10
func _on_area_entered(area_that_entered: Area2D) -> void:
area_that_entered.update_health(heal_value)
queue_free()
func _ready() -> void:
area_entered.connect(_on_area_entered
```
The code worked, but when following the lesson, I spotted a lot of differences, i.e. get_node instead of referencing the UI with $ and making *on_area_entered* inside the Ship, not health pack. I'd be very grateful if you could provide some feedback.
*With my current knowledge, the only problem I can think about is that if there is no* *"update_health" function in the area that entered the HealthPack, the script may return an error but this could be avoided for example with an if statement and assigning the player to the group.* 10Jun. 21, 2024
Wouldn't be better to pass a "healing_value" var from the healing package and make the healing package itself call the heal() function?Lucas PscheidtI ask this because I have learned before that for best code practice, if we have a object that do something, we should write the code of what it does inside that object's script. For example, the healing package heals the player, so wouldn't be better to let it decide for how much they are going to heal and call from there the heal() function?30May. 29, 2024
I passed the L6.P2 test, but the number of coins calculated by my code was 4, and the correct result was 3. What is the reason? Thanks for the guidancehumble-pelican
extends Area2D
signal target_reached
var max_speed := 1200.0
var velocity := Vector2(0, 0)
var steering_factor := 10.0
var target_position := Vector2(0, 0)
var coins := 0
var energy := 20
func _ready() -> void:
area_entered.connect(_on_area_entered)
set_process(false)
func set_target_position(new_target_position: Vector2) -> void:
target_position = new_target_position
set_process(true)
func _process(delta: float) -> void:
var desired_velocity := position.direction_to(target_position) * max_speed
var steering := desired_velocity - velocity
velocity += steering * steering_factor * delta
position += velocity * delta
if velocity.length() > 0.0:
get_node("Sprite2D").rotation = velocity.angle()
if position.distance_to(target_position) < 10.0:
set_process(false)
target_reached.emit()
func _on_area_entered(area_that_entered: Area2D) -> void:
if area_that_entered.is_in_group("energy"):
energy += 20.0
get_node("UI/EnergyBar").value = energy
elif area_that_entered.is_in_group("coin"):
coins += 1
get_node("UI/CoinsCount").text = "x" + str(coins)60May. 22, 2024
L5.P1 Error won't let me run the project.unwilling-fishWhen i'm trying to run the the practice scene. I get the following error:
E 0:00:02:0316 ui_test_panel.gd:31 @ _ready(): Node not found: "Metadata" (relative to "/root").
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1638 @ get_node()
<Stack Trace> ui_test_panel.gd:31 @ _ready()
20Apr. 04, 2024
Small Typomda2894In the second line you have "In this lesson, we'll work on the remaining two points to ***health*** the player when the ship touches a health pack" when I think you intended to say "In this lesson, we'll work on the remaining two points to ***heal*** the player when the ship touches a health pack."
Not a big deal, but I appreciate all the effort you put into these lessons, so I thought I'd return the favor with a little bit of copyediting.10Mar. 27, 2024
M5.L5 Having Two Area2D's Monitoring each otherSpiRiTsI have a quick question regarding having the 2 Areas in this Lesson monitoring each other. For this example, the "HealthPack" checks for collisions with the ship to delete the node (queue_free), and the Ship checks for collisions with the "HealthPack" to add Health to the ship.
We could have delete (queue_free) the "HealthPack" node within the ship with:
**Ship Script**
`func _on_area_entered(area : Area2D) -> void:`
`set_health(health + 20)`
`area.queue_free()`
However, my question is more related to the method use having both Nodes monitoring each other. Is it possible that the "HealthPack" Node deletes itself before the ship can add health to it? Or this will NEVER be a problem because of the "queue_free()" function executes at the end of the frame.30Feb. 28, 2024
Confusing code exampleZeraWhen the code example is given for updating the health bar, the removal of the line of code for updating the health confused me for a moment and I removed that line in my own code. Didn't take long to figure out my mistake, but I wonder how many other people that's happened to.10Feb. 25, 2024
Final code example has +10EdwardThe directions say health +20, but the final code has +10. My test is not passing with either value because it says the signal is not connected - even though it is connect.
`func _ready() -> void:`
`area_entered.connect(_on_area_entered)`
`# This call updates the health bar to match th`
`set_health(health)`
10Feb. 23, 2024
Snap (Side-By-Side) Layout Break This Webpage's StylingRiskyPixelsI often use the snap layout feature in windows to have both the webpage and the godot editor open at the same time side by side. This worked really well until this current webpage, `code_player_health`. I haven't encountered this on any other pages yet, but it seems to break the page's ability to resize the content to fit the width of the window. Tested and confirmed on chrome and edge.20Feb. 20, 2024
L5.P1 doesn't state how much to add to the energy value.GrumpyI failed L5.P1 as it does not state anywhere how much to add to the energy value when you go over an energy pack?
I was able to figure it because in the failure message it states why it failed.20Feb. 19, 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.