#Multiple of the same object that contains unique data inside?

4 messages · Page 1 of 1 (latest)

sleek juniper
#

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?

unique thunder
#

I think your main issue is that you have it setup so that pressing the "debug" key adds a fish, but all of your instances of fish tank are listening to the same event.

#

In your finished game where are you looking to put the add fish button? if you put a button on your fish_tank scene then each fish_tank instance will listen to it's own button press.

sleek juniper
#

In a finished game I plan to add those fishes by buying them through shop and placing them inside a selected tank of chose. I want to have a few of those tanks in a main scene that are also purchasable and could vary in sizes (increasing or decreasing max capacity).