Godot Version – v4.2.1
Hi! For a bit of background, I am working on a 2D platformer which has a main character (CharactedBody2D node) and several objects the player interacts with, which come from one RigidBody2D node ‘spawned’ by a spawner script. The spawner instantiates the object, and I use a series of if statements to check the proximity of the player to the Area2D of the object so the player can pick it up (in reallity, the sprite of the object and the collision layer are momentarily hidden and the player can perform certain actions until they drop the object again).
extends Node2D
var balloon_scene = preload("res://scenes/Balloon.tscn")
var new_balloon_scene = balloon_scene.instantiate()
func spawn_balloon(balloon):
balloon = new_balloon_scene
balloon.set_collision_layer(1)
balloon.set_collision_mask(1)
balloon.set_position(self.position)
print("\nspawn_balloon()")
print("- BalloonSpawner is: %s" % self)
print(" - Spawning balloon at position at: %s" % self.position)
var balloon_sprite = balloon.find_child("Sprite2D")
balloon_sprite.show()
func remove_balloon(balloon):
balloon.set_collision_layer(2)
balloon.set_collision_mask(2)
var balloon_sprite = balloon.find_child("Sprite2D")
balloon_sprite.hide()
In the character code:
Custom signal
func tiny_guy_grabs_balloon():
emit_signal("grabbed_balloon")
emit_signal("position_with_balloon")
Where it checks whether the main character meets criteria to grab the object:
if Input.is_action_just_pressed("activate") and is_close_to_balloon:
print("\nCharacter pressed activate")
$Sprite2D.play("baloonfloat")
print("\nPlaying animation of char with baloon")
has_balloon = true
print("grabbed_balloon at pos: %s" % self.position)
tiny_guy_grabs_balloon()
The custom signal called in the spawner node:
func _on_tiny_guy_grabbed_balloon():
get_tree().call_group("BalloonSpawners", "test_connection")
print("Trying to call group here")
can_drop_balloon = true
print("_on_tiny_guy_grabbed_balloon() - can_drop_balloon=%s" % can_drop_balloon)
print("_on_tiny_guy_grabbed_balloon() - balloon=%s" % self)
Now, onto the problem – when the player picks up and drops the object for the first instance it works fine. However, for all subsequent instances of the object the spawner hides the object at the right place, but never ‘respawns’ it (the second instance remains hidden and can_drop_balloon never becomes true). I have tried to put all instances in a group and emit a custom signal to the whole group, but it doesn’t seem to work.
Is this a problem with the underlying logic of how Godot operates or could it be a faulty signal not reaching all instances? Would appreciate any advice on this.
Thanks!
P.S. I’ve tried to include relevant snippets of code. I’m also new to both programming and Godot, so please keep that in mind!