godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.02k stars 21.1k forks source link

Calling a method of your own Reference script within PREDELETE notification fails #31166

Open Zylann opened 5 years ago

Zylann commented 5 years ago

Godot 3.1.1

I made a script class which has a clear() function, for which the goal is to cleanup some external resources. It can be called by code using the class, or at the destruction of the instance. The only destructor logic that I know of is NOTIFICATION_PREDELETE. However... that doesn't work :(

extends Node

class Test:
    var a = 42

    func _notification(what):
        if what == NOTIFICATION_PREDELETE:
            print("Predelete ", a)
            clear() # Breaks here

    func clear():
        print("Clearing Test ", a)

func _ready():
    var test1 = Test.new()
    test1 = null

When I run this, I get the following error:

Attempt to call function 'clear' in base 'null instance' on a null instance.

Which isn't helping here, and makes no sense because _notification was called in the first place. I would like to be be able to call functions of my own script, otherwise I have to entirely copy/paste its contents...

Note: that situation only seems to occur with References. If I inherit Node and free() it, the code runs normally. I tested with inner classes but the same issue happens with file scripts.

Xrayez commented 5 years ago

I've stumbled upon very similar experience in https://github.com/godotengine/godot/issues/6784#issuecomment-480588033.

KoBeWi commented 4 years ago

Hehe prints("Predelete", self, self.a) Output: Predelete [Object:null] 42

No idea what happens, but it's still valid in d711c57d7

KoBeWi commented 3 years ago

Still valid in 16524d4

Jummit commented 2 years ago

Still valid in v3.4

dalexeev commented 1 year ago

Still reproducible in 4.0 RC 1.

akien-mga commented 1 year ago

Still reproducible in 4.2-dev (0606ba7c50ab7606c9db81a2c73162008ca683a6), and it's crashing:

Predelete 42

================================================================
handle_crash: Program crashed with signal 11
Engine version: Godot Engine v4.2.dev.custom_build (c97e4a31ac85839dea31d84a86c9441b7e775273)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /lib64/libc.so.6(+0x36980) [0x7f98954ef980] (??:0)
[2] EncodedObjectAsID::get_object_id() const (/home/akien/Projects/godot/godot.git/./core/io/marshalls.cpp:52)
[3] EditorDebuggerInspector::add_stack_variable(Array const&) (/home/akien/Projects/godot/godot.git/./editor/debugger/editor_debugger_inspector.cpp:236 (discriminator 2))
[4] ScriptEditorDebugger::_parse_message(String const&, unsigned long, Array const&) (/home/akien/Projects/godot/godot.git/./editor/debugger/script_editor_debugger.cpp:466)
[5] ScriptEditorDebugger::_notification(int) (/home/akien/Projects/godot/godot.git/./editor/debugger/script_editor_debugger.cpp:939 (discriminator 14))
[6] ScriptEditorDebugger::_notificationv(int, bool) (/home/akien/Projects/godot/godot.git/./editor/debugger/script_editor_debugger.h:60 (discriminator 14))
[7] Object::notification(int, bool) (/home/akien/Projects/godot/godot.git/./core/object/object.cpp:800)
[8] SceneTree::_process_group(SceneTree::ProcessGroup*, bool) (/home/akien/Projects/godot/godot.git/./scene/main/scene_tree.cpp:948)
[9] SceneTree::_process(bool) (/home/akien/Projects/godot/godot.git/./scene/main/scene_tree.cpp:1020 (discriminator 2))
[10] SceneTree::process(double) (/home/akien/Projects/godot/godot.git/./scene/main/scene_tree.cpp:510)
[11] Main::iteration() (/home/akien/Projects/godot/godot.git/main/main.cpp:3443)
[12] OS_LinuxBSD::run() (/home/akien/Projects/godot/godot.git/platform/linuxbsd/os_linuxbsd.cpp:912)
[13] /home/akien/Projects/godot/godot.git/bin/godot.linuxbsd.editor.dev.x86_64(main+0x15a) [0x574bf40] (/home/akien/Projects/godot/godot.git/platform/linuxbsd/godot_linuxbsd.cpp:76)
[14] /lib64/libc.so.6(+0x236b7) [0x7f98954dc6b7] (??:0)
[15] /lib64/libc.so.6(__libc_start_main+0x85) [0x7f98954dc775] (??:0)
[16] /home/akien/Projects/godot/godot.git/bin/godot.linuxbsd.editor.dev.x86_64(_start+0x21) [0x574bd21] (??:?)
-- END OF BACKTRACE --
================================================================