1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Unit Created Event

Discussion in 'CivBE - Code Snippets' started by Machiavelli24, May 9, 2015.

  1. Machiavelli24

    Machiavelli24 Mod creator

    May 9, 2012
    This code is compatible with vanilla Beyond Earth and Rising Tide.

    CivFanatics Download
    Workshop Download

    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_UCE_MARKED_FOR_DEATH"], true)". The UCE stands for Unit created event. 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.

    IMPORTANT: Because this snippet adds a promotion users of any mod that incorporates this snippet will need to "enable->back->re-enable" your mod for to avoid the flaw in Fraxis's design for UnitPanel. If they don't do this all their units will be killed when they start or load a game (along with general rending issues for the UnitPanel).

    How to incorporate into your mod
    Copy the "Unit Created Event" 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 "UnitCreatedEvent" 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.

Share This Page