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.
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.