Godot Version
4.3.stable
Question
So I have a very basic player script at the moment and coins I can spawn where I click. I’d like the player to be able to pick up these coins and increment a coin counter by a value depending on which coin they pick up.
From what I can guess, I need to use the on_body_entered signal somewhere and make something execute whenever the player passes through the coin body, but what I don’t understand is how to do this when the coin does not exist initially. Whenever I try to drop it into the coin script, the option to choose the player is not there, because it doesn’t know its going to be a sibling yet.
I’m sorry I’m very new to Godot, but here is my script for spawning the coins. The player script is just the default Characterbody2D script for now but I do plan to change that later.
extends Node2D
var copper_coin = preload(“res://copper_coin.tscn”)
var silver_coin = preload(“res://silver_coin.tscn”)
var gold_coin = preload(“res://gold_coin.tscn”)
@onready var character_body_2d = $CharacterBody2Dfunc _physics_process(delta):
if Input.is_action_just_pressed(“left_click”):
var value = randi_range(0,2)if value == 0: inst_copper(get_global_mouse_position()) if value == 1: inst_silver(get_global_mouse_position()) if value == 2: inst_gold(get_global_mouse_position())
func inst_copper(pos):
var copper_coin_instance = copper_coin.instantiate()
copper_coin_instance.position = pos
copper_coin_instance.linear_velocity = Vector2(randf_range(-100,100),randf_range(-100,-200))
add_child(copper_coin_instance)func inst_silver(pos):
var silver_coin_instance = silver_coin.instantiate()
silver_coin_instance.position = pos
silver_coin_instance.linear_velocity = Vector2(randf_range(-100,100),randf_range(-100,-200))
add_child(silver_coin_instance)func inst_gold(pos):
var gold_coin_instance = gold_coin.instantiate()
gold_coin_instance.position = pos
gold_coin_instance.linear_velocity = Vector2(randf_range(-100,100),randf_range(-100,-200))
add_child(gold_coin_instance)