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

Cannot access member variable from lambda body due cyclic reference #70592

Closed
SomeRanDev opened this issue Dec 26, 2022 · 5 comments · Fixed by #80923
Closed

Cannot access member variable from lambda body due cyclic reference #70592

SomeRanDev opened this issue Dec 26, 2022 · 5 comments · Fixed by #80923

Comments

@SomeRanDev
Copy link
Contributor

SomeRanDev commented Dec 26, 2022

Godot version

4.0.beta10

System information

Windows 10

Issue description

For top-level class variables, a lambda stored in a dictionary cannot reference the dictionary members.

Steps to reproduce

Assuming MyAutoload.gd is an autoload class, this code that worked in beta 8, no longer works in beta 10.

var f = func():
	MyAutoload.MyDict._str = "New string."

var MyDict: Dictionary = {
	"_str": "",
	"fun": f
}

# Parser Error: Could not resolve member "_OtherClass".

Autoload is unrelated however, just wanted to demonstrate how there's an inconsistent error if this is intended behavior. The following occurs regardless of autoload. If the "MyAutoload" part is removed from the lambda, a "cyclic reference" error occurs.

var f = func():
	MyDict._str = "New string."

var MyDict: Dictionary = {
	"_str": "",
	"fun": f
}

# Parser Error: Could not resolve member "f": Cyclic reference

Minimal reproduction project

Issue_70592_BugProject.zip

@Zireael07
Copy link
Contributor

From the mention of autoload, possible duplicate of #70495

@adamscott
Copy link
Member

@RobertBorghese Notwithstanding the possible duplicate, please join a minimal reproduction project when code is needed to demonstrate the issue, even if the code seems simple. It's so much easier for the developers to unzip the project and run it, rather than to create a new project and copy-paste the code.

@SomeRanDev
Copy link
Contributor Author

SomeRanDev commented Dec 26, 2022

From the mention of autoload, possible duplicate of #70495

Apologies for bad reporting, this occurs without autoload as well. Updated original post.

@RobertBorghese Notwithstanding the possible duplicate, please join a minimal reproduction project when code is needed to demonstrate the issue, even if the code seems simple. It's so much easier for the developers to unzip the project and run it, rather than to create a new project and copy-paste the code.

No problem! Updated post with minimal project! 👌

@adamscott
Copy link
Member

CC @rune-scape

Seems related to this code:

Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source) {
if (p_source == nullptr && parser->has_class(p_class)) {
p_source = p_class;
}
if (p_class->base_type.is_resolving()) {
push_error(vformat(R"(Could not resolve class "%s": Cyclic reference.)", type_from_metatype(p_class->get_datatype()).to_string()), p_source);
return ERR_PARSE_ERROR;
}

@Daiz
Copy link

Daiz commented Mar 3, 2023

Just ran into this issue myself while playing around with Godot 4.0 stable. Small sample code to demonstrate:

var stats = {
	health = 100
}

var actions = {
	hit = func(damage := 1):
		stats.health -= damage
		if (stats.health <= 0):
			actions.die()
,	die = func():
		queue_free()
}

This currently errors on actions.die() with Could not resolve member "actions": Cyclic reference.

@dalexeev dalexeev changed the title Cannot reference dictionary members from a lambda stored in the same dictionary (worked in beta-8) Cannot access member variable from lambda body due cyclic reference Aug 23, 2023
@YuriSizov YuriSizov added this to the 4.2 milestone Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants