Call method in script - not parent

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By monk-e-boy
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.

:bust_in_silhouette: Reply From: UnRealCloud 1

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()

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 :slight_smile:

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.

monk-e-boy | 2020-07-28 22:33

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?

UnRealCloud 1 | 2020-07-28 22:45

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.

monk-e-boy | 2020-07-28 23:01

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

UnRealCloud 1 | 2020-07-28 23:05

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

EDIT

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

Sign up to continue coding - Replit

monk-e-boy | 2020-07-28 23:07

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

UnRealCloud 1 | 2020-07-28 23:12

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

How do I access things from my inner class?

monk-e-boy | 2020-07-28 23:14

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.

UnRealCloud 1 | 2020-07-28 23:17

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?

monk-e-boy | 2020-07-28 23:20

I think for the moment this is not a possible.
There is an github issue : A nested class cannot access function outside its scope · Issue #4472 · godotengine/godot · GitHub

UnRealCloud 1 | 2020-07-28 23:26

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 : python - How to access outer class from an inner class? - Stack Overflow

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

UnRealCloud 1 | 2020-07-28 23:30