+1 vote

Suppose I have code like this:

class Class1:
    var pie

class Class2:
    func blah():
        return Class1.new()

This works fine and Class2 can refer to Class1. However, the following doesn't work:

class Class1:
    func blah():
        return Class2.new()

class Class2:
    func blah():
        return Class1.new()

because Class2 has not been declared when Class1 refers to it. What is a simple way to get them to be able to refer to each other?

in Engine by (239 points)
edited by

I don't think that plugin lends itself well to parameterized states, since it uses nodes for states, and parameterizing nodes requires the ugly hack of setting its parameters after creating them. It also makes it hard to have states that have another state nested inside of them (e.g. a menu state that holds a reference to the state the game was in before the menu was entered).

class Class1:
    func blah():
        return Class2.new()

class Class2:
    func blah():
        return Class1.new()

var Class2 = Class2
var Class1 = Class1

That doesn't work, for multiple reasons. The Class2 var comes after Class1's definition where it's needed, and inner classes can't access vars of the outer class (since vars can't be made static, vars need an instance to be part of).

I wasn't sure it would work either... that's why I commented -_-

How about this?

class Class1:
    var Class2
    func blah():
        return Class2.new()

class Class2:
    Class1
    func blah():
        return Class1.new()

func _ready():
    Class1.Class2 = Class2
    Class2.Class1 = Class1

That doesn't work either because Godot has no static variables.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.