Buildings in city go haywire on loading saved game

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
Come across a very weird bug in my latest play testing of my Eastern Front mod.

Every time I save a game and load it (irrespective of which turn), all the buildings go haywire in each city with most dropping off and the palace appearing in multiple cities.

Here is what I tried so far with no results:
1. Removed any UI mods...problem still occurs
2. Re-saved map into new file just in case of corruption...problem still occurs
3. Removed all buildings in cities except capital...problem still occurs
4. Removed all buildings in capital (continuing from step 3) and only left palace.....reload drops the sole palace building and now the list is empty. The star still appears next to the city indicated it is the capital!!!!

This can only be reproduced when saving and then reloading the saved game. If I start a game and play through each turn then all city buildings are fine. It is just when reloading from a saved game that the issue occurs.

Has anyone come across any type of UI bug using loaded games before and if so, can you remember what might have caused it??

or....is this some post-patch bug that I may have missed info on??

This one really has me stumped :sad:
 
The problem is more likely to be Lua running when the save is loaded that shouldn't be executing. Any Lua not inside methods will execute both when the game is initially started and when the game is loaded. You may need to add a check that you only execute "initialisation code" when the game is actually starting and not loading.
 
I think I am doing what you just mentioned. Here is a snippet of the init code sections of my main lua file. Note that this is based on Gedemon's RED lua structure.

Code:
-- Initialize when RedMain is loaded
if ( bWaitBeforeInitialize ) then
	bWaitBeforeInitialize = false	
	local savedData = Modding.OpenSaveData()
	local iValue = savedData.GetValue("EF_InitDone")
	if (iValue ~= 1) then
		-- begin function to call section		
		OnFirstTurn()
		Events.ActivePlayerTurnEnd.Add(OnFirstTurnEnd)
		-- end function to call section
		savedData.SetValue("EF_InitDone", 1)
	else
		OnLoading()
	end
end


Events.SequenceGameInitComplete.Add( OnGameInit )


-- functions to call after game initialization (DoM screen button "Begin your journey" appears)
function OnGameInit ()
	local savedData = Modding.OpenSaveData()
	local iValue = savedData.GetValue("EF_FinalInitDone")
	if (iValue ~= 1) then
		Dprint ("Game is initialized, calling OnGameInit() for new game ...")
		-- begin function to call section
		LoadAllTable() -- before any change on tables...
		SaveData("TownData", g_UnitSupplyData, TOWN_DATA_SAVE_SLOT)

		-- Trigger on Start of Scenario ------------------------------------------
		CreateTerritoryMap()	
		SetInitialCityBuilds() -- Places buildings in citites
		InitSupplySystem()
		CalculateSupplySystem()
		InitExistingUnitSupplyData()
		SetInvasionPromotions()
		RemoveNewPromotionFirstTurn()

		-- Custom Event Triggers ------------------------------------------
		Events.SerialEventCityCaptured.Add(CityCaptureGermanyTroopBonus)
		Events.SerialEventCityCaptured.Add(CityCaptureAxisCossacks)
		Events.SerialEventCityCaptured.Add(CityCaptureSpawnPartisans)

		-- SerialEvents  ------------------------------------------
		Events.SerialEventHexCultureChanged.Add(CheckCultureChange)
		Events.SerialEventUnitCreated.Add(InitNewUnitSupplyData)

		-- GameEvents  ------------------------------------------
		GameEvents.UnitSetXY.Add( UnitCaptureTile )
		GameEvents.PlayerDoTurn.Add(CheckNewUnits)
		GameEvents.PlayerDoTurn.Add(CalculateSuppliedUnits)
		GameEvents.PlayerDoTurn.Add(CheckForEvents)
		
------------------------------------------
		SaveAllTable() -- Allows data to be shown on turn one!	
		savedData.SetValue("EF_FinalInitDone", 1)
	else
		Dprint ("Game is initialized, calling OnGameInit() for loaded game ...")
	end
end


-- functions to call ASAP after loading a saved game
function OnLoading ()
	Dprint ("EF_Main.lua loaded, initializing for saved game ...")
	NewTurnSummary()
	LoadAllTable() -- before any change on tables...
	SaveData("TownData", g_UnitSupplyData, TOWN_DATA_SAVE_SLOT)

	-- Trigger on Load of Scenario ------------------------------------------
	PrintReport()
	SetInvasionPromotions()								
	RemoveNewPromotionFirstTurn()
		
	-- Custom Event Triggers ------------------------------------------
	Events.SerialEventCityCaptured.Add(CityCaptureGermanyTroopBonus)
	Events.SerialEventCityCaptured.Add(CityCaptureAxisCossacks)
	Events.SerialEventCityCaptured.Add(CityCaptureSpawnPartisans)
	
	-- SerialEvents  ------------------------------------------
	Events.SerialEventHexCultureChanged.Add(CheckCultureChange)
	Events.SerialEventUnitCreated.Add(InitNewUnitSupplyData)

	-- GameEvents  ------------------------------------------
	GameEvents.UnitSetXY.Add( UnitCaptureTile )
	GameEvents.PlayerDoTurn.Add(CheckNewUnits)
	GameEvents.PlayerDoTurn.Add(CalculateSuppliedUnits)
	GameEvents.PlayerDoTurn.Add(CheckForEvents)
	
end

UPDATE: I have made a backup of my mod and gone crazy hacking it apart to figure out this issue. Still no dice as to what is causing it. Buildings have been stripped, loading code has been commented out and any type of UI has been stripped.
 
Quick update on this matter incase it ever occurs to anyone else.

I have mostly sorted it out (small amount still incorrect), but it seems the main source of my problem was deleting most building tables (i.e. clean slate approach) and their supporting tables.

It seems that the load code must have a hard-coded approach to loading and placing buildings into cities which goes out of whack if a modder deletes entries. Also re-indexing the IDs via SQL (for those who know what that is) does not seem to solve the problem from what I can tell.

So for the time being, save yourself the headaches and just leave the vanilla buildings alone if redoing the buildings in your mods.
 
UPDATE: To put this issue to rest as the last post was 50% right and just incase someone else ever comes across this behaviour...

The issue was caused through the use of additional custom tables I had created to store additional info on buildings.

First the offending SQL:
Code:
CREATE TABLE EF_SupplyBuildingModifiers ('[COLOR="Red"][B]ID[/B][/COLOR]' integer primary key autoincrement, '[COLOR="Red"][B]Type[/B][/COLOR]' text not null unique, 'ProdModifier' integer default 0);

Unbeknown to me, I wrote the above SQL code to create a new table with a field named 'ID' and 'Type' within it. The 'Type' field will be used to contain the unique building identifiers such as "BUILDING_HARBOR".

Now the reason this is a no-no:
To quote whoward69 who spotted the problem with my files (bolding and underlining are mine):
ANY table with both an ID(int) and a Type(text) field is handled very specially by the game database core - it becomes part of the GameInfos array and the GameInfoTypes array

GameInfos.EF_SupplyBuildingModifiers[0].ID – This is the BUILDING_FARM ID value
GameInfos.EF_SupplyBuildingModifiers.BUILDING_FARM.ID – as is this

No problem so far BUT

You can also do GameInfoTypes.BUILDING_FARM to get same the ID value as above

Seen the problem? You’ve just overwritten things like GameInfoTypes.BUILDING_HARBOR with a different value

You need to change all your “Type” column names to be “BuildingType” (or whatever) – and then your tables won’t be treated in the special way

And there you have it kids, one rogue naming of a field can cause major havoc if you don't have a clue about these various array types!:faint:
 
And there you have it kids, one rogue naming of a field can cause major havoc if you don't have a clue about these various array types!:faint:

I'm in the process of writing a "tutorial" about this, as these types of tables frequently cause confusion.
 
My vote is to get this info on the new wiki if possible as well as on the forum.
 
Back
Top Bottom