[LUA] Question about Events.SerialEventUnitCreated and UnitSetXY

Troller0001

I've anxiously awaited your arrival!
Joined
Mar 9, 2016
Messages
755
Location
The Netherlands
Im kinda new to Lua and had a few questions about this function that I found on the Modwiki:
void Events.SerialEventUnitCreated(PlayerID player, UnitID unitID, int hexVec, int unitType, int cultureType, int civID, int primaryColor, int secondaryColor, int unitFlagIndex, int fogState)

These are my questions:
  1. This function is executed every time a unit is created right? If yes, does this include city state gifting as well (so when a city state has gifted you a unit) Partial Answer: Yes. (Thanks to Klisz, bane_ and LeeS)
  2. What does hexVec mean? Is it the hex-ID that the unit was created on?
  3. What does fogState mean? Does it have to do with the fog of war?
  4. Does cultureType have anything to do with the art-style of the Civ? (E.g. Mediterranean)
  5. If #1 is true, do I use the function as this:
    Code:
    Events.SerialEventUnitCreated.Add(function(iPlayer, iUnitID, iHexVec, iUnitType, iCultureType, iCivID, iPrimaryColor, iSecondaryColor, iUnitFlagIndex, iFogState)
    --further code blah blah
    end)
--------------------
So here's my other question:
If I want to check for the movement of a specific unit how would I do this without causing much lag/crash potential (The code below probably illustrates a bad example which will cause lag, especially in the late-game)
Spoiler :
Code:
local iUnitToCheck = GameInfoTypes.UNIT_ARCHER --this could either be a unit type or a unit ID; It's here just to illustrate

GameEvents.UnitSetXY.Add(function(iPlayer, iUnit, iPlotX, iPlotY)
      if(iUnit==iUnitToCheck)then
              --further code blah blah
      end  
end)

Last question, does UnitSetXY execute if a unit is given a new move command, or when it has moved to a new hex? (So also if a unit is given a move command 2 tiles further away, does it execute for every tile it will move over?) Answer: Yes it does. It also fires when a unit is created or when it embarks. The unit walking animation will still fire first though (even if it moves more than 1 tile) (Thanks to Civitar, bane_ and Civitar)

Sorry if these questions seem very easy to answer, I'm just starting out with Lua.

Thanks in advance!

-Troller0001
 
UnitSetXY definitely fires for every hex a unit moves over, as I had a script that gave a 50% chance of killing units moving over a certain improvement. when i surrounded a unit with 2 tiles in all directions with that improvement and it was killed by one, the lua log showed it was killed by the first one it moved over.
 
@Civitar, thanks for clearing that up!
@bane_, wow, that is truly interesting. (But kinda logical too, since the unit moves a hex if it embarks)
@Klisz, good to note! I guess serial events are not the way to go ;)
 
@Troller0001: I think that while you can have stuff happen on the first tile a unit moves over, if it is moving over a tile en route to another it will finish its graphical movement first (the models running across the map).
 
While Klisz is surely correct about SerialEvents, I do not recall any issues with this particular one. In fact, one great tool for modders, by Machiavelli24, was widely used before the last BNW Lua API & hooks patch hit, and was tied to SerialEvenUnitCreated.
And Machiavelli's version of SerialEventUnitCreated should still be used because the Firaxis version can fire more than once for the same unit. Example: it fires for every unit when a game is reloaded from a save, because part of the "load saved game" process is placing units on the map, and this is considered a "unit creation".
 
@Civitar, @LeeS, That both shouldn't matter, since I'll just be applying and un-applying a promotion (see the Lua code below). Anyway, since the UnitSetXY also fires when a unit is created, it should work normally.
I'm not sure if this'll lag (a lot) in the late-game (when there are many units), and how much it'll lag if it does. If any of you have alternatives, don't hesitate to post them! (I'm just starting out with (Civ 5) Lua and I'm here to learn stuff :))

Spoiler :

Code:
-- SetWaterPromotion
-- Author: Troller0001 (Special thanks to the CivFanatics Forum)
-- DateCreated: 4/2/2016 2:54:15 PM
--------------------------------------------------------------

--applies the water-promotion if the unit is next to a coast-tile and removes it if it isn't
--The reason why I don't check for a specific Civ is because city states can gift UU's as well. Also, I don't see a reason why someone would enable a custom civ if they're not gonna include them in their game

local iPromotion = GameInfoTypes.PROMOTION_NEARCOAST_BUFF
local iDummyPromotion = GameInfoTypes.PROMOTION_DUMMY_NEARCOAST_BUFF

GameEvents.UnitSetXY.Add(function(iPlayer, iUnit, iPlotX, iPlotY)
		if(iUnit:IsHasPromotion(iPromotion) or iUnit:IsHasPromotion(iDummyPromotion)) then -- if the unit has either the actual promotion or the dummy promotion
			local pPlot=iUnit:GetPlot();

			if(pPlot:IsCoastalLand())then--if the unit is next to water
				iUnit:SetHasPromotion(iDummyPromotion, false)--if it is, apply the actual promotion
				iUnit:SetHasPromotion(iPromotion, true)
			else
				iUnit:SetHasPromotion(iDummyPromotion, true)--if it isn't, apply the dummy promotion
				iUnit:SetHasPromotion(iPromotion, false)
			end
		end

end)


@bane_ Oh good to note that this SerialEvent will work unlike many others, thanks!
 
And Machiavelli's version of SerialEventUnitCreated should still be used because the Firaxis version can fire more than once for the same unit. Example: it fires for every unit when a game is reloaded from a save, because part of the "load saved game" process is placing units on the map, and this is considered a "unit creation".

Indeed, but one can use his workaround (which is quite simple/genius) with the current game version.
 
@whoward69, very useful thread, thanks! However, there's this little dilemma I'm stuck with right now. I either have to prevent major lag with the method you showed me (which currently will be my choice), or the civ's UU's that CS gift when the civ is not in-game are bugged (in a sense that their promotion will not work). Talking about CS unit gifting, is it possible to prevent a specific UU to be gift-able, or is there a way to check and change which UU the CS gifts?

@bane_, ooh, sounds interesting. I'll certainly check that one out!
 
standard game does this, pop up the CS dialog box, and it's on one of the mouse-over tooltips
Yes, I knew about that, but I meant to ask if i could check it within a (lua) code. (Though since you can change/prevent a unit gift, I would assume that you could check which unit it actually is ;))
 
Yes, I knew about that, but I meant to ask if i could check it within a (lua) code.

The UI is written in Lua, so look to see how the UI code does it!
 
@whoward69 Oooh, I'll most certainly do that! Thanks for all of your help!
 
Events.SerialEventUnitCreated(PlayerID player, UnitID unitID, int hexVec, int unitType, int cultureType, int civID, int primaryColor, int secondaryColor, int unitFlagIndex, int fogState)
What's unitType? It's not unit type ID that you get with pUnit:GetUnitType().

How do find this event in source code? Searching for SerialEventUnitCreated gives no results.
 
How do find this event in source code? Searching for SerialEventUnitCreated gives no results.

You don't - because we don't have the source code for the game engine (.exe) or the UI dll - which is where these events come from.

Only the GameEvents are in the game core dll (the one we have the source code for)

The Events are either documented by Firaxis (can't remember where the file is, but it's in the SDK) or from trial-and-error

HTH

W
 
What's unitType? It's not unit type ID that you get with pUnit:GetUnitType().

How do find this event in source code? Searching for SerialEventUnitCreated gives no results.
The only parameters I ever use from SerialEventUnitCreated are PlayerID and UnitID because I can use those to get any other information I need.
Code:
function UnitCreated(PlayerID, UnitID, hexVec, unitType, cultureType, civID, primaryColor, secondaryColor, unitFlagIndex, fogState)
	local pPlayer = Players[PlayerID]
	local pUnit = pPlayer:GetUnitByID(UnitID)
	local iUnitType = pUnit:GetUnitType()
	--Do Other Stuff
end
Events.SerialEventUnitCreated.Add(UnitCreated)
The fourth parameter as "unitType" I am not sure is entirely accurate. I remember having some wildly unexpected results when relying on it being equal to pUnit:GetUnitType().
 
Back
Top Bottom