Skip to content

Calling Functions from Dialogue

Dia:
    "You can call functions from written Dialogues.
    Provided that there's the Object
    to call said functions: the scope."
:
    "Let's use a simple ColorRect node as a scope."
:
    "And we'll modify its properties
    using its various setter functions."
  1. Add ColorRect node to the scene. Adjust its position and size to your liking.

    MyScene
      ├─ TheatreStage
      ├─ PanelContainer
      │     └─ VBoxContainer
      │         ├─ Label
      │         └─ DialogueLabel
      └─ ColorRect
    

    And reference it in the script.

    @onready var color_rect = $ColorRect
    
  2. Register it as a scope in the TheatreStage, using add_scope().

    extends Control
    
    var dlg: Dialogue # Load/create Dialogue here
    
    @export var stage: TheatreStage
    @onready var color_rect = $ColorRect
    
    func _input(event):
        if event.is_action_pressed("ui_accept"):
            stage.progress()
    
    func _ready():
        stage.add_scope("CoolRect", color_rect)
        stage.start(dlg)
    
    add_scope() requires 2 arguments:

    • The ID/name of the scope object to be used in the written Dialogue.
    • The object itself.

    Here, we'll name our ColorRect as CoolRect.

  3. We are ready to call functions (and even do other stuff) on our ColorRect.

    :
        "Now, I will turn this rectangle blue..."
    :
        "Ta-da~!"
    
        CoolRect.set_color("#0000FF")
    

Code Summary

MyScene
  ├─ TheatreStage
  ├─ PanelContainer
  │     └─ VBoxContainer
  │         ├─ Label
  │         └─ DialogueLabel
  └─ ColorRect
extends Control

var dlg: Dialogue # Load/create Dialogue here

@export var stage: TheatreStage
@onready var color_rect = $ColorRect

func _input(event):
    if event.is_action_pressed("ui_accept"):
        stage.progress()

func _ready():
    stage.add_scope("CoolRect", color_rect)
    stage.start(dlg)