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.
Why won't Pickup.new() work?DMKInstead of `var pickup: Pickup = preload("pickup.tscn").instantiate()`, I tried `var pickup: Pickup = Pickup.new()`, expecting it to work like Timer.new().
But it shows the errors:
> pickup.gd:4 @ _ready(): Node not found: "%PickupSprite" (relative to "/root/Node2D/Chest/@Area2D@2").
>
> pickup.gd:5 @ _ready(): Node not found: "%AudioStreamPlayer2D" (relative to "/root/Node2D/Chest/@Area2D@2").
And the item is not spawned.
I just didn't like the idea of hard-coding the file path, so this is what I tried.
Is there a way around it?30Jan. 15, 2025
About the two tweenhumble-pelicanHi, I was wondering if in the chest example code, creating an animation for pickup creates a tween animation, why is the second animation still a tween? Won't it overwrite the previous tween? Why not give the second tween another name? Thank you very much.20Jan. 10, 2025
Issues with more than 1 Healing Item Resource, please help!Lucas PscheidtSo, I tried making 2 different Item Healing Resources from the same script. One for detecting collisions in a small area and 1 in a bigger area (small areas should be for chest only). The issue is that, depending on how I set up the item pickup code to instantiate and to define variables, instead of only spawning the small detection area ones, it also changes all of my item pickups objects from the scene to that small area. I lost more than 2 hours testnig diferent things and trying to understand the issue, but I couldn't understand it 100%. I found a fix, but it is not very good. Basically I have this function to define properties in my item_pipckup script:
func update_resource_variables(item_resource: Item):
item_audio.stream = item_resource.sound_on_pickup
item_audio.volume_db = item_resource.sound_volume
item_audio.pitch_scale = item_resource.sound_pitch
point_light_2d.enabled = item_resource.light_enabled
point_light_2d.energy = item_resource.light_energy
point_light_2d.color = item_resource.light_color
detection_collision.shape.radius = item_resource.detection_range
And I was calling this function at set_item() function, to perform simillary to item_sprite property. But it was causing that issue of transforming all resourcers from the scene to the small detection area when oppening the chest, but when I moved this function from the set() to the ready(), it worked. But not 100%, the collision damage on my small one stays big, even though it acts like a small (it is very hard to explain). Other thing, if the spawn_items() on my chest script is like this, it works:
func spawn_items(item: Item)->void:
var random_angle := randf_range(0, 2*PI)
var random_direction := Vector2(1,0).rotated(random_angle)
var random_distance := randf_range(120, 250)
var final_position := global_position + (random_direction * random_distance)
var item_pickup_instance:= ITEM_PICKUP_TSCN.instantiate()
add_child(item_pickup_instance)
item_pickup_instance.global_position = final_position
item_pickup_instance.item_resource = item
BUT if it is like this, it doesn't and cause the issue of transforming all objects into small detection area ones
func spawn_items(item: Item)->void:
var random_angle := randf_range(0, 2*PI)
var random_direction := Vector2(1,0).rotated(random_angle)
var random_distance := randf_range(120, 250)
var final_position := global_position + (random_direction * random_distance)
var item_pickup_instance:= ITEM_PICKUP_TSCN.instantiate()
item_pickup_instance.global_position = final_position
item_pickup_instance.item_resource = item
add_child(item_pickup_instance)
And here are the rest of the code relevent to this bug in my item pickup (the code like this doesn't occur the bug but also doesn't set my properties other than sprite texture the right way):
func set_item(new_item: Item)-> void:
item_resource = new_item
if item_sprite != null and item_resource != null and item_resource.item_image != null:
item_sprite.texture = item_resource.item_image
func _ready() -> void:
update_resource_variables(item_resource)
set_item(item_resource)
assert(item_resource != null, "No Item resource assigned!")
func update_resource_variables(item_resource: Item):
item_audio.stream = item_resource.sound_on_pickup
item_audio.volume_db = item_resource.sound_volume
item_audio.pitch_scale = item_resource.sound_pitch
point_light_2d.enabled = item_resource.light_enabled
point_light_2d.energy = item_resource.light_energy
point_light_2d.color = item_resource.light_color
detection_collision.shape.radius = item_resource.detection_range30Jan. 07, 2025
Why does this not work in this case and where to use preloade+instantiate vs new() ?Lucas Pscheidt```gdscript
func spawn_items()->void:
for item: Item in chest_items:
var item_pickup: ItemPickup = ItemPickup.new()
get_tree().root.add_child(item_pickup)
item_pickup.item_resource = item
```50Jan. 06, 2025
Sticking to the chest for a short timeram876Hello! I've noticed that if I walk up and collide with a chest and immediately point the character in the opposite direction, it feels like he's stuck to the chest for a while. At first, I thought it had something to do with the physics of the chest. For the collision, I added a physical body to it. But then I realized that the sticking was due to the character's decelerate time. Therefore, for the case when the character collides with something, I added stronger deceleration. That's how I solved my problem for this. If there was no collision, decelerate_more = 1
```gdscript
if get_slide_collision_count():
decelerate_more = 1000
velocity = velocity.move_toward(Vector2.ZERO, decelerate * delta * decelerate_more)
```10Jan. 01, 2025
When using animation player for spawing pickup, started having issues with other normal pickup's position◆ LPI tried using animation player in pickup scene to animate them when spawning them from chest.
Soon after that, all of the pickup's position in my level_test scene moved to origin. And they keep moving back to origin even if I change their position manually.
After fiddling here & there, I found a fix. By setting the animation player to be active only when I need to spawn items. Removing animation player also solves the position issue.
But I don't understand why it is happening.
Screenshot of the pickup with animation player, [https://imgur.com/a/NYul2D6](https://imgur.com/a/NYul2D6)
I was having issues with the position of pickup instance in level_test scene(included in the screenshots)
Here is the pickup & chest script:
```gdscript
# chest.gd
@tool
extends Area2D
@onready var animation_player: AnimationPlayer = %AnimationPlayer
@export var possible_items: Array[Item] = []
var chest_opened: bool = false
var _player: Player = null
func _ready() -> void:
if Engine.is_editor_hint():
return
body_entered.connect( func (body: Node2D) -> void:
if body is Player:
_player = body
if body is not Player:
_player = null
)
func _process(_delta: float) -> void:
pass
func _unhandled_input(_event: InputEvent) -> void:
if Input.is_action_just_released("interact") and _player != null:
open_chest()
func open_chest() -> void:
if possible_items == [] or chest_opened == true:
return
animation_player.play("chest_opened")
var pickup: Pickup = preload("res://pickups/pickup.tscn").instantiate()
pickup.item = possible_items.pick_random()
add_child(pickup)
pickup.animation_player.active = true
pickup.animation_player.play("spawn")
chest_opened = true
func _get_configuration_warnings() -> PackedStringArray:
var warrnings := PackedStringArray()
if possible_items == null:
warrnings.append("No possible items have been added to the Chest.")
return warrnings
# pickup.gd
@tool
class_name Pickup extends Area2D
@export var item: Item = null: set = set_item
@onready var animation_player: AnimationPlayer = %AnimationPlayer
@onready var sprite_2d: Sprite2D = %Sprite2D
@onready var audio_stream_player_2d: AudioStreamPlayer2D = %AudioStreamPlayer2D
func _ready() -> void:
if Engine.is_editor_hint():
return
set_item(item)
body_entered.connect( func(body: Node2D) -> void:
if body is Player:
item.use(body)
set_deferred("monitoring", false)
visible = false
audio_stream_player_2d.play()
audio_stream_player_2d.finished.connect(_destroy)
)
func set_item(value: Item) -> void:
item = value
if sprite_2d != null:
sprite_2d.texture = item.texture
if audio_stream_player_2d != null:
audio_stream_player_2d.stream = item.item_sound
func _destroy() -> void:
queue_free()
```10Dec. 18, 2024
Variable names inconsistenciesRuben TeijeiroIn the challenge you suggest the name possible_items but in the code you are using possible_items as variable name.
Also the random_direction variable is different in the challenge and in the final code sample.
it's a small nitpick but it would be great to have it consistent. Thanks! :)10Dec. 06, 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.