Spawning two fish on the scene, squid eats one but can't eat the other?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By $maug

I’ve been playing with Godot for about a week. I’m trying to expand on the Your First Game example with the squids. Here’s my goal…

  1. fish spawns
  2. player eats the fish
  3. fish changes to a skeleton animation and falls off the scene via gravity

So far all works great except I can’t get the signal to fire again until the dead fish falls off the screen (and is queue_free’d), like if two fish spawn at the same time only the first fish the player touches will work.

Main (Node)
--Player (Area2D)
-----AnimatedSprite
-----CollisionBody2D
--Fish (RigidBody2D)
-----AnimatedSprite
-----CollisionBody2D
-----VisibilityNotifier2D
--FishSpawner (Timer)

Here’s my code…

Main

func _on_FishSpawner_timeout() -> void:
	print("new fish")
	var fish = load("res://Fish.tscn").instance()
	add_child(fish)
	
func _on_Player_eat() -> void:
	print("FISH EATEN")
	$Fish/AnimatedSprite.set_animation("dead")
	$Fish.gravity_scale = 10

Player

signal eat

func _on_Player_body_entered(body: Node) -> void:
	if body.name == "Fish":
		print("Fish Touched")
		emit_signal("eat")

Fish

func _on_VisibilityNotifier2D_screen_exited():
	queue_free()

So, the fish spawns every X seconds thanks to the Timer. The player touches the fish. The fish animation changes to the “dead” set and gravity is applied to make it fall off the screen. When the fish gets off the screen it frees up.

All this works fine…for a single fish. If I try to touch two+ fish with the player before the initially touched fish falls off the screen nothing happens, not even the print command.

How can my hungry squid gobble up multiple fish at once?

:bust_in_silhouette: Reply From: $maug

Ok, I figured it out. I’ll leave the solution here for anyone else who might need it.

I set the class_name in Fish and then added this to Player:

func _on_Player_body_entered(body: Node) -> void:
    if body is Fish:
        body.get_node("AnimatedSprite").set_animation("dead")
        body.gravity_scale = 10 
        emit_signal("eat")

Since I’m changing the animation and gravity in the Player now, I removed all that from “on Player eat”.

Now it works great!