View Full Version : Help with getting Plot in Python


Thorn
Mar 18, 2007, 05:45 PM
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

EmperorFool
Mar 18, 2007, 06:19 PM
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 (http://civilization4.net/files/modding/PythonAPI/) 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.

unit, iAttacker = argsList
map = gc.getMap()
plot = map.plot(unit.getX(), unit.getY())

Thorn
Mar 19, 2007, 08:18 AM
Thanks for the response! That's kind of the approach I was taking but it doesn't seem to be working. :mad:
Also, is there a way to print out values in-game on the screen for debugging? I would like to print variables on screen to check their values.

EmperorFool
Mar 19, 2007, 08:40 AM
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

gc = CyGlobalContext()

at the top of your file.

Here's an example of printing a value to the screen (in this case an integer):

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.

CvUtil.pyPrint(message)

Thorn
Mar 19, 2007, 12:46 PM
I still can't seem to grab the plot that the unit dies on. Here's the code I have in my CustomEventManager file:

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 ;)

Thorn
Mar 19, 2007, 12:49 PM
Actually, if I correct the line: plot = map.plot(ojbUnit.getX(), objUnit.getY()), it doesn't causes errors. :mischief:

But, now nothing works. Not even the popup messages...

IVZanIV
Mar 19, 2007, 02:32 PM
gc.getMap() is not a valid function... What you'll want is CyMap().plot(objUnit.getX(),objUnit.getY())

EmperorFool
Mar 19, 2007, 04:30 PM
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

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. ;)

Thorn
Mar 19, 2007, 05:02 PM
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...

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

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

Maniac
Mar 19, 2007, 05:10 PM
pPlot = unit.plot()

?