Looks like you're not logged in

Login or Register to continue

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.

  • Question for set_input_as_handledGoatzHello ! I'm not sure I understand what `get_viewport().set_input_as_handled()` does exactly. The documentation says "Stops the input from propagating further down the SceneTree" but I can't picture its effect properly. Do you have some kind of practical example you could give me/us to clear it up ? Thank you so much ! 4 0 Mar. 18, 2025
  • Trouble assigning item and reading error messageSootyHello, I am on my second run through of module 10 and I am still struggling. I ultimately failed the challenge and had to look at the solution at the end. This was the code I wrote but, it obviously doesn't work because all I needed to do was new_pickup.item = rand_item However I couldn't make sense of the error message when the game crashes upon opening the chest. **Invalid assignment of property or key 'texture_pickup' with value of type 'CompressedTexture2D' on a base object of type 'Area2D (Pickup)'.** Here was what I wrote. func _open()-> void: animation_player.play("open") set_deferred("monitoring", false) var rand_item : Item = possible_items.pick_random() var new_pickup : Pickup = PICKUP.instantiate() add_child(new_pickup) new_pickup.texture_pickup = rand_item.texture_pickup 1 0 Mar. 13, 2025
  • I've had some trouble but finally made it!blind-ibisThe code is a mess but I'm happy I managed to make it work. ```gdscript class_name Chest extends Area2D @export var possible_items: Array[Item] = [] @onready var player_inside : Player = null @onready var animation_player: AnimationPlayer = $AnimationPlayer const PICKUP = preload("res://pickups/pickup.tscn") const PICKUP_HEART_DARKPINK = preload("res://pickups/pickup_heart_darkpink.png") const PICKUP_GEM = preload("res://pickups/pickup_gem.png") const SOUND_PICKUP_HEALTH = preload("res://pickups/pickup_health.wav") const SOUND_PICKUP_LIGHTNING = preload("res://pickups/pickup_lightning.wav") func _unhandled_input(event: InputEvent) -> void: if event.is_action_pressed("Interact") and player_inside != null: open() func _on_body_entered(body: Node2D) -> void: if body is Player: player_inside = body func _on_body_exited(body: Node2D) -> void: if body is Player: player_inside = null func open() -> void: animation_player.play("open") monitoring = false spawn_pickup() func spawn_pickup() -> void: if possible_items == []: print("Need to select the possible pickups on inspector") return var item : Item = possible_items.pick_random() var pickup : Pickup = PICKUP.instantiate() if item is HealingItem: item.display_image = PICKUP_HEART_DARKPINK item.sound_on_pickup = SOUND_PICKUP_LIGHTNING elif item is SpeedItem: item.display_image = PICKUP_GEM item.sound_on_pickup = SOUND_PICKUP_HEALTH pickup.item = item add_child(pickup) pickup.set_deferred("monitoring", false) var random_angle := randf_range(0.0, 2.0 * PI) var random_direction : Vector2 = Vector2.RIGHT.rotated(random_angle) var final_offset: Vector2 = random_direction * randf_range(60.0, 100.0) pickup.position = Vector2.ZERO var jump_height: float = 50.0 var apex_offset: Vector2 = final_offset - Vector2(0, jump_height) var drop_tween = create_tween() drop_tween.tween_property(pickup, "position", apex_offset, 0.3) \ .set_trans(Tween.TRANS_SINE) \ .set_ease(Tween.EASE_OUT) drop_tween.tween_property(pickup, "position", final_offset, 0.3) \ .set_trans(Tween.TRANS_BOUNCE) \ .set_ease(Tween.EASE_IN) drop_tween.finished.connect(Callable(self, "_on_drop_tween_finished").bind(pickup)) func _on_drop_tween_finished(pickup : Pickup) -> void: pickup.set_deferred("monitoring", true) ``` 2 0 Mar. 12, 2025
  • How to add a default Texture on ItemrobustaMoveI forgot to hand a texture to my `HealthPack`item in `PossibleItems` I spent way to long searching for why I could not see the item. For `PossibleItems` you need to create the specific item instance then add the texture. I can see this being very tedious when making many chests/items. Why not have the image attached to the item type. I attempted to make a default texture like the following. So that `HealthPack`will always default to the `pickup_heart_pink.png` ```gdscript class_name HealthPack extends Item const PICKUP_HEART_PINK = preload("res://pickups/pickup_heart_pink.png") func _ready() -> void: texture = PICKUP_HEART_PINK ## don't copy this it doesn't work ... ``` This does not work. You also cannot overwrite the `@export variable texture` Is this even an appropriate thing for the HealthPack class to do? Should I be approaching this problem differently? 2 0 Mar. 11, 2025
  • 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? 3 0 Jan. 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. 2 0 Jan. 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_range 3 0 Jan. 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 ``` 5 0 Jan. 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) ``` 1 0 Jan. 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() ``` 1 0 Dec. 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! :) 1 0 Dec. 06, 2024
Site is in BETA!found a bug?