0 votes
func draw():
    print("Draw")

class T:
    func go():
        draw()

func _ready():
    var t = T.new()
    t.go()

How do I do this?
It's not in the parent... in Python draw() would be in global scope so this would work.

in Engine by (12 points)

1 Answer

0 votes

Please read
https://docs.godotengine.org/fr/stable/getting_started/scripting/gdscript/gdscript_basics.html#classes

############
#MyClass.gd
extends Node 

class_name MyClass


func draw()
   print("draw")





#################
#ChildClass.gd
extends MyClass  # or extends "res://path/to/MyClass.gd"

func _ready():
  draw()
by (288 points)

https://www.datacamp.com/community/tutorials/inner-classes-python

How do I access things from my inner class?

I don't think it's working the same as your python example

This is not working like that in gdscript

The inner class do not inherit from the main, so you have to declare variable and function, the same way you're doing for any classes.

How would I pass in the 'outer' class in? It has no definition or name?

EDIT

FFS - OMG. It's just like Python, there's a self keyword:

extends MeshInstance

func draw():
print("Draw")

class T:
    func go(parent):
        parent.draw()

func _ready():
    var t = T.new()
    t.go(self)

This works. Thanks for the help and the hints. I gues because class T method go doesn't have a self parameter (as Python would have) I assumed self wasn't a keyword.

Thanks Unrealcloud 1 == you're a flipping legend! I owe you a pint.

EDIT 2

I think parent is a dirty variable name, maybe owner would be better? Hm... Godot does present you with some weird decision - lol. Maybe this should just be illegal code?

I think for the moment this is not a possible.
There is an github issue : https://github.com/godotengine/godot/issues/4472

Uhm ok, but this is working but nothing to do with inner class and outter class .
And actually class T method have a self parameter .

Anyway I'm happy you find a way

EDIT : Well parent is a bad name, because usually parent refer to parent Node - Child Node. You can acces to a parent in the node tree by get_parent(), but whatever.

But what really distrub me is that your inner class for the moment ( godot 3.2.2), can't acces function out of scope soooo, class T it's just an object, you could literaly create a new class in another file, if you give a function like func go(parent): parent.draw(), well you just applied command pattern design, it could be

func go(receiver):
        receiver.draw()

same stuff:

So why use inner class ^^ . Anyway not my jam, but if it works for you , fine for me

Edit 2 : As described here : https://stackoverflow.com/questions/2024566/how-to-access-outer-class-from-an-inner-class

So your first statement It's not in the parent... in Python draw() would be in global scope so this would work. is wrong. I presume in python the key word self is passing . But as you can try in python .

def draw():
  print("Hello UnRealCloud1")

class T:
  def go(self):
    draw()

t = T()
t.go()

t.draw()

will return an error .

But thx to point it out, I have a better view of how inner class work in general

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.