- What might cause a random crash?
The most common cause of this is if you clicked "Reload Landmark System" in your mod's properties. If you did, then go turn that off; it does nothing except cause lots of crashes. They're not really "random" crashes; the game will crash whenever it tries to load the graphics for a large number of hexes at once. This can happen if you tell a naval unit to move a long distance along the shore of an enemy's empire, but it'll also happen during AI turns depending on what orders they give their units.
If that's not it, then there are a lot of Lua errors that can cause a freeze/crash effect instead of gracefully failing. You pretty much have to debug by bisection (or brute-force print debugging); put a print statement at the start and end of each Lua function you modify, and once you know which function it's crashing inside, you can put print statements every few lines and see which is the last one printed before the crash. Unfortunately, this game does have some rudimentary multithreading capability, so it'll sometimes be running two processes in parallel. But this method will give you a good idea of where the offending statement is, which allows you to focus a bit more.
The most common Lua crash-causing error, in my experience, is unsanitized inputs. If you try to use an illegal value (like a null) in an sort of routine that expected valid inputs, then it'll often crash.
That is, if you have a loop like
Code:
for index,pPlayer in pairs(Players) do
(stuff)
end
then you really need to put an if check inside it to make sure the entity is valid, like
Code:
for index,pPlayer in pairs(Players) do
if( pPlayer ~= nil and pPlayer:IsAlive() and not (pPlayer:IsMinorCiv() or pPlayer:IsBarbarian() ) ) then
(stuff)
end
end
Likewise, if you've created a function like
Code:
function MyStuff(iPlayer, iCity, iUnit)
(stuff)
end
then you need to make it look like
Code:
function MyStuff(iPlayer, iCity, iUnit)
if( iPlayer ~= nil and iCity ~= nil and iUnit ~= nil ) then
(stuff)
end
end
to ensure that it never tries to do anything with illegal values.
- Why can't I load any saved games?
If it refuses to load at all, then it usually means that there was a Lua error while it was trying to re-initialize the interface. Yes, there are quite a few of these that can happen when you load a saved game but that won't crash a game that starts from scratch; it's a slightly different set of Lua functions. Same basic debugging method as described above.