| General | Hosted Sites | Civ5 | CivRev | Civ4Col | Civ4 | Civ3 | Civ2 | Civ1 | Misc | Marketplace |
![]() |
|
|
Welcome to Civilization Fanatics' Center. You are currently viewing our site as a guest which gives you limited access to our site features. By joining our free community, you will be able to participate in the discussions, search the forum, send private messages, vote in polls, upload your own screenshots to the gallery, and access many other special features. Registration is fast, simple and absolutely free, so sign up today! If you have any problems with the registration process or your account login, please contact support. |
|
|||||||
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Chieftain
Join Date: Aug 2011
Posts: 4
|
Grab a unit from a Unit Event
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 |
|
|
|
|
|
#2 |
|
Crazy Engineer
Join Date: Mar 2007
Posts: 134
|
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 );
|
|
|
|
|
|
#3 |
|
Mad Scientist
Join Date: Sep 2005
Location: Los Angeles, CA
Posts: 3,063
|
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.
__________________
Once upon a time there was the Ages of Man mod set. Then Firaxis broke it. But thanks to the DLL efforts of the community, the mods shall rise again! |
|
|
|
![]() |
| Bookmarks |
|
| Thread Tools | |
|
|