How do you get a lua file to run before game setup?

TofuSojo

Chieftain
Joined
Sep 28, 2014
Messages
41
Location
Orlando, FL
I'm trying to get my mod to detect if my other mod is enabled and only then update the database with a specific xml file. Detecting my other mod is easy, and I read elsewhere how to get a lua script to update the database with an xml (assuming it works). The trouble I'm having is getting my lua script to run at the right time, since it has to happen after mods have been enabled but before the debug database has been created (after which it's too late to change it).

From Gedemon's great Custom Advanced Setup mod I got the idea to use Events.AfterModsActivate, since that sounded like the right event for when my lua should run and like his AdvancedSetup.lua file I've set mine to VFS=true and not added it to the Content tab as an InGameUIAddIn--which I understand only happens right before the player presses the Begin Your Journey Button, so it's too late--yet, my lua file never even runs. Here is the only function in the lua file:

Code:
function DetectTechtreeOverhaul()
	print("DetectTechtreeOverhaul running...")
	local TechtreeOverhaulModID = "265a41e4-afb6-45d3-b361-fa16d74a8a70"

	for _, mod in pairs(Modding.GetActivatedMods()) do
		if (mod.ID == TechtreeOverhaulModID) then
			print("Techtree Overhaul mod detected!")
			Modding.PerformActions("TechtreeOverhaulDetected")
		end
	end
end
Events.AfterModsActivate.Add(DetectTechtreeOverhaul)

DetectTechtreeOverhaul is the custom action I added to the Actions tab of my mod that updates the database with my xml file. Confirming that works right would be the next step :P.

Anyone know how to get this to work?
 
The reason Gedemon's method works (i.e., set to VFS=true but without an Entry Point) is that it's overriding the AdvancedSetup.lua that comes with the game. You could certainly override one of the base game Lua files that load before the database is finalized (e.g., MapGenerator.lua), but of course that results in possible compatibility issues.

There are other methods:
  • Associations (Dependencies/References/Blocks)
  • SQL Triggers
  • ...It requires some backwards-thinking mental acrobatics, but you could take advantage of the fact that a file will stop being processed when an error is encountered. You can purposefully generate an error by, e.g., adding something that already exists, and put your conditional database entries after the error would otherwise be generated
 
Back
Top Bottom