+1 vote

I would like to create a inner class (potentialy a singleton) that works over a child of the current node. To do this in a clean way, I would like to initialize its members in a single statement. Even after reading a lot of forum and GDScript reference texts. I was unable to achieve my code goals. So, is it possible? If not, what would be the better approach?

Example

 class VelocityObserver:
    onready var velocity : Vector2 = (outer_scope).get_node("child_of_current_scope").speed
    func display_speed():
        print("Component X :+velocity.x)

Yet, my doubt creates one more question. Instances of VelocityObserver, in the above example, don't derive/inherit from outer class but prefix "." still seems to have a mean (compile and runs normally). In this case, is it refer to the global scope? To be more clear about what I want to mean, this next line

var velocity : Vector2 = .get_node( "child_of_current_scope" ).speed

makes sense to the compiler.

Thanks in advance

in Engine by (20 points)
edited by

Your example should not compile if you perform assignment with := operator (but it should with =) because there is not a valid value at the right side to inference its type. What version are you using?

Have you thought about setting the member variables of the inner class (really an object of the class) from the parent class? Either set them directly when the inner class object is created, or have a member function which sets the desired data in the inner class object.

@unlutYou are right! I've forgot to declare the type of variable as Vector2!!! But even declaring that, it doesn't work (I got a null value).

@Ertain, there is no parent class to this inner classI Look its definition. Or am I wrong? Anyway, thanks for your suggestion. It was really good! has showed me that I was "misusing" inner classes. Some properties are to be initialized by the constructor!

Thanks for all help! I am going to make changes in the code.

1 Answer

0 votes

I was going to write an answer about that part but I was confused because you said code was compiling okay. Here is the thing.
1- What you assume is true, inner class is not derived from outer class so you cannot access its stuff without having a reference to outer class object. Easiest way is to just pass a reference to outer class during constructor (init function).
2- "." means self. You can test with with a small script where you print ".getinstanceid()" and "self.getinstanceid()" in a class function.

func _ready():
    print(.get_instance_id())
    print(self.get_instance_id())

3- You are getting null when you use onready variable in your inner class, because ready function is called when a node enters the scene tree. When you instantiate an inner class with var velObs = VelocityObserver.new(), its _init method is called (init is called when an object is created), but its _ready function is not, so your onready variables are not initialized.

by (98 points)

VelocityObserver's _ready() won't ever be called because it doesn't exist :) The class isn't a Node.Same thing with get_node().

GDScript's interpreter should be complaining about onready making no sense for non-Node classes but it doesn't :( Removing that keyword will uncover an error with get_node().

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.