6
\$\begingroup\$

I want a single line of code which shows a popup dialog with the words "Hello World!" and an "Ok" button. (I want something simple for now, I know how to make something more complicated but that would be stupid at this point in development).

\$\endgroup\$
2
  • \$\begingroup\$ Hi Strategy Thinker, have you searched for any Godot tutorials? Last time I searched online, I found more than enough sites with helpful content. \$\endgroup\$ Commented Dec 13, 2019 at 11:54
  • 2
    \$\begingroup\$ @TomTsagk Before posting this question I searched for "godot message box", "godot dialogue tutorial", "godot message to user", "godot popup message to user", "godot pupup dialog example", "godot display message on screen", "godot messagebox show", "godot message box show" \$\endgroup\$ Commented Dec 14, 2019 at 23:05

1 Answer 1

11
\$\begingroup\$

You can use the OS / platform's alert system:

OS.alert('This is your message', 'Message Title')

You can use WindowDialog or subclass on any node like this:

func alert(text: String, title: String='Message') -> void:
    var dialog = AcceptDialog.new()
    dialog.dialog_text = text
    dialog.window_title = title
    dialog.connect('modal_closed', dialog, 'queue_free')
    add_child(dialog)
    dialog.popup_centered()

You can do the above, but globally:

func alert(text: String, title: String='Message') -> void:
    # ... code from above, but instead of `add_child(dialog)` do
    var scene_tree = Engine.get_main_loop()
    scene_tree.current_scene.add_child(dialog)
    dialog.popup_centered()
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Excellent answer, it had never occurred to me to connect a runtime spawned object directly to the queue_free method before, so simple! \$\endgroup\$
    – Giles
    Commented Jan 2, 2022 at 14:09

You must log in to answer this question.

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