Topic: Newbie struggling with message box syntax

SandJ
Avatar
Topic Opener
Joined: 2015-05-08, 22:46 UTC+2.0
Posts: 29
OS: Linux
Ranking
Pry about Widelands
Location: UK
Posted at: Yesterday 20:45 UTC+2.0

I am trying to get the hang of scripting and want to use one of my favourite debugging techniques: a pop-up box at start and end of each function so I can trace what is happening. But I can't work out how to use send_to_inbox or message_box or campaign_message_box. I either get an error about trying to index a string or index a number null values or an outright crash saying "FATAL ERROR: Received signal 11 (Segmentation fault)"

I have tried using:

include "scripting/ui.lua"
include "scripting/messages.lua"

and various forms of:

send_to_inbox(plr, _("xyz"), _("ABC"), {popup=false}),
message_box(_("xyz"), _("ABC")),
campaign_message_box(_("xyz"), _("ABC")),
campaign_message_box({title = "xyz",
    body = p(_("ABC")),    and    body = "ABC",
    position = "top"}
 ),

and also wl.game.Player.message_box() and player:message_box() without success.

Q1. Is there a better way to trace program execution than pop-ups for the start and end of programs and functions?

Q2. Can someone point me at an example of working code for inclusion in an init.lua that provides that sort of functionality?

Yes, I have tried looking at the wiki and at existing scripts, but cannot work out what I am getting wrong, despite spending about four hours on just this one thing.

Edited: Yesterday 20:49 UTC+2.0

Top Quote
SandJ
Avatar
Topic Opener
Joined: 2015-05-08, 22:46 UTC+2.0
Posts: 29
OS: Linux
Ranking
Pry about Widelands
Location: UK
Posted at: Yesterday 22:14 UTC+2.0
    send_to_all_inboxes("text"),

gives me "attempt to index a nil value (global 'game')"

    send_to_all_inboxes(_("text")),

gives me "attempt to index a nil value (global 'game')"

    message_box(_("xyz"), _("ABC")),

crashes with "FATAL ERROR: Received signal 11 (Segmentation fault)"

    message_box(_("xyz"), p(_("ABC"))),

crashes with "FATAL ERROR: Received signal 11 (Segmentation fault)"

    message_box("xyz","ABC"),

crashes with "FATAL ERROR: Received signal 11 (Segmentation fault)"

    send_to_inbox(1, _("xyz"), _("ABC"), {popup=false}),

gives me "attempt to index a number value (local 'player'"

    campaign_message_box(_("xyz"), _("ABC")),

gives me "attempt to index a string value (local 'message')"

and so on.

Edited: Yesterday 22:14 UTC+2.0

Top Quote
SandJ
Avatar
Topic Opener
Joined: 2015-05-08, 22:46 UTC+2.0
Posts: 29
OS: Linux
Ranking
Pry about Widelands
Location: UK
Posted at: Yesterday 23:30 UTC+2.0

I have tried:

campaign_message_box({title = "This is the title", body = p("Hi, this is my first scenario")})

from the Scenario Tutorial, but that causes a "FATAL ERROR: Received signal 11 (Segmentation fault)" crash.


Top Quote
Nordfriese
Avatar
Joined: 2017-01-17, 18:07 UTC+1.0
Posts: 2173
OS: Debian Testing
Version: Latest master
Ranking
One Elder of Players
Location: 0x55555d3a34c0
Posted at: Today 10:43 UTC+2.0

Some pointers to the documentation

Importantly, are you running this in a game, in the editor, or in the main menu? As an interactive player or as a spectator? Several of these functions are intended for interactive players in-game only.

"attempt to index a nil value (global 'game')"

This seems to be a bug in send_to_all_inboxes which assumes that you created a global variable game like this:

game = wl.Game()

This should be fixed in the function to use wl.Game() instead


Top Quote
kaputtnik
Avatar
Joined: 2013-02-18, 20:48 UTC+1.0
Posts: 2691
OS: Archlinux
Version: current master
Ranking
One Elder of Players
Location: Germany
Posted at: Today 10:50 UTC+2.0

Working with lua can be hard …

If this is just for you you can leave out the localization function (_()) which makes the code more readable. Some functions need the text to be richtext formatted which requires including richtext.lua

Personally i use just print() statements which prints the output to the console, or i use a debug version of widelands which provides a "debug console" directly within widelands but unfortunately print statements ar not printed to this console. Anyway using this console testing scripts can be easier by loading and executing a script-file directly by writing dofile("full/path/to/script.lua"). If something doesn't work as expected you can change the script and execute it with dofile() again without restarting widelands. Also testing small pieces of scripts can be tested directly this way. So i did for the examples below.

This works for me:

include "scripting/messages.lua"
plr = wl.Game().players[1]
send_to_inbox(plr, "title", "message", {popup=true})

Shows immediately a messagebox. With popup=false the messagebox is not shown.

Easier might be to to use show_messagebox():

wl.ui.show_messagebox("title", "message")

If you are struggling further you need to provide the full command chain, e.g. with all includes and commands before the failing command.


Top Quote