@JPetroski As Prof. Garfield said, the key is that each trigger -- meaning
civ.scen.on___() -- should only appear once. From an organizational perspective, if you want to use a modular approach, I'd offer two suggestions or recommendations:
1. Keep all of those triggers within the top-level
events.lua, never within other modules. The modules should have functions
called from within triggers, not the trigger definitions directly.
2. As much as possible, try to keep
events.lua "lean" by not filling it up with a ton of direct event code. Put the event code in functions within separate modules, so that
events.lua only has a single line calling that function.
Events.lua then becomes more of a "broker", coordinating references to the separate modules and managing the triggers and timing. (Note that I said "as much as possible" -- this is a goal, but it doesn't need to be a hard-and-fast rule.)
If I wanted to set up a single-player scenario that would mimic the previous structure of having separate events files for each playable tribe, while integrating them as seamlessly as possible, I think I'd try something like this:
Events.lua:
Code:
baseEvents = require("baseEvents")
if object.tUSA.isHuman then
tribeEvents = require("usaEvents")
elseif object.tUSSR.isHuman then
tribeEvents = require("ussrEvents")
-- continue in a similar manner for all other playable tribes
end
civ.scen.onActivateUnit(function(unit, source)
baseEvents.activateUnitTrigger(unit, source)
tribeEvents.activateUnitTrigger(unit, source)
end)
civ.scen.onCityProduction(function(city, prod)
baseEvents.cityProductionTrigger(city, prod)
tribeEvents.cityProductionTrigger(city, prod)
end)
-- continue in a similar manner for every civ.scen.on___() trigger that your scenario requires
As you can see, after it's initial definition, the
tribeEvents variable can be referenced anywhere in the file
without any checks for which tribe the human is actually playing. The key, though, is that every one of your tribe-specific events files
must contain the same set of trigger-related functions. If you don't actually have any code to put there for a given playable tribe, just add an empty function declaration anyway with no code inside it.
Then
baseEvents.lua would contain the code you always want to run, regardless of which tribe the human is playing. It would have definitions for
activateUnitTrigger() and
cityProductionTrigger() functions, along with ones like
turnTrigger() or
unitKilledTrigger() if you need them. This gives you one place to make updates when an event isn't affected by which tribe the human is playing.
The tribe-specific files like
usaEvents.lua and
ussrEvents.lua would look very similar
structurally, with the same trigger functions, but of course with different contents. Remember that
usaEvents.lua doesn't [only] contain events
affecting the USA, it contains all events
that should run only when the human is playing the USA. Also, you can add as many
other functions in those files as you want/need, which are
called from that set of trigger functions. Those don't have to match across all the tribe-specific events files, since they wouldn't be called directly from
events.lua.
Disclaimer: I haven't tested out running code exactly like I'm proposing it here, but I'm pretty confident the approach is sound. Let me know if you have further questions, or if you try this and run into error messages!