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)

You say "read the docs" (RTFM - lol) .... but you haven't answered my question.

I appreciate the answer and the effort and the fact that you are spending your precious time helping an idiot like me :)

You have split the code into two files. Mine is in one file.

The way you have set up your files and one class extending another makes sense for this silly example, but not for what I'm trying to do.

#################
#ChildClass.gd
extends MeshInstance

func _ready():
  draw()

Please note MeshInstance.

extends MeshInstance

func draw():
    print("Draw")
    # draw triangles here

class T:
    func go():
        draw() # render the contents of this class

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

The user can deifne a number of shapes (like in CAD) and these are arranged and rendered.

I would like to have some common render functions in the script that can be called (e.g. draw-quad, draw-outline, draw-x-ray ) so if the user clicks 'x-ray' the item is rendered semi-transparent with an outline.

Again - let me re-iterate how thankful I am that you are taking the time to answer me.

I don't get some part of the code cause of the indentation.
( is the second _ready belong to the class T???? I don't think so because you're declaringT.new() in . So why there is two _ready() function, if you are in the same file as you said. And why do you need an inner class?

Why do I need it? Who cares?!

This is a question about scope and access.

I fully understand how to create a hierarchy of classes, coroutines, mix-ins, multiple inheritance (in c++, python etc)

I'm wondering how to do the above.

Reason: Playing with Godot. Pushing and prodding.

Answers:

T.new() calls _init() which I have not bothered to define so I assume the Godot just builts a class by defaut (my code works)

Have you tried to run my code? Create a MeshInstance, then paste my code in.

Your code works? You don't get an error for :

class T:
    func go():
        draw() # render the contents of this class

because the draw function insn't declared in the current class

Well, obviously - that's my question. How do I call draw() ?

EDIT

Here you go, an example in Python that just works.

https://repl.it/repls/BriskBadBrain#main.py

Well, class T inherit from nothing, so I don't see why do you expect it to have a function draw.

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 Frequently asked questions and 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 [email protected] with your username.