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.