GameEvents.UnitSetXY (Civ5 API)

From Civilization Modding Wiki
Jump to navigationJump to search

This page is a part of the Lua and UI Reference (Civ5).


Function.png This function is a member of GameEvents.

This is a static method, invoke it with a dot.


Triggered whenever any unit moves by any means, for every tile they move into. This includes units that are being created. Note that this event fires before animations are completed.


Usage

void GameEvents.UnitSetXY(PlayerID player, UnitID unit, int x, int y)


Event Type

Unknown

Parameters

player: The ID of the player the unit belongs to.
unit: The ID of the unit.
x: The location of the plot the unit has moved into.
y: No description available.

To kill the unit (UnitID) on this event, use unit:Kill(true, -1) to initiate a delayed death. Otherwise the game will crash.--Killmeplease (talk) 04:16, 1 November 2015 (UTC)

Source code samples

Too many occurences. Only 50 out of 3 are listed.

TurnsRemaining.lua - DLC_02 DLC

DLC/DLC_02/Scenarios/NewWorldScenario/TurnsRemaining.lua
0130
GameEvents.UnitSetXY.Add(function(iPlayer, iUnitID, iX, iY)
0132
local plot = Map.GetPlot(iX, iY);
0133
local player = Players[iPlayer];
0134
local unit = player:GetUnitByID(iUnitID);
0135
if (unit == nil or unit:IsDelayedDeath()) then
0136
return false;
0137
end
0139
-- Caravel that has reached China?
0140
if (unit:GetUnitType() == GameInfoTypes["UNIT_CARAVEL"]) then
0142
-- Adjacent to plot owned by China?
0143
local bAdjacentChina = IsAdjacentToChina (iX, iY);
0145
if (bAdjacentChina and not player:IsMinorCiv() and not player:IsBarbarian()) then
0147
if(iPlayer == Game.GetActivePlayer()) then
0148
UI.UnlockAchievement("ACHIEVEMENT_SCENARIO_02_ROUTE_TO_ORIENT");
0149
end
0151
-- How many have already been? (KLUDGE - using an unused modern era CvGame data field as a counter)
0152
local iTotalCircumnavigators = Game:GetNukesExploded();
0154
if (iTotalCircumnavigators < 3) then
0156
unit:Kill(true, -1);
0158
local iGoldReceived;
0159
local iVPReceived;
0161
-- Grant gold and VP
0162
if (iTotalCircumnavigators == 0) then
0163
iGoldReceived = 200;
0164
iVPReceived = 200;
0165
elseif (iTotalCircumnavigators == 1) then
0166
iGoldReceived = 100;
0167
iVPReceived = 100;
0168
else
0169
iGoldReceived = 50;
0170
iVPReceived = 50;
0171
end
0173
player:ChangeGold(iGoldReceived);
0174
local iNumWondersToCredit = iVPReceived / GameDefines["SCORE_WONDER_MULTIPLIER"];
0175
player:ChangeNumWorldWonders( iNumWondersToCredit );
0177
-- Put up dialog box
0178
local popupInfo = {
0179
Data1 = 500,
0180
Type = ButtonPopupTypes.BUTTONPOPUP_TEXT,
0181
}
0183
-- Human player?
0184
if (iPlayer == 0) then
0185
if (iTotalCircumnavigators == 0) then
0186
popupInfo.Text = Locale.ConvertTextKey("TXT_KEY_NEWWORLD_SCENARIO_FIRST_CIRCUMNAVIGATION", unit:GetName(), iGoldReceived, iVPReceived);
0187
elseif (iTotalCircumnavigators == 1) then
0188
popupInfo.Text = Locale.ConvertTextKey("TXT_KEY_NEWWORLD_SCENARIO_SECOND_CIRCUMNAVIGATION", unit:GetName(), iGoldReceived, iVPReceived);
0189
else
0190
popupInfo.Text = Locale.ConvertTextKey("TXT_KEY_NEWWORLD_SCENARIO_THIRD_CIRCUMNAVIGATION", unit:GetName(), iGoldReceived, iVPReceived);
0191
end
0192
else
0193
local iRemaining = 2 - iTotalCircumnavigators;
0194
popupInfo.Text = Locale.ConvertTextKey("TXT_KEY_NEWWORLD_SCENARIO_OTHER_CIRCUMNAVIGATION", player:GetName(), iRemaining);
0195
end
0197
UI.AddPopup(popupInfo);
0199
Game:ChangeNukesExploded(1);
0200
end
0201
end
0202
end
0204
-- Treasure unit that has reached capital?
0205
if (unit:GetUnitType() == GameInfoTypes["UNIT_TREASURE"]) then
0207
-- At my capital?
0208
local capital = player:GetCapitalCity();
0209
if (capital ~= nil and capital:GetX() == iX and capital:GetY() == iY) then
0211
unit:Kill(true, -1);
0213
-- Grant gold and VP
0214
local iGoldReceived = 100;
0215
local iVPReceived = 50;
0216
player:ChangeGold(iGoldReceived);
0217
player:ChangeScoreFromFutureTech(iVPReceived);
0219
-- Put out notification
0220
local text;
0221
local heading;
0222
text = Locale.ConvertTextKey("TXT_KEY_NEWWORLD_SCENARIO_TREASURE_RETURNED", capital:GetName(), iGoldReceived, iVPReceived);
0223
heading = Locale.ConvertTextKey("TXT_KEY_NEWWORLD_SCENARIO_TREASURE_RETURNED_SHORT");
0224
player:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, text, heading);
0226
if(iPlayer == Game.GetActivePlayer()) then
0227
if(capital:GetX() > 50) then
0228
UI.UnlockAchievement("ACHIEVEMENT_SCENARIO_02_RETURN_TREASURE");
0229
end
0230
end
0231
end
0232
end
0234
return true;
0235
end);


TurnsRemaining.lua (G&K)

DLC/Expansion/Scenarios/FallOfRomeScenario/TurnsRemaining.lua
1030
GameEvents.UnitSetXY.Add(UpdatePromotion);


TurnsRemaining.lua (G&K)

DLC/Expansion/Scenarios/MedievalScenario/TurnsRemaining.lua
0594
GameEvents.UnitSetXY.Add(function(iPlayer, iUnitID, iX, iY)
0596
local plot = Map.GetPlot(iX, iY);
0597
local player = Players[iPlayer];
0598
local unit = player:GetUnitByID(iUnitID);
0599
if (unit == nil or unit:IsDelayedDeath()) then
0600
return false;
0601
end
0602
local iNumCaravels = GetPersistentProperty("NumCaravels");
0604
-- Caravel that has reached China?
0605
if (unit:GetUnitType() == GameInfoTypes["UNIT_CARAVEL"] and iNumCaravels < 5) then
0607
-- Adjacent to plot owned by China?
0608
local bAdjacentChina = IsAdjacentToChina (iX, iY);
0610
if (bAdjacentChina and not player:IsMinorCiv() and not player:IsBarbarian()) then
0612
-- How many have already been?
0613
iNumCaravels = iNumCaravels + 1;
0614
SetPersistentProperty("NumCaravels", iNumCaravels);
0616
unit:Kill(true, -1);
0618
local iVPReceived;
0620
-- Grant VP
0621
iVPReceived = 600 - (iNumCaravels * 100);
0623
player:ChangeScoreFromFutureTech(iVPReceived);
0625
-- Put up dialog box
0626
local popupInfo = {
0627
Data1 = 500,
0628
Type = ButtonPopupTypes.BUTTONPOPUP_TEXT,
0629
}
0631
-- Human player?
0632
if (iPlayer == 0) then
0633
popupInfo.Text = Locale.ConvertTextKey("TXT_KEY_MEDIEVAL_SCENARIO_CARAVEL", unit:GetName(), iVPReceived, (5 - iNumCaravels));
0634
UI.AddPopup(popupInfo);
0635
else
0636
popupInfo.Text = Locale.ConvertTextKey("TXT_KEY_MEDIEVAL_SCENARIO_OTHER_CARAVEL", player:GetName(), iVPReceived, (5 - iNumCaravels));
0637
UI.AddPopup(popupInfo);
0638
end
0639
end
0640
end
0642
-- Conquistador that has reached China?
0643
if (unit:GetUnitType() == GameInfoTypes["UNIT_SPANISH_CONQUISTADOR"]) then
0645
-- Adjacent to plot owned by China?
0646
local bAdjacentChina = IsAdjacentToChina (iX, iY);
0648
-- How many have already been?
0649
local iNumConquistadors = GetPersistentProperty("NumConquistadors");
0651
if (bAdjacentChina and not player:IsMinorCiv() and not player:IsBarbarian() and iNumConquistadors < 3) then
0653
local pTeam;
0654
pTeam = Teams[player:GetTeam()];
0655
if (pTeam:IsHasTech(GameInfoTypes["TECH_EXPLORATION"])) then
0657
iNumConquistadors = iNumConquistadors + 1;
0658
SetPersistentProperty("NumConquistadors", iNumConquistadors);
0660
unit:Kill(true, -1);
0662
local iGoldReceived;
0663
local iVPReceived;
0665
-- Grant gold and VP
0666
iGoldReceived = 500;
0667
iVPReceived = 250;
0669
player:ChangeGold(iGoldReceived);
0670
player:ChangeScoreFromFutureTech(iVPReceived);
0672
-- Put up dialog box
0673
local popupInfo = {
0674
Data1 = 500,
0675
Type = ButtonPopupTypes.BUTTONPOPUP_TEXT,
0676
}
0678
-- Human player?
0679
if (iPlayer == 0) then
0680
popupInfo.Text = Locale.ConvertTextKey("TXT_KEY_MEDIEVAL_SCENARIO_CONQUISTADOR", unit:GetName(), iGoldReceived, iVPReceived);
0681
UI.AddPopup(popupInfo);
0682
end
0683
end
0684
end
0685
end
0687
return true;
0688
end);



The initial version of this page was created by the Civ5 API Bot, see the Civ5 API Reference FAQ. Some of the texts come from the 2kgames' wiki and most of code samples are copyrighted to Firaxis.
Functions' signatures were either copied from the 2kgames' wiki, or infered from the Lua source files and the binaries. Errors are possible.
Contributors may find help in the Contributors guide to the Civ5 API.