Skip to content

Instantly share code, notes, and snippets.

@worldsayshi
Created January 7, 2025 20:23
Show Gist options
  • Save worldsayshi/261e8d824719d92f74ddfb4bef8afd39 to your computer and use it in GitHub Desktop.
Save worldsayshi/261e8d824719d92f74ddfb4bef8afd39 to your computer and use it in GitHub Desktop.
Detect when Godot performs hot reload
class_name FileWatcher extends Node
var last_hash: String = ""
func watch_file(filepath: String, callback: Callable):
var timer = Timer.new()
# Function to get file hash
var get_hash = func():
var file = FileAccess.open(filepath, FileAccess.READ)
return file.get_as_text().sha256_text() if file else ""
# Function to check for changes
var check_changes = func():
var current_hash = get_hash.call()
if current_hash != last_hash and current_hash != "":
last_hash = current_hash
callback.call()
# Setup timer
add_child(timer)
timer.wait_time = 1.0
timer.connect("timeout", check_changes)
timer.start()
last_hash = get_hash.call()
print("Watching file for changes...")
# This script will set up a FileWatcher that with a certain interval checks if the current file has changed.
# When it has changed, on_hot_reload will be called. There you can recreate whatever you want to recreate.
# It's somewhat hacky but it's for making the dev experience nicer so you can disable it for prod builds.
extends Node
var file_watcher = preload("res://FileWatcher.gd").new()
func _ready():
add_child(file_watcher)
file_watcher.watch_file("res://Main.gd", on_hot_reload)
func on_hot_reload():
print("Hot reload happened!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment