Anyway to know when RefCounted get freed?

Godot Version

v4.2.1.stable.mono.official [b09f793f5]

Question

I am creating a state machine with node-less scripts, because of this I have gotten into a side track reading about refcounted vs Objects.

Is there anyway to see when a RefCounted object has been freed?

I haven’t tried it, but I guess this could work?

What users might not realize is that notifications exist for types other than Node alone, for example:


public override void _Notification(int what)
{
    if (what == NotificationPredelete)
    {
        GD.Print("Goodbye!");
    }
}

Tested it in gdscript, seems to work:

class_name DeleteTestObject
extends RefCounted

func _notification(what):
	if what == NOTIFICATION_PREDELETE:
		print("Bye")

Create object and unreference it after 1 sec

extends Node

var object: DeleteTestObject

func _ready():
	object = DeleteTestObject.new()

	get_tree().create_timer(1).timeout.connect(func(): object = null)

Prints “bye”

Thank you, I will give it a try!

Yep, This seems to work. Thank you very much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.