The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I made a coin scene and when the player collide with it the coin disappears and the score goes higher but it's not working.

here is the coin code:

extends Area2D

signal e_grabbed

func _on_coin_body_entered(body):
    if body.name == "player":
        emit_signal("e_grabbed")
    queue_free()

and this is the main scene code:

func spawn():
    var coin_scene = load("res://scenes/coin.tscn")
    randomize()
    var coin = coin_scene.instance()
    var random = Vector2(rand_range(30,230),-4)
    coin.set_position(random)
    coin.connect("e_grabbed", self, "_on_e_grabbed")
    $coins.add_child(coin)

func _on_e_grabbed():
    print("grgrg")

it doesn't print 'grgrg'

in Engine by (62 points)

Did you try adding a debugger to the _on_coin_body_entered(body) function and checking if the body entering is actually what you expect it to be? For example, you might have a player but the name is not matching 'player'.

1 Answer

0 votes

Ah, I see an issue. In your Coin.gd, you left queue_free() outside of the if loop.

extends Area2D

signal e_grabbed

func _on_coin_body_entered(body):
    if body.name == "player":
        emit_signal("e_grabbed")
        queue_free()

That's all I would do. If the system fusses, try putting the Player scene into a Group called "player", and change if body.name == "player" to if body.is_in_group("player").

In mainscene.gd, I'd put randomize() into the _ready() function, and have var coin_scene = preload("res://scenes/coin.tscn") between the topmost function, and the extends line.

Apologies if this is a bit of a mess.

by (199 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 Frequently asked questions and 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 [email protected] with your username.