Python Loops and Counter ...

Now that I've gotten into it, I'm making slight progress. For one, no python errors upon loading the mod. Secondly, more parts of the code are working. I took out the SD-Toolkit related parts and decided to just do it the 'old fashioned way' as you have shown me how to do, Belizan. For some reason I had a mental block earlier and couldn't understand, but now coming back to it I see that it was pretty simple, especially since you gave me your own code:

Code:
	def initPlotDictionary(pPlot):
            plotDict = {}
            pPlot.setScriptData(cPickle.dumps(plotDict))

        def getPlotDictionary(pPlot):
            plotDict = cPickle.loads(pPlot.getScriptData())
            return plotDict
    
        def setPlotDictionary(pPlot, plotDict):
            pPlot.setScriptData(cPickle.dumps(plotDict))

        def getTurnImpBuilt(pPlot):
            plotDict = getPlotDictionary(pPlot)
            return plotDict["turnImpBuilt"]

        def getOriginalOwner(pPlot):
            plotDict = getPlotDictionary(pPlot)
            return plotDict["originalOwner"]

My onImprovementBuilt looks something like this:
Code:
	def onImprovementBuilt(self, argsList):
		'Improvement Built'
		self.parent.onImprovementBuilt(self, argsList)
                iImprovement, iX, iY = argsList
                pPlot = gc.getMap().plot(iX, iY)
                pPlayer = gc.getPlayer(pPlot.getOwner())
                if (iImprovement == gc.getInfoTypeForString('IMPROVEMENT_ANIMAL_DEN')):
                    newUnit = pPlayer.initUnit(gc.getInfoTypeForString('UNIT_AXEMAN'), pPlot.getX(), pPlot.getY(), UnitAITypes.NO_UNITAI)
                    self.initPlotDictionary(pPlot)
                    self.setPlotDictionary(pPlot, plotDict)
This game me an error:
initPlotDictionary() takes exactly 1 argument (2 given)
So I changed to this instead:
Code:
self.initPlotDictionary()
setscriptdataerror6lp.jpg


Now, what other argument was I giving in the first example? And, after changing it, why the new error about setScriptData? How are these issues resolved?

In order to stop 1 check for each player, I changed the code to "onBeginGameTurn" instead of "onBeginPlayerTurn." It works nicely. I set up popups after my if statements to make sure I at least made it that far. The map scan for the improvement does in fact return a value that it found the improvement, and the appropriate popup comes up every turn after the improvement was built, so I'm happy about that.
 
Well, first off, you don't want to call setPlotDictionary immediately after calling initPlotDictionary. Particularly since you call setPlotDictionary without a valid plotDict object. But that's not your problem.

Your problem is that you defined these functions (setPlotDictionary, initPlotDictionary, etc.) inside a class. Python's implementation of classes has it add on an extra parameter to all methods of a class--self. It appears as the first parameter to every method (function) defined in a class, hense all those OnMumbleMumble(self, argsList) functions. If you want to leave initPlotDictionary et al as methods defined on a class, you need to change their definition to start with self. E.g. initPlotDictionary(self, pPlot). Remember, when calling that function you won't particularly want to include the self parameter.

Alternatively, (or if this is confusing) you could move the plotDictionary function definitions to come before the class CvShqypeEventManager(CvEventManager.CvEventManager) line. Then you would call them directly (initPlotDictionary(pPlot) not self.initPlotDictionary(pPlot)) and your parameter issues would go away.

I thought these things were covered in the Python tutorial in this forum?
 
Sorry Belizan because I haven't gotten into this in a while, but right now my problem seems to be using a variable that was stored in the script data after recalling it. Can you show me how you do it?
 
Ok, I don't think I understand your question. The literal answer is.. You use it like any python variable. But clearly that's not what you meant.

I don't know enough about what you've actually done to intuit the answer to your question. Maybe what you really want is some code to lift. I have lots of code.

Code:
    def onBuildingBuilt(self, argsList):
        pCity = argsList[0]
        iBuildingType = argsList[1]
        
        pPlayer = gc.getPlayer(pCity.getOwner())
        eBuilding = gc.getBuildingInfo(iBuildingType).getType()
        iX = pCity.getX()
        iY = pCity.getY()
        
        # Biodomes and Bunkers
        if(iBuildingType == gc.getInfoTypeForString("BUILDING_CITY_ARCHITECTURE_BIODOME_REFIT") or iBuildingType == gc.getInfoTypeForString("BUILDING_CITY_ARCHITECTURE_BIODOME")):
            CityUtils.setArchitecture(pCity.plot(), "Biodome")

.....
### CityUtils.py:
from CvPythonExtensions import *
from types import *
import random
#import string        #You don't need this
import cPickle
#import Config     #You don't use this

gc = CyGlobalContext()

def initCityDictionary(pCity):
    cityDict = {}
    pCity.setScriptData(cPickle.dumps(cityDict))
    
def getCityDictionary(pCity):
    if(pCity.getScriptData() != ""):
        cityDict = cPickle.loads(pCity.getScriptData())
    else:
        cityDict = {}
    return cityDict
    
def setCityDictionary(pCity, cityDict):
    pCity.setScriptData(cPickle.dumps(cityDict))
    
def getCityDictionaryValue(pCity, sValKey):
    cityDict = getCityDictionary(pCity)
    if(cityDict.has_key(sValKey)):
        return cityDict[sValKey]
    else:
        return 0
        
def setCityDictionaryValue(pCity, sValKey, oVal):
    cityDict = getCityDictionary(pCity)
    cityDict[sValKey] = oVal
    setCityDictionary(pCity, cityDict)
    
def increaseCityDictionaryValue(pCity, sValKey, iIncrement):
    cityDict = getCityDictionary(pCity)
    cityDict[sValKey] = cityDict[sValKey] + iIncrement
    setCityDictionary(pCity, cityDict)
    return cityDict[sValKey]
    
def removeCityDictionaryValue(pCity, sValKey):
    cityDict = getCityDictionary(pCity)
    if(cityDict.has_key(sValKey)):
        del cityDict[sValKey]
    setCityDictionary(pCity, cityDict)
    
def setArchitecture(pCity, sArchitecture):
    cityDict = getCityDictionary(pCity)
    cityDict["architecture"] = sArchitecture
    setCityDictionary(pCity, cityDict)
    
def getArchitecture(pCity):
    cityDict = getCityDictionary(pCity)
    if(cityDict.has_key("architecture")):
        return cityDict["architecture"]
    else:
        return "Default"

...

The first is taken from the Moon mod's custom CvEventManager class. The second is enough of CityUtils.py to stand on it's own. There is nothing special about the code in CityUtils, and it could just as easily be used for units, or plots or what have you (as in fact I do in this very example).
 
Back
Top Bottom