Value becomes Null on runtime when script changed

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

I have a base scene(A), and the base scene makes use of the onready keyword(base scene script) to get a child node. Then create a scene(B) which instance the base scene and create a script which also extends the base script(B inherits from A), compiling the program the collision object will be assigned to the onready in the base, if I click a button which changes the script at runtime(B script), the onready in A becomes Null.

Note: The new script attached at runtime also extends the base class

My question:
I noticed the ready function for the based was called only once when program ran, but the B which extends A was called twice when program ran and when script was changed. Can someone please kindly explain to me how that happened.

Thank you.

Your description is hard to follow. Consider including diagrams or, better, a minimal scene showcasing your problem.

a fact maybe related to your issue:

  • when class B extends class A, and they both have a _ready() method, both their _ready methods are run (so B didn’t override A’s _ready())

putper | 2023-06-06 00:18

:bust_in_silhouette: Reply From: Just_a_Bee

If a node has one script, and then its script is changed to another during runtime all variables like that are lost. _ready() is only called when the node enters the scene tree for the first time, so it isn’t being called when you change the script. Hope this helps!

Yep thanks these what was actually looking for, lastly what of signals for better explanation will add a code example:
If I have a node and the node attaches a script. The script:

var timer: Timer = Timer.new()
func _ready():
    timer.auto start = true
    timer.connect(“timeout”, callable_method)
    add_child(timer)

Then also again at runtime change the script, it going to throw an error that signal method not found in new script.
Error:

emit_signalp:Error calling signal ‘timeout’ to callable:‘Node_Type(newScriptName)::method’: Method not found

How do I disconnect the signal

Venex2004 | 2023-06-06 11:00

You can call the disconnect method of the signal to disconnect it. Your post says you’re using Godot 4 but your code example looks like it’s for Godot 3.

In Godot 4 this would look like:

timer.timeout.disconnect(callable_method)

In Godot 3 this would look like:

timer.disconnect("timeout", callable_method)

Just_a_Bee | 2023-06-06 13:41