Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cyclic reference of Scene and its Script #27048

Closed
GlaDos28 opened this issue Mar 14, 2019 · 4 comments
Closed

Cyclic reference of Scene and its Script #27048

GlaDos28 opened this issue Mar 14, 2019 · 4 comments

Comments

@GlaDos28
Copy link
Contributor

GlaDos28 commented Mar 14, 2019

Godot version:
3.1

Issue description:

Consider a scene SomeScene.tscn and its script SomeScene.gd.

In 3.0 one could write

const SelfScene = preload("res://SomeScene.tscn")

and use scene instancing in code. But now this is forbidden, and the project doesn't work.

I understand that there is real cyclic dependence, but I did not find anything on how to bypass this problem correctly according to code style guide. I don't want to use load instead of preload because it is slower, and really not needed logically. Several scenes can have the same script, but in real life almost every scene has a unique script resource, and it is convenient to be able to address to self scene in script resource using compiler abilities ( preload).

Steps to reproduce:

Create SomeScene.tscn and SomeScene.gd, attach script to scene then add line to .gd file:

const SelfScene = preload("res://SomeScene.tscn")
@Zylann
Copy link
Contributor

Zylann commented Mar 14, 2019

As I understood, it was not really supposed to work in 3.0 and it was the source of other bugs which are now fixed in 3.1.
Using preload makes a cyclic reference because loading SomeScene.tscn also loads its script, and also what that script preloads.

You should indeed use load now. load won't create a cycle because it will store the reference in the instances of that script, not the script itself.

Did you measure its speed? IMO it is not slower, the only difference is when loading will happen, but depending on how you instance this scene in your game, there might not be a difference.

@GlaDos28
Copy link
Contributor Author

GlaDos28 commented Mar 14, 2019

I think it is not comfortable to use. In not all cases You can replace

const SelfScene = preload("res://SomeScene.tscn")

with

var SelfScene = load("res://SomeScene.tscn")

What if there are static functions in script that use instancing? Then You should write the following to work:

const SELF_SCENE_PATH = "res://SomeScene.tscn"

static func create_and_smth_else(params):
    var instance = load(SELF_SCENE_PATH).instance()

It is rather dirty, I think.

@Zylann
Copy link
Contributor

Zylann commented Mar 14, 2019

If there are static functions you want to use, you can preload the script, not the whole scene.
I'm not even sure how Godot could support this reference cycle tbh. Before it basically caused leaks.

@Calinou
Copy link
Member

Calinou commented Sep 1, 2021

Closing in favor of godotengine/godot-proposals#460, as feature proposals are now tracked on the Godot proposals repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants