0 votes

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?

Godot version 3.3.2.stable.official
in Engine by (19 points)

1 Answer

0 votes
Best answer

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!

by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.