[SOLVED] Wesnoth hanging on map generation
Moderator: Forum Moderators
[SOLVED] Wesnoth hanging on map generation
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.

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?
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.
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.
Re: Wesnoth hanging on map generation
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.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).
Re: Wesnoth hanging on map generation
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.
Re: Wesnoth hanging on map generation
Thanks for the pointer, that's useful.
I didn't realise there was a Discord server for Wesnoth. Thanks!
Re: Wesnoth hanging on map generation
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.