Best way to create collectibles

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

Honestly it’s several questions in one, if this is not possible please tell me for next questions. I am quite initiated in Godot but I have certain notions. I’m in the part of creating objects for the player to collect, for now only coins. This is the structure of the scene where it all happens.

enter image description here

However, I have encountered a small problem. First I present the way I have programmed the coin scene (It should be clarified that I only place the code corresponding to the currency.):

enter image description here

The way to put them on my level is as follows:

export (PackedScene) var Coin
var CoinPos

CoinPos = $CoinSpawn.get_children()

for co in range(len(CoinPos)):
	coin = Coin.instance()
	add_child(coin)
	coin.position = CoinPos[co].position

Based on the position nodes I place the coins with a for, there will be the number of coins according to the number of position nodes. All right so far, now I’ll show you the coin scene code:

extends Area2D

signal get_coin

func _ready():
	pass

# warning-ignore:unused_argument
func _on_Coin_body_entered(body):
	$Collision.call_deferred("set", "disabled", true)
	$Sound.play()
	hide()
	emit_signal("get_coin")

Everything works fine, I manage to pick up the coins and they disappear, but the problem came when I saw that I couldn’t connect a signal (get_coin) to the main scene because as such it wasn’t instantiated in this one, but it packs up (I still don’t grab the thread to the packing up of scenes).

My doubts are:

  1. Is it possible to get what I’m looking for with the code this way or
    should I change?
  2. Is the way the coin scene is structured okay? I mean the use of an
    Area2D node
  3. (Extra question) Can I apply gravity to the coins? I mean, the way I
    have programmed all the coin is static at its spawn point. I would
    like that after the coin is generated it is quiet and it is above
    the floor, like the player, I tried that the root node of the coins
    was type RigidBody2D but although I managed that the coin achieved
    that effect, the player pushed them and didn’t grab them.

I repeat, if there are too many things you can help me just by connecting the coins to the main scene, because what I am looking for is to generate several coins in specific places, thank you.

:bust_in_silhouette: Reply From: Jalkhov

Ok, I discovered the solution by trial and error, I just added the following line after creating the coin instance in the for loop.

coin.connect("get_coin", self, "coin_getted")

Can help me to apply gravity to Area2D from Coin Scene?

Hello, to add movement to coins you can use an animation node and make collision follow sprite coordinates

lukifah | 2021-12-28 18:35