I'm trying to create a fish tank that holds a specific amount of fishes inside it, and I want to give a player the ability to have multiple of them. With only one object it works good but when I add more than one tank to the main scene it shares the data between them which causes the curr_fishe variable to do some shenanigans.
extends StaticBody2D
var max_fishe : int = 5
var curr_fishe : int = 0
var can_add_fishe : bool = true
@export var fishe_scene : PackedScene
func _ready():
SignalBus.connect("fishe_died", Callable(self, "remove_fishe"))
func _process(_delta):
#Check if the fishes can be added to the tank
if curr_fishe < max_fishe:
can_add_fishe = true
else:
can_add_fishe = false
print(curr_fishe)
#Spawn fishes with the debug button for now
func _input(event):
if event.is_action_pressed("debug"):
add_fishe()
#Spawn fishes
func add_fishe():
if can_add_fishe:
var fishe = fishe_scene.instantiate()
add_child(fishe)
curr_fishe += 1
return curr_fishe
#Remove fishes when they die
func remove_fishe():
curr_fishe -= 1
return curr_fishe
I don't know how complicated it would be but my guess is to probably make some sort of manager node that every time the new tank is created/spawned it gives it a unique ID and holds other variables. But how would I access those variables later if I had to? For example, if I wanted to remove a specific fish from a specific tank?