View Full Version : Create / Destroy Units using Python
ruff_hi Jul 02, 2007, 05:18 PM I would like to be able to destroy a unit using python. I have in mind a mod that sometimes wants to destroy Great Scientists. I am planning to put some code in the 'onUnitCreate' event (or similar) that does the following:
if human player
and unit.type = great scientist then unit.destroy
Similarly, sometimes if the player gets a Great Priest - I want to give an extra Priest ... (sample code) ...
if human player
and unit.type = great priest then unit(great priest).create
Is this possible? If so, can someone suggest some suitable civ4 python code?
IVZanIV Jul 02, 2007, 05:42 PM Definitely possible, give me a moment and I'll write up some code for you if you'd like...
IVZanIV Jul 02, 2007, 05:58 PM def onUnitCreated(self, argsList):
'Unit Completed'
unit = argsList[0]
player = PyPlayer(unit.getOwner())
#Starts here#
if player.isHuman():
if unit.getUnitType() == gc.getInfoTypeForString('UNIT_GREAT_SCIENTIST'):
unit.kill(False, unit.getOwner())
#Ends here#
Not really sure about the unit.kill(False, unit.getOwner()) line... Might want someone else to check or just test it and post here telling me, I'll figure it out if it's wrong.
EmperorFool Jul 02, 2007, 06:32 PM I'll take a crack at the second request.
def onUnitCreated(self, argsList):
'Unit Completed'
unit = argsList[0]
player = PyPlayer(unit.getOwner())
iUnitTypeGreatProphet = gc.getInfoTypeForString("GREAT_PROPHET")
if (player.isHuman() and unit.getUnitType() == iUnitTypeGreatProphet):
player.initUnit(iUnitTypeGreatProphet, unit.getX(), unit.getY(), -1)
I see in the API that CyUnit has this method:
void kill ( bDelay , PlayerType ePlayer )
I wonder if you could use that for your first feature. Perhaps pass in False and -1 or unit.getOwner().
IVZanIV Jul 02, 2007, 06:48 PM Yep, I edited to use that in my suggestion, but shouldn't the player index be 0 for human player rather than -1? I'll change it to the unit.getOwner() function anyway.
EmperorFool Jul 02, 2007, 06:54 PM shouldn't the player index be 0 for human player rather than -1?
My thinking on trying -1 is to mean "no player" as in when asking what bonus exists on a plot without respect to a particular player's knowledge. I assume that the parameter tells the game which player to say in a message killed the unit, as in combat-wise. I have no idea though, thus I figure unit.getOwner() should definitely work. I'm just preemptively trying to avoid "Your forces are under attack by YOU!" :)
ruff_hi Jul 02, 2007, 09:17 PM I've found that 'if player.isHuman()' doesn't actually work - always return true. Better to say 'if pUnit.getOwner() == CyGame().getActivePlayer()):' Sure - single player only - but this mod is aimed at single player only.
Anyway, thx for the info re killing a unit.
ruff_hi Aug 05, 2007, 05:54 AM sample code to create a unit ...
iplayer = pPlot.getOwner()
pPlayer = gc.getPlayer(iplayer)
# pPlayer = CyGame().getActivePlayer()
# pPlayer = CyGame().getActivePlayer()
zNewUnit = pPlayer.initUnit(gc.getInfoTypeForString('UNIT_PRO PHET'), pPlot.getX(), pPlot.getY(), UnitAITypes.NO_UNITAI)
zUnitType = pLoopUnit.getUnitType()
zMsg = "Unit Type %i" % (zUnitType)
CyInterface().addImmediateMessage(zMsg, "")
|
|