View Full Version : [NOOB Python] Why won't they attack damn it!


Feeder74
Nov 13, 2007, 04:48 PM
I am trying to make a total war situation at 50 turns of play, where everybody is forced to suddenly be at war with one another.
I have written this into the CvEventManager.py def onBeginPlayerTurn(self, argsList):
'Called at the beginning of a players turn'
iGameTurn, iPlayer = argsList
if (iGameTurn==50):
team=gc.getTeam(gc.getPlayer(iPlayer).getTeam())
if (team.isAlive()):
for i in range(gc.getMAX_PLAYERS()):
team2=gc.getTeam(gc.getPlayer(i).getTeam())
if (i!=iPlayer and team2.isAlive()):
team.declareWar(team2,false); It doesn't work, could someone with a bit of python exp tell me what I am doing wrong here?

Sto
Nov 13, 2007, 06:46 PM
if (i!=iPlayer and team2.isAlive()):
team.declareWar(team2,false);

There is no tabulation with this test , team2 must be an integer in declareWar , the ";" is a synthax error i think ... you're code doesn't prevent 2 players of the same team to declare war each other (don't know what happen in this case ) . You should enable the debugger to check python error .

Tcho !

Feeder74
Nov 13, 2007, 08:11 PM
Well I changed it to this... if (iGameTurn==50):
team=gc.getTeam(gc.getPlayer(iPlayer).getTeam())
if (team.isAlive()):
for i in range(gc.getMAX_PLAYERS()):
team2=gc.getTeam(gc.getPlayer(i).getTeam())
if (i!=iPlayer and team2.isAlive()):
team.declareWar(i,false) and it still isn't working for me, but thanks sto, on the note of poor indenting and syntax, is there a good IDE somewhere for this stuff, I am just using notepad at the moment.
I have Code::Blocks already installed as of last night, but since I can't get the SDK to compile, I am wondering now if it can be configured to do python, anyhow will have a look straight after this ;)
I have the debugger enabled - just can't follow the error message it spit out last time - I will have a look again in a few minutes to find out what it says this time :)

Well I had a good look through the error messages, looks 1,688KB of this message to me...Traceback (most recent call last):

File "CvEventInterface", line 23, in onEvent

File "CvEventManager", line 164, in handleEvent

File "CvEventManager", line 464, in onUnitMove

ValueError: too many values to unpack
ERR: Python function onEvent failed, module CvEventInterface...anyhow this means diddly to me, as it isn't even refering to the edits I made :(

Sto
Nov 14, 2007, 03:38 AM
Here the python tutorial (http://forums.civfanatics.com/showthread.php?t=154130) where you can find python an how to debug ( if not just search python with google ) . This is a strange bug , you have made no change in onUnitMove ? and what is the line where you get an error ? . Notepad is not a good editor because it can cause some tab problems ( a mix of spaces and tab ) . Anyway , the code will not work in a game with a team . You should make a loop with teams and not players (perhaps place this function in onBeginGameTurn ) . You should check if the team canDeclareWar also .

If you have some problems , do not hesitate to post your file with all the changes you've made in it , the version you play .

Tcho !

PeteT
Nov 14, 2007, 04:52 AM
This can't be right:


team2=gc.getTeam(gc.getPlayer(i).getTeam())

Feeder74
Nov 14, 2007, 07:25 AM
thanks for the link to the tut - I hadn't seen that one - and it is very helpfull for where I am at ;)
As for the 1,688KB of error messages I mentioned above - I think this may hve been caused by using Vanilla file instead of BTS3.13

Anyhow after some mucking around I ended up with this...
def onBeginGameTurn(self, argsList):
'Called at the beginning of the end of each turn'
iGameTurn = argsList[0]
CvTopCivs.CvTopCivs().turnChecker(iGameTurn)
#***Start of edit!***
if (iGameTurn==50):
for iPlayerLoop in range(gc.getMAX_PLAYERS()):
pPlayer=gc.getPlayer(iPlayerLoop)
for iPlayerLoop2 in range(gc.getMAX_PLAYERS()):
pPlayer2=gc.getPlayer(iPlayerLoop2)
if (pPlayer.getTeam()!=pPlayer2.getTeam()):
gc.getTeam(iPlayerLoop).declareWar(gc.getPlayer(iP layerLoop2).getTeam(),false,0)
it does what was intended, and I'd just like to say thanks heaps for getting me started with this - I sometimes find it hard to come to a new API and get the layout of it the first few time, hopefully next time, I might have something a bit more elaborate for you to wrap your minds around ;)
PS: If you see anyways of optimizing that last line of code - I'd love to hear it - to me it seems - well ugly, and that isn't good, and prolly isn't bad either since it does do what it was meant too, but I like my stuff to look nice too ;)