godot icon indicating copy to clipboard operation
godot copied to clipboard

Fix preload cyclic references

Open adamscott opened this issue 3 years ago • 0 comments

This PR is the follow-up to #65672. This PR works in tandem with #65664 for fixing cyclic references issues.

This PR makes possible to use cyclic references with preloaded scripts.

It's really simple. Every preload() call of a gdscript file load them as shallow scripts using GDScriptCache::get_shallow_script(). Then, if the GDScript analyser fails to get a property of a GDScript instance, then it tries to load it completely before retrying. It makes scripts fully load when needed, which resolves the issue where scripts were fully loaded ahead of time and, thus, making it impossible to resolve cyclic references.

So, this code works with this PR, which returns this error on master: Constant value uses script from "res://b.gd" which is loaded but not compiled.:

# main.gd
const A = preload("res://a.gd")

func _ready() -> void:
	A.hello()  # The console will print: "A.HELLO_WORLD: hello, world!"

# a.gd
extends Node

const B = preload("res://b.gd")

const HELLO_WORLD: = "hello, world!"

static func test() -> void:
	B.test()

# b.gd
extends Node

const A = preload("res://a.gd")

static func test() -> void:
	prints("A.HELLO_WORLD:", A.HELLO_WORLD)

Minimal reproduction project: cyclic3.zip

Fixes #58181 Fixes #61043 Fixes #32165

adamscott avatar Sep 13 '22 17:09 adamscott