I’m looking for a method to notify my class when a hot reload occurs. I read this thread, where two solutions were suggested.
Modify a const dictionary - since const is reloaded every hot reload, you can detect it easily.
Compare with get_script().source_code and see if something changed.
I tried solution 1, but I have build errors indicating the dictionary is (correctly) read-only. Perhaps that solution worked in the past, but not on the latest Godot version.
For solution 2, get_script() only fetches the current class, but my modifications are in an extending class, so it doesn’t quite work either.
Perhaps a global script with a build number? I’m spitballing here, not sure if this would work:
# Totally untested...
extends Node
var build_number: int = 0
var cached_build_number: int = 1 # Ignore the first load...
signal reloaded
func _ready() -> void:
build_number += 1
func _process(_delta: float) -> void:
if cached_build_number < build_number:
cached_build_number = build_number
print("We have rebuilt it, stronger. We had the technology.")
reloaded.emit()
The logic here is that build_number and cached_build_number should retain their values through a hot reload (I think?), but the reload will call _ready() and increment build_number.
Edit: Actually, thinking about it, it could be simplified…
# Totally untested...
extends Node
var build_number: int = 0
var cached_build_number: int = 1 # Ignore the first load...
signal reloaded
func _ready() -> void:
build_number += 1
if cached_build_number < build_number:
cached_build_number = build_number
print("We have rebuilt it, stronger. We had the technology.")
reloaded.emit()
This approach relies on hot reload calling _ready, which it does not. In fact, hot reload is eerily quiet, calling no lifecycle methods nor emitting any notifications.