Grab a unit from a Unit Event

whoshotdk

Chieftain
Joined
Aug 12, 2011
Messages
4
Hi,

Im probably being really thick here, but Im trying to grab the currently 'selected' unit (whenever a unit selection changes, i grab the newly selected unit) so that I can check its type and the feature (if any) its standing on.

I found the UnitSelectionChanged event, so I tied a listener function to it and determined that my listener is being passed 5 parameters (Ive guessed what they do when I can):

Code:
function doSomething(unknownvar1, some_object, x, y, unknownvar3, unknownvar4, unknownvar5)
print("Output: ["..tostring(unknownvar1).."] ["..tostring(some_object).."] ["..tostring(x).."] ["..tostring(y).."] ["..tostring(unknownvar3).."] ["..tostring(unknownvar4).."] ["..tostring(unknownvar5).."]");
end
Events.UnitSelectionChanged.Add(doSomething);


The event seems to fire, and I get differing values when I select units, I.e:

Output: [0] [24578] [7] [14] [0] [true] [false]
Output: [0] [8192] [6] [14] [0] [true] [false]
Output: [0] [8192] [6] [14] [0] [true] [false]


So, I figured the 2nd parameter (some_object) is a Unit object, but trying to use a getUnitType() on it gives me a 'attempt to index local 'some_object' (a number value) runtime error.

So, perhaps it is just an ID of the unit. But how do I get 'at' the unit so that I can retrieve its details (check its type etc)?

Also, does anyone know if the event 'UnitSelectionChanged' would also fire when the AI changes their unit selection, not just the player?

Thanks
Dave
 
Turning on an option to search through text in files is really a life-saver on Windows. If you just search the LUA-files in the Civ-folder for that event this pops up in the UnitFlagManager.lua:

Code:
-------------------------------------------------
-------------------------------------------------
function OnUnitSelect( playerID, unitID, i, j, k, isSelected )

    if( Players[ playerID ] == nil or
		not Players[ playerID ]:IsAlive() or
        Players[ playerID ]:GetUnitByID( unitID ) == nil or
        Players[ playerID ]:GetUnitByID( unitID ):IsDead() )
    then
        return;
    end
    
    if( g_MasterList[ playerID ] == nil or
        g_MasterList[ playerID ][ unitID ] == nil ) 
    then
        print( string.format( "Unit not found for OnUnitSelect: Player[%i] Unit[%i]", playerID, unitID ) );
    else
        g_MasterList[ playerID ][ unitID ]:UpdateSelected( isSelected );
    end
end
Events.UnitSelectionChanged.Add( OnUnitSelect );

The IsDead-function is one of those functions for the unit-objects so the "unit" just seem to be Players[ playerID ]:GetUnitByID( unitID ).
 
The IsDead-function is one of those functions for the unit-objects so the "unit" just seem to be Players[ playerID ]:GetUnitByID( unitID ).

Correct. Nearly every Lua event involving units is structured this way, with the player ID as the first argument, the unit ID as the second, and you have to use the GetUnitByID function to turn it into the useful Unit structure. It's actually kind of nice that they were so consistent on this; it almost makes up for the fact that many of the Lua functions either don't work at all, or just work very erratically.
 
Back
Top Bottom