Hi, I'm still getting used to how Godot works, so …

I guess the answer is "no", but can I somehow access the outer scope from an inner class in the same script? That is without passing it to the class as a parameter.

extends Node2D

var foo := 'bar'

func _ready() -> void:
	print_debug( foo )
	MyInnerClass.new()

class MyInnerClass extends Node2D:
	func _init() -> void:
		print_debug( foo ) # <-- This will break. Can I somehow access the outer scope?

This isn't a Godot question really more of an object oriented question. The answer is "no". However if you were using nodes with scripts instead of just scripts you could access the parent node

Probably the correct way is to create a member function in MyInnerClass and call that from ready.

Thanks for the replies. I didn't think it was possible. I'm just trying to avoid having to pass all (potentially a lot) relevant variables around. But of course it makes sense that the class shouldn't need to be aware of any external scope. I just thought maybe there's an option if it's in the same module.

To explain the background: The only reason why I am thinking of using an inner class here is to be able to use the convenience of the draw_ functions which I can only use inside _draw. I want to be able to transform some child nodes, but (I think) I can't create child nodes and overload their _draw function from within their parent node like this:

extends Node2D

func _ready() -> void:
	var shape := Node2D.new()
	self.add_child( shape )
	shape._draw = funcref( self, 'draw_shape' ) # <--- I can't seem to set _.draw like this

So I couldn't find a way to overload shape._draw unless I create a separate class that extends Node2D and overload _draw instead of the above, something like

extends Node2D

func _ready() -> void:
	var shape := Shape.new( self )

class Shape extends Node2D:
	func _init( parent ) -> void:
		parent.add_child( self )

	func _draw() -> void:
		draw_polyline( etc )

Another alternative is to create a var shape := Polygon2D.new() in _ready and draw with that, it's just less convenient because I don't have all the draw_ functions available.

But I'll probably just pass all those parameters to the inner class. It's the most obvious way, I was just lazy or trying to find a "cleaner looking" way without having to pass variables around within the same module.

Or, assuming I'll need these draw tools more often in this way, I could create a couple of global classes for each draw_ function.

class_name Circle

extends Node2D

var radius: float
var color: Color

func _init( _position, _radius, _color ) -> void:
	position = _position
	radius = _radius
	color = _color

func _draw() -> void:
	draw_circle( Vector2(), radius, color )

Sorry, this is becoming a rubber duck monolog … but at least I learned a lot from it.

To be used with caution, but if I go back to my first post here, I could just pass the parent to the child:

extends Node2D

var foo := 'bar'

func _ready() -> void:
	print_debug( foo )
	MyInnerClass.new( self )

class MyInnerClass extends Node2D:
	func _init( parent: Node2D ) -> void:
		print_debug( parent.foo )
2 years later