View Full Version : (Lua) EventUnitDestroyed question


Optik
Apr 06, 2011, 08:07 AM
Well, I'm sure I totally wrong in my script, but I wonder if someone could at least try to understand what I'm trying to do with what I wrote. Free style :hmm:

function OnUnitDestroyed (Player, Civ, Unit, Plot)

if Players[Game.GetActivePlayer()]:GetName() == "Amanishakheto" then

if UnitDestroyed:GetCivilizationType() == GameInfoCivilizations["CIVILIZATION_BARBARIAN"] then

-- Add 1 Slave to player
AddNewUnit( player, "UNIT_SLAVE", plot:GetX(), plot:GetY() );
UserEventMessage( Locale.ConvertTextKey( "TXT_USER_EVENT_UNIT_CAPTURED_BY_NUBIA" ) );
end
UI.AddPopup(popupInfo);
end

Events.SerialEventUnitDestroyed.Add( OnUnitDestroyed );

SerialEventUnitDestroyed is in Event list in ModBuddy (http://wiki.2kgames.com/civ5/index.php/Lua_Game_Events), so could it be used to be attached to a civilization, and then with a specific unit?
:help:

Skajaquada
Apr 06, 2011, 12:03 PM
Where did you get those arguments from? Searching for "SerialEventUnitDestroyed" in the LUA-files gives this example in UnitFlagManager.lua:

-------------------------------------------------
-- On Unit Destroyed
-------------------------------------------------
function OnUnitDestroyed( playerID, unitID )
if( g_MasterList[ playerID ] == nil or
g_MasterList[ playerID ][ unitID ] == nil )
then
--print( string.format( "Unit not found for OnUnitDestroyed: Player[%i] Unit[%i]", playerID, unitID ) );
else
g_MasterList[ playerID ][ unitID ]:destroy();
end
end
Events.SerialEventUnitDestroyed.Add( OnUnitDestroyed );

You can then only use playerID and unitID in your own function. Then "Players[ playerID ]:GetUnitByID( unitID )" gets the unit and you can get it's name. I think a good help is to search for those events and functions listed in the Wiki in LUA-files in the Civilization 5-folder to see how they're used.