0
\$\begingroup\$

I have a class inside a class. I would like the inner class to be able to access functions from the outer class. Something like what's below:

@onready var my_label = $MarginContainer/Panel/Label

func display_text(text: String):
    my_label.set_text(text)
    
class DialogueSession
    var num = 1
    func show_next():
         display_text("the next number is:" + num++)

I get the error:

Error: Function display_text() not found in base self

How can I achieve this code structuring?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

One way is to pass self into the DialogueSession._init(), like so:

@onready var my_label = $MarginContainer/Panel/Label

func display_text(text: String):
    my_label.set_text(text)

func create_dialogue_session():
    return DialogueSession.new(self)

class DialogueSession:
    var container_class
    _init(c_c):
        container_class = c_c

    func show_next():
         container_class.display_text("the next number is:" + num++)

I do not believe this is likely to be the best way

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .