Python pickle problem

Bad Player

Deity
Joined
Oct 31, 2005
Messages
3,534
Location
(Bris)Vegas!
Hi, I've been working on this code with the help of some other ppl but we can't work out why the program crashes on turn 9. I've run the program a few times and every time it crashes on turn 9. I use worldbuilder to place a few units and move them into tile (10,10) where they get the HELD promotion (so they can't move) and the program teleports them N/S/W/NW/NE (except once it moved them SE to avoid opponent borders which was unexpected).


CvEventManager (in FFH2/MNAI):
Code:
		#NEW CODE STARTS HERE
        #This code should give all units in tile(10,10) the HELD promotion so that they can't move anywhere.
		if iGameTurn < 5:
			pPlot = CyMap().plot(10,10)
			for i in range(pPlot.getNumUnits()):
				pUnit = pPlot.getUnit(i)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_HELD'), True)
				#pUnit.setScriptData("StackofDoom")
                   
		#This should teleport all units in the tile to an adjacent clear tile not east of the tile
		if iGameTurn == 5:
			pPlot = CyMap().plot(10,10)
			pPlotWest = cf.findWestClearPlot(-1, pPlot)
                           
			#save new data to the actual game file
			iNewX = pPlotWest.getX()
			iNewY = pPlotWest.getY()
			myPickle = [iNewX, iNewY]
			serializedMyPickle = pickle.dumps(myPickle)
			gc.getGame().setScriptData(serializedMyPickle)
                           
			#apply new data on units
			for i in range(pPlot.getNumUnits(), -1, -1):
				pUnit = pPlot.getUnit(i)
				pUnit.setXY(iNewX, iNewY, False, True, True)
     
		if iGameTurn > 5:      
			if iGameTurn <=12:
				# get old data from the actual game file
				serializedMyPickle = gc.getGame().getScriptData()
				myPickle = pickle.loads(serializedMyPickle)
				iOldX = myPickle[0]
				iOldY = myPickle[1]
				pPlot = CyMap().plot(iOldX, iOldY)
				pPlotWest = cf.findWestClearPlot(-1, pPlot)
                                   
				#save new data to the actual game file
				iNewX = pPlotWest.getX()
				iNewY = pPlotWest.getY()
				myPickle = [iNewX, iNewY]
				serializedMyPickle = pickle.dumps(myPickle)
				gc.getGame().setScriptData(serializedMyPickle)
                                   
				#apply new data on units
				for i in range(pPlot.getNumUnits(), -1, -1):
					pUnit = pPlot.getUnit(i)
					pUnit.setXY(iNewX, iNewY, False, True, True)

The error messages are:
KeyError: 0
Line 567 CvEventManager (which is iOldX = myPickle[0])
BugEventManager line 361
Traceback (most recent call last)
 
MNAI includes BUG and revolutions, which uses the SDToolkitCustom to store mod data. SDToolkitCustom uses script data of various objects, therefore you should expect crashes when using script data.
On the other hand, SDToolkitCustom provides a pretty simple interface for storing key/value pairs without having to fiddle with pickle, so you can just use it for your purpose. See examples in BarbarianCiv.py and Revolutions.py
 
Using BugData to save data and load data should go something like this:

Save data:
BugData.getTable("MY_TABLE").setData(serializedMyPickle)
instead of the gc.getGame().setScriptData(serializedMyPickle)

Load data:
serializedMyPickle = BugData.getTable("MY_TABLE").data
instead of the serializedMyPickle = gc.getGame().getScriptData()

At the start of the .py file you need this:
import BugData

And that is pretty much it for the basic usage. The BugData stuff takes care of everything else.
 
Back
Top Bottom