[SOLVED] Wesnoth hanging on map generation

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
Sventimir
Posts: 15
Joined: November 11th, 2020, 9:01 am

[SOLVED] Wesnoth hanging on map generation

Post by Sventimir »

Hello,
I'm developing a random map generator for my campaign. It's currently over 3000 lines of Lua code and probably not a very well-optimized at that. As I try to add a new feature to it. I'm going into the situation, where Wesnoth hangs on map generation. The window stops refreshing. When I call gui.show_lua_console() in order to debug and peek some values, I can sometimes see a broken text as visible at the bottom of the screenshot.

Image

Typically, as I revert the code back to the state from before adding the feature, the problem goes away.

I suspect it might be consuming too much memory. During generation I'm keeping a model of the entire map as an array of objects and as more and more features go into these objects, I guess it might at some point hit some limit. It's surely not an amount that my computer couldn't handle (overall mem consumption never goes beyond 30%), but I guess Wesnoth might have an internal limit of memory it assigns to Lua. Is that correct?

Another guess I have is that there might be a reference cycle between objects somewhere, which makes Lua go into an infinite loop. I don't think this should be the case, but I cannot firmly deny it.

Is there indeed a limited memory allocated to Lua in Wesnoth? If so, what's the limit? If not, what else could be responsible for this behaviour?
Last edited by Sventimir on December 17th, 2025, 9:57 am, edited 1 time in total.
gnombat
Posts: 968
Joined: June 10th, 2010, 8:49 pm

Re: Wesnoth hanging on map generation

Post by gnombat »

Sventimir wrote: December 16th, 2025, 12:57 pm The window stops refreshing. When I call gui.show_lua_console() in order to debug and peek some values, I can sometimes see a broken text as visible at the bottom of the screenshot.
It would probably be better to use std_print(message) or wesnoth.log('error', message, false) here - that will log to a file rather than the Lua console.
Sventimir wrote: December 16th, 2025, 12:57 pm Another guess I have is that there might be a reference cycle between objects somewhere, which makes Lua go into an infinite loop.
Yes, I would guess that this is probably an infinite loop (or else a loop that is taking a very long time to run - maybe if you wait an hour it will finish).
mentos987
Posts: 29
Joined: September 17th, 2021, 10:47 pm

Re: Wesnoth hanging on map generation

Post by mentos987 »

If you are on discord you can ask Emperor/ZombieKnight for his map gen code. It is well optimised and fairly far along in development.
Sventimir
Posts: 15
Joined: November 11th, 2020, 9:01 am

Re: Wesnoth hanging on map generation

Post by Sventimir »

gnombat wrote: December 16th, 2025, 1:49 pm It would probably be better to use std_print(message) or wesnoth.log('error', message, false) here - that will log to a file rather than the Lua console.
Thanks for the pointer, that's useful.

I didn't realise there was a Discord server for Wesnoth. Thanks!
Sventimir
Posts: 15
Joined: November 11th, 2020, 9:01 am

Re: Wesnoth hanging on map generation

Post by Sventimir »

I think I have found the problem. I made a mistake when setting metatables for my internal objects, apparently creating a reference cycle between my prototypes. That was most likely the source of the issue. It explains why Wesnoth choked when trying to display these objects or call their methods. Also explains infinite loops.
Post Reply