trying to make a lua mod that affects empire happiness based on cities and population
First of all, you can't change Happiness through Lua, at least not directly. It may LOOK like you can, but the game re-derives each player's happiness at the end of their turn. So if you use SetHappiness to add 10 happiness to your empire, it'll automatically be removed before having any effect since it won't be counted in the rederivation. (You can tell because it won't change the Golden Age timer or affect whether you receive a negative-happiness penalty.) Other Lua functions, like ChangeHappinessFromBuildings, don't work at all.
You CAN get around this somewhat by creating a temporary building, for instance, and placing it in the city. That WOULD add to the city's actual happiness, but it has a few other issues. First and foremost being,
you can't subtract happiness this way. If you give a negative Happiness value to a building, it does nothing. You can add a negative-happiness Policy, but this then screws up the culture costs of future policies, the branches needed for a cultural victory, and the player's score.
also, what does serial events mean in general?
"Serial" events are UI-oriented. The event will only trigger when the game NEEDS it to trigger for display purposes.
For instance, consider SerialEventImprovementCreated, which is what the game uses to draw an Improvement on the map. It has two states: 2, and 4. "2" means an incomplete improvement, the half-drawn version you see while a worker is building it. "4" means complete.
If you're building an improvement in your own territory then it'll work fine; it'll trigger the "2" event when you start building, and the "4" event when it finishes. (It'll also trigger when an Ancient Ruin or Barbarian Camp are seen, but those are easy to check for.) But if an AI on the other side of the world builds the improvement, the Serial Event will only trigger
when you, the active player, first SEE the tile in question. This is nearly always a state 4 version, for obvious reasons. So if you have logic that's supposed to trigger when a certain type of improvement is created, this event just won't work correctly for the AI.
But it's not consistent for all serial events. SerialEventUnitCreated, for instance, WILL trigger even if the unit is somewhere you can't see it. However, it'll ALSO trigger when:
> A savegame is first loaded
> The unit embarks
> The unit DISembarks
> An air unit rebases (I think)
As you can guess, this makes it a bit of a headache to use for unit-creation events. I got around that in my mod by creating a new "Rookie" promotion that a unit has until after its first fight, so that it's now easy to tell whether it's the first time the event has triggered for a given unit. So it's not insurmountable.
In general, ActivePlayerStartTurn and ActivePlayerEndTurn are what you'll use for the vast majority of your Lua coding. Just remember the "Active Player" part, meaning YOU, the guy sitting at the keyboard; it'll trigger at the start and end of your turn, and not do the same for the AI turns, so anything you put in there should loop over all players so that everyone gets the same opportunity to trigger your logic.
(Also, this'd hose multiplayer, but that one's a lost cause at this point regardless.)