Create / Destroy Units using Python

ruff_hi

Live 4ever! Or die trying
Joined
Oct 24, 2005
Messages
9,135
Location
an Aussie in Boston
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:

PHP:
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) ...

PHP:
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?
 
Definitely possible, give me a moment and I'll write up some code for you if you'd like...
 
Code:
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.
 
I'll take a crack at the second request.

Code:
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:

Code:
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().
 
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.
 
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!" :)
 
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.
 
sample code to create a unit ...

Code:
				iplayer = pPlot.getOwner()
				pPlayer = gc.getPlayer(iplayer)
#				pPlayer = CyGame().getActivePlayer()
#				pPlayer = CyGame().getActivePlayer()
				zNewUnit = pPlayer.initUnit(gc.getInfoTypeForString('UNIT_PROPHET'), pPlot.getX(), pPlot.getY(), UnitAITypes.NO_UNITAI)
				zUnitType = pLoopUnit.getUnitType()
				zMsg = "Unit Type %i" % (zUnitType)
				CyInterface().addImmediateMessage(zMsg, "")
 
Back
Top Bottom