Detect if Code was loaded before

LastSword

Prince
Joined
Feb 24, 2013
Messages
1,129
Imagine I have lua code used in mod A and mod B. Both mods rely on this code, however it is pointless (multipling same operations) for mod B to have this code if mod A is already having it.

I am looking for the way to solve this.
Obviously, it must work for every game initiation (new one/load). Included the fact that player may load game multiple times in same turn (making game turn not reliable).

What can be helpful: something unique (ID/string) generated upon each game initiation.
 
Mod A has its unique code with common functions in a separate Lua file - this common file will be VFS=true and "include"d into mod A's code

Mod B has its own unique code and uses (some of) the same common functions. Mod B will also have the common functions lua file (with VFS=true) and "include" it into it's own code.
 
Ok, I am afraid I wasn't clear.
I have made a lua code ( C ) that is sharing GetScenarioDiploModifier (~name) between multiple mods.
Mod A includes code ( C ) => resulting in adding function triggering upon GameEvent.Get(...) to mod A.
Mod B includes code ( C ) => resulting in adding function triggering upon GameEvent.Get(...) to mod B.

However, those functions are identical and will return same value (making same operation twice).
What I want is for code ( C ) is a way to prevent adding GameEvent in mod B if it is already done in mod A:
Code:
if SOMETHING then
GameEvents.GetScenarioDiploModifier2.Add(LSGetSDM2)
end

Well, the problem is that this Event firing a lot.

I figured that I can use "MapModData.", but I need something unique to game load (game turn will not work).
 
You'll need to use a broadcast/listen approach

Mod A starts up first
CodeA uses a LuaEvent to broadcast for other listeners
Nothing comes back, so it installs a listener on the LuaEvent AND hooks the game event

Mod B starts up next
CodeB use the same LuaEvent to broadcast for other listeners
The listener in ModA will respond, so ModB does nothing

I use a similar approach in my Summary Bar code - you can even pass a version number and use it make sure the latest version is installed
 
A bare bones example of what whoward is talking about is below. A version number approach is the obvious next step you would want to incorporate.

Code:
--------------
-- Initialization check.  Ensures this code isn't loaded twice
--------------
local retVal = {};
LuaEvents.SerialEventUnitCreatedGood_IsInitialized(retVal);

-- If retVal isn't changed, no other mod has initialized this code.
if (retVal.isInitialized == nil) then
	LuaEvents.SerialEventUnitCreatedGood_IsInitialized.Add(function (retVal) retVal.isInitialized = true; end);
	-- Initialize the code
	Events.SerialEventUnitCreated.Add(CallSerialEventUnitCreatedGood);
end
 
Back
Top Bottom