Reference Counting

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

Was watching a tutorial and the person made this statement in reference to node management, " To better manage nodes, create a pool of objects and reuse the node object."

What does this mean and look like as I have no idea? Is this the same as object pooling?

TYIA!

:bust_in_silhouette: Reply From: Wakatta

Yes.

In essence Load/Unload uses resources through MEMORY read/write operations that make CPU > MEMORY cycles noticeable when done in quick succession or with multiple objects at once

Visual Representation

The term literally operates Like a Pool filled with water
Lets say you need to clean your hands, compare using a pool to a bottle of water and you immediately see which is more tedious when multiple hands need to be washed.

Code Example

var node = preload("res://demo.tscn")
var pool = Array() #Creates empty array named pool

func _ready():
    for x in range(10):
        var water = node.instance()
        water.name = str(x)
        add_child(water)
        pool.append(water)

func _remove_water(water_instance):
    remove_child(water_instance) 
    #removed from scene tree but still in memory and still in pool

func _get_unused_water():
    for water in pool:
        if not water.is_inside_tree(): #lol
            return water

Recommendations

Use Groups to track pooled used and unused objects

Thank you for your detailed answer!

trejordan | 2021-04-18 12:26

Groups are a lot slower than Arrays, so use Arrays instead.

Solstice Projekt | 2022-02-15 11:06