Sid Meier's Civilization V

Sid Meier's Civilization V

Not enough ratings
Unit Created Event [snippet for mod makers]
   
Award
Favorite
Favorited
Unfavorite
File Size
Posted
Updated
2.355 KB
28 May, 2014 @ 6:59pm
30 Jul, 2014 @ 3:27pm
4 Change Notes ( view )

Subscribe to download
Unit Created Event [snippet for mod makers]

In 1 collection by Machiavelli
Machiavelli's snippets for mod makers
6 items
Description
For mod makers.

What it does
This mod contains code that defines a lua event that fires off when a unit is created. The event does not fire when a unit is upgraded. The lua event provides the information you need to get the owning player object and the unit object.

If you want to delete the unit don't use "unit:Kill()" as that could cause other code hooked into this event to crash. Instead use "unit:SetHasPromotion(GameInfoTypes["PROMOTION_MARKED_FOR_DEATH"], true)". Units with that promotion will automatically be killed after all functions hooked into the event are executed.

If an end-user is playing with multiple mods that use this snippet it will work properly.

Compatibility
This code is compatible with Vanilla, Gods & Kings and Brave New World.

This code is identical to what is used in "Race for Religion", "Reform and Rule" and other mods I've made.

How to incorporate into your mod
Copy the "unit created" directory and all its contents to your mod. Have the XML file update the database (via the Action tab) and set the LUA file as an "InGameUIAddin" (via the Content tab).

How to use
Add your functions to the "SerialEventUnitCreatedGood" event.

How it works
The code uses "SerialEventUnitCreated" (which fires too often) and does some filtering logic to determine if a unit was actually created. If so, the code fires off the lua event and passes all the information that "SerialEventUnitCreated" had.

A promotion that has no in-game effect (but carries over when a unit is upgraded) marks every unit that has been processed, ensuring that the code does not fire more than once for a single unit.

CivFanatics Download[forums.civfanatics.com]
5 Comments
Machiavelli  [author] 4 Apr, 2015 @ 9:27pm 
Sadly GetGameTurnCreated() is insufficient to ensure that the event only fires once per unit. A unit that was created and embarked in the same turn (ei purchased landshnek) would generate SerialEventUnitCreated twice. The serial event also fires if a human reloads the game. Also, upgrading a unit is implemented as deleting and spawning another unit, which would have a misleading creation turn.
qqqbbb 1 Apr, 2015 @ 7:37pm 
Instead of addong promotion to every unit you can use GetGameTurnCreated().
Machiavelli  [author] 29 May, 2014 @ 7:18am 
I am the same machiavelli on CivFanatics (technically I'm "Machiavelli24" on CivFanatics). I haven't posted this code on there yet. I want to get my religion snippets on the workshop first.
lshipp 29 May, 2014 @ 6:36am 
NIce. I've subscribed to both of your these. Wondered if you had posted them to Civfanatics. I'm assuming you're the same Machiavelli as over there.
DJSHenninger 29 May, 2014 @ 2:06am 
These mods sound really awesome for people like I who absolutely know nothing about lua! Thanks :D Hope you make more of these!