Godot-proposals: Allow cyclic references

Created on 10 Feb 2020  路  12Comments  路  Source: godotengine/godot-proposals

Describe the project you are working on:

  • Networking packet system
  • Celestial bodies, script hierarchy

Describe the problem or limitation you are having in your project:

Use Case: Packets

Static constructor wrappers are impossible to make due to Parser Error: Using own name in class file is not allowed (creates a cyclic reference)

Consider the following code

extends Packet
class_name PacketPing

var message: String

static func create(message: String) -> PacketPing:
    var packet: PacketPing = PacketPing.new() #// Cyclic reference

    packet.message = message

    return packet

#// We may need multiple initializers that do completely different things
#//static func from_bytes(bytes: PoolByteArray) -> PacketPing:

It is impossible to make a static packet constructor wrapper for this since PacketPing refers to itself

Use Case: Scripts in a hierarchy

image

extends Node
class_name CelestialBody

var mass: float
var radius: float

#// Check if we are orbiting another CelestialBody
func is_orbiting() -> bool:
    return get_parent() is CelestialBody #// Cyclic reference

What I don't understand: Cyclic references work fine in almost every other programming language


Cyclic references that work fine in 5 of the most popular programming languages

C#:

public class A
{
    public static A Create()
    {
        return new A(); // This works fine!
    }
}

Java:

public class A {
    public static A create() {
        return new A(); // This works fine!
    }
}

C++:

class A {
public:
    static A create()
    {
        return A(); // This works fine!
    }
};

JavaScript:

function A() {
        this.create = () => new A(); // This works fine!
}
//new A().aa();

Python:

class A:
    @classmethod
    def create(cls):
        return A() # This works fine!

PHP: It should be noted that cyclic references actually do not work in PHP


Describe how this feature / enhancement will help you overcome this problem or limitation:
Cyclic references are completely valid for good code. It is needed to make named, static, wrapped constructors.

Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:
GDScript code samples above would run without errors since cyclic references should be valid

Describe implementation detail for your proposal (in code), if possible:
Cyclic references cannot be implemented without changing the scripting engine

If this enhancement will not be used often, can it be worked around with a few lines of script?:
No, right now there is no good way to work around cyclic references. is operator can be worked around by comparing the script with load()

Is there a reason why this should be core and not an add-on in the asset library?:
This can not be worked around with an addon. Cyclic references are a core part of the scripting engine

core gdscript

All 12 comments

Related to https://github.com/godotengine/godot/issues/21461. There are plans to solve this in 4.0.

I had the same issue yesterday.
One thing for now which can work is:

Overriding the get_class() function and use that to check if itself is of a certain type. Not sure if performance is well for this but at least I could get rid of cyclic reference warnings while keeping most of my types.

I think this can be closed, since we will solve this anyway and there's already an issue being tracked in the main repository.

Isn't that issue about a specific problem related to class_name and is? This proposal is general for all cyclic examples

Closing for now, Duplicate of godotengine/godot#21461

Another instance of (future) cyclic references which can happen while serializing GDScript instances: godotengine/godot#33137 (currently just errors out without crashing at least). 馃槢

@akien-mga Re-open this since we are migrating to proposals?

There's no need to reopen, the issue is already tracked in the main repo as a bug.

From that issue:

Certain uses of class_name can produce cyclic errors in situations where there are no cyclic references

So isn't that unrelated?

@nathanfranke the issue evolved from the initial description to cover all cases. Don't worry, what's in this proposal will also be allowed.

@vnen I suggest to re-open this proposal to prevent further duplicate proposals, see previous #595, #1629, #1737, even if this is already tracked in the main repository.

I agree. Why are we closing thousands of issues in the main repository for being proposals, but keeping obscure ones like godotengine/godot#21461? Edit: We should still keep godotengine/godot#21461 open since it seems to be a separate bug.

Was this page helpful?
0 / 5 - 0 ratings