Loading games breaks Lua mods

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
Mods with global lua variables break when a save is loaded from within an existing game. For example, consider this code:

PHP:
print("Check MapModData.VEM")
if not MapModData.VEM then
    print("Loading MapModData.VEM")
    MapModData.VEM = {}
end
(This code is in a file imported to the VFS, and included in other core UI files. I'm uncertain if that is an important factor.)

When a game starts this prints in the tuner:

Code:
CiVUP - Events: Check MapModData.VEM
CiVUP - Events: Loading MapModData.VEM
Now perform these steps:

  1. Save the game.
  2. Exit to the main menu.
  3. Load the save.
This prints in the tuner:

Code:
CiVUP - Events: Check MapModData.VEM
CiVUP - Events: Loading MapModData.VEM
Everything still works. Now without exiting to the main menu, load the save while still in a game. Only this prints to the tuner:
Code:
CiVUP - Events: Check MapModData.VEM
MapModData.VEM is already set in this new game, even though it was actually set in the previous game. This is inconsistent with loading games from the main menu. This problem has existed since at least March, but it took me half a year to identify precisely what was going wrong.
 
Was the bug introduced in a patch, or did you mod first start using global state in March?

Is there a LoadGame Lua event? If so, one support module could allow other modules to register "reset global state" callbacks that are called every time a game is loaded.
 
I first started using globals in March. I suspect this bug has always been in the game. There's a "PlayerChoseToLoadGame" event in the api, but after looking at where that's utilized in the game files I don't think it would be useful. (The API is under the help menu in modbuddy.)
 
Very strange. PlayerChoseToLoadGame is an event to which you can attach a callback, but the event itself is triggered by LoadMenu.lua to do the actual loading of the game. Does this mean your callback gets triggered before the game is loaded or after? I've never seen a system that allowed calling an event in this way. I would expect LoadGame(file) to load the game and then trigger a GameLoaded event. PlayerChoseToLoadGame makes it sounds like it gets triggered before the game is loaded.

Maybe that's fine for our needs, though. Once the user has chosen to load a game, the game will be loaded, right? Is there anything that could abort the load and leave you back in the current game? If not, this should be fine.

The only thing left is finding an event that gets triggered when *starting* a game, such as SerialEventStartGame. I assume that the "serial" part here has nothing to do with the serial I/O ports on older computers. Do you know what it means in this context? I see a lot of serial events in that list.
 
A significant majority are called "SerialEvent" but I have no clue what that means either. The only event I've found which can indicate when the game is loaded is LoadScreenClose. This happens when the player clicks/hits enter on the splash screen of info about the selected civilization. However, this would not work for most of the initialization I need to do, since other core game files use the initialized variables, and are loaded earlier.
 
Back
Top Bottom