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.
My solution for random angle + dumb questionPurpleSunriseHi!
My solution to random angle! This one was pretty fun! It's a bit different from the reference code but I didn't use hints.
```gdscript
class_name Weapon extends Node2D
@export var bullet_scene : PackedScene
@export var max_range : float = 400.0
@export var max_bullet_speed : float = 600.0
#Is this how you convert degrees to radians...?
var min_angle = -(10 * (PI/180))
var max_angle = (10 * (PI/180))
func _ready() -> void:
randomize()
func _physics_process(delta: float) -> void:
if Input.is_action_just_pressed("shoot"):
shoot()
# I thought I'd try with the _input as well, it works but I guess it's not very functional.
# If I have to set_physics_process(false) I'll also have to set_process_input(false)
# while having everything inside the _physics_process() is better I guess?
#func _input(event: InputEvent) -> void:
#if event.is_action_pressed("shoot"):
#shoot()
func shoot():
var bullet : Bullet = bullet_scene.instantiate()
bullet.global_position = global_position
bullet.global_rotation = global_rotation
bullet.speed = max_bullet_speed
bullet.max_range = max_range
bullet.rotate(randf_range(min_angle - rotation, max_angle - rotation))
get_tree().current_scene.add_child(bullet)
```
**Dumb question:**
I don't like all the @tool stuff much, at the moment I feel it kind of complicates things for me to have functions in the script that are not really functional, as I am still learning and I see @tool as a secondary thing. Is it wrong to think that way and leave it out for the time being or should I make effort and start including that in my codes from now to be confident with it later on?
Thank you!00Jan. 21, 2025
setting the bullet position after add_child() causes problemsDMK`new_bullet.global_position = global_position`
`get_tree().current_scene.add_child(new_bullet)`
This works, but
`get_tree().current_scene.add_child(new_bullet)`
`new_bullet.global_position = global_position`
This causes a problem where you can see the bullet get spawned somewhere else around the player and then immediately jumps to the weapon's position.
I know that theoretically the execution for both lines of code happen before the frame is rendered so this shouldn't be possible, which is why I'm very confused.
Should I just set all the properties of the new node before add_child()?
Is there a reason you add_child() first and then set the properties?30Jan. 14, 2025
About the Angle of the bullethumble-pelicanI have a question, why is it that when I add the function bullet.rotation += randf_range(-random_angle/2.0,random_angle/2.0), my bullets go in all directions, is that right? Because I watched the video of the last challenge example in line100Jan. 03, 2025
A few things here that I don't quite get.PJ1. Why isn’t "random_angle" actually random and instead has a fixed value here?
2. What does "radians_as_degrees" mean?
3. Why are we modifying the "global_rotation" property, but then only editing "rotation" on line 33?
4. When using `randf_range`, why do we divide "random_angle" by 2.0? Wouldn’t it have been more convenient to set the correct property value on line 6 instead?10Jan. 03, 2025
When to call add_child, before defining properties or after? Lucas PscheidtI had this code for the shoot function before reading the reference:
```gdscript
var bullet_instance : Bullet = bullet.instantiate()
bullet_instance.global_position = global_position
bullet_instance.rotation = rotation
bullet_instance.max_range = bullet_max_range
bullet_instance.speed = bullet_speed
timer_cooldown.start(cooldown_seconds)
get_tree().current_scene.add_child(bullet_instance)
```
I was wondering, is there any problem with calling `add_child()` after defining the properties? One thing I noticed was that if I called this line before setting the properties, my bullets would instantly call the `destroy` function upon spawning. This happened because, in their `_ready()` function, I was setting `initial_position = global_position` to calculate `max_range`.
To fix that, I used `call_deferred()` to delay the `initial_position` assignment, and it worked. However, if I call `add_child()` after setting the bullet's properties, it also works without needing `call_deferred()`.
Are there any drawbacks to this method?30Jan. 02, 2025
AudioStreamChuckAm I missing something or is there no code for making the firing sound in the reference code?
10Jan. 01, 2025
Accidentely forgot to remove @tool after testing the warning stuff in bullet.gd & now the editor auto close itself even on a restart◆ LPIt probably happened because I removed the code for Engine.is_editor_hint() condition.
I know I can change the script outside the Godot to fix this but I still find it a little amusing. The bullet moves & then closes the editor after reaching it's destination.10Dec. 17, 2024
Radians vs DegreesPaulI'm having a bit of a tangle getting my head around Radians. I understand how they are different to degrees, but my brain hasn't got used to calculating/visualising them.
For instance, in final challenge, I did:
```gdscript
##A value of 0 is perfect accuracy. Higher values increase spread.
@export_range(0, 20) var weapon_spread := 5
func shoot() -> void:
...
var spread_modifier := randf_range(0 - weapon_spread, weapon_spread)
bullet.global_rotation_degrees = global_rotation_degrees + spread_modifier
```
This makes sense in my brain, and it works here. I know it's going to add a few degrees either side to the angle of the shot. But I fear 'cheating' with the `rotation_degrees` property now might cause me some problems in the future? Should I try to stick with radians or is there a time and place to use either.40Dec. 05, 2024
Why do you add @tool in the Code Reference?MuryanIt feels like it’s unnecessary.
Without @tool, it seems the entire code still works fine.30Nov. 24, 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.