Help with getting Plot in Python

Thorn

King
Joined
Jan 5, 2002
Messages
820
Location
Project Tic-Toc
I'm (still) trying to learn Python and I have a basic question. If a certain unit dies (ie, a warrior) and you want to know (and store) what plot he died on, how do you do that? So far I've been able to use onUnitKilled to pop up a message when a warrior dies. Hey, it's a start! :D
 
I'm a programmer by trade but not in Python. However, as with spoken languages you can figure a lot from other languages, so prepare your grain of salt. :)

First, Apolyton has a good start at a cIV Python reference that can really help.

From the CyUnit object argument to the event, you can get its X and Y coordinates on the map. Use those with the "global context" to acquire the CyPlot.
Code:
unit, iAttacker = argsList
map = gc.getMap()
plot = map.plot(unit.getX(), unit.getY())
 
Can you define "not working"? Make sure you have logging enabled in the cIV.ini file. It should tell you the error, file, and line number.

Make sure you have
Code:
gc = CyGlobalContext()
at the top of your file.

Here's an example of printing a value to the screen (in this case an integer):
Code:
iGameTurn = 50
szMessage = "Game Turn %d" %(iGameTurn)
CyInterface().addMessage(CyGame().getActivePlayer(), True, 10, szMessage, "", 2, None, ColorTypes(8), 0, 0, False, False)
There's a much simpler method, but I haven't tried it and don't know how to enable it. I assume in the ini file as well, so you might give it a shot. This is what cIV uses for its debugging messages.
Code:
CvUtil.pyPrint(message)
 
I still can't seem to grab the plot that the unit dies on. Here's the code I have in my CustomEventManager file:

Code:
	def onUnitKilled(self, argsList):
		'Unit Killed'
		objUnit = argsList[0]
		map = gc.getMap()
		# plot = map.plot(unit.getX(), unit.getY())
		if(objUnit.getUnitType() == gc.getInfoTypeForString("UNIT_WARRIOR")):
			self.popup("", "A warrior died...")
			CyInterface().addImmediateMessage("A Warrior died...", "") 
			iGameTurn = 500
			szMessage = "Game Turn %d" %(iGameTurn)
			CyInterface().addMessage(CyGame().getActivePlayer(), True, 10, szMessage, "", 2, None, ColorTypes(8), 

0, 0, False, False)

The line: plot = map.plot(unit.getX(), unit.getY()) seems to cause problems.
Everything else works okay in the game (I get the popup messages).
By the way, I do have the gc = CyGlobalContext() line at the top ;)
 
gc.getMap() is not a valid function... What you'll want is
Code:
CyMap().plot(objUnit.getX(),objUnit.getY())
 
gc.getMap() is not a valid function...

Bummer, it's listed as such at the Apolyton site. :( And yes, you must use objUnit to match the variable name you declared above. My code declared it as
Code:
unit, iAttacker = argsList
Python, and other programming languages, are very precise when it comes to naming things -- unlike humans. You can refer to me as EF and I'll know what you mean. Of course, Edward Francis might get confused. ;)
 
gc.getMap() is not a valid function... What you'll want is
Code:
CyMap().plot(objUnit.getX(),objUnit.getY())

That seemed to do the trick! I used this code and was able to print values for mx and my. I'm assuming the numbers printed were the Plot x,y coordinates on the map...

Code:
mx = objUnit.getX()
my = objUnit.getY()

Thanks for the help EmperorFool & IVZanIV!:goodjob:
And yes, the precise naming thing can be frustrating at times:)
 
Back
Top Bottom