View Full Version : DataStorage question


RogerBacon
Jan 25, 2006, 07:02 PM
I'm trying to use the DataStorage class Bhruic made and I'm having a bit of trouble.

I want to store three values for a city: the gold, research and culture generated by specialists each turn. Basically, I'm doing something to add to it each turn. The problem is that I want to add 2 each turn but using City.changeSpecialistCommerce() is cumulative. i.e. 2, 4, 6, etc. So, I decided to store last turn's bonus, add the new bonus (which might be the same) minus last turn's bonus. So, I did this:

iRandNum = gc.getGame().getMapRand().get(3, "Type of commerce") # gold, science, culture

lastTurnBonusCommerce = data.getVal('Workers', pCity.getID(), iRandNum, pCity)
...
bonusCommerce += pUnit.getLevel()
...
pCity.changeSpecialistCommerce(iRandNum, bonusCommerce - lastTurnBonusCommerce )

specialistCommerce = pCity.getSpecialistCommerce(iRandNum)

data.setVal('Workers', pCity.getID(), iRandNum, specialistCommerce , pCity)

Unfortunately, I'm not using the dataStorage class correctly, I guess. For one thing, I don't exactly understand what pEntity is supposed to be. Does anyone have any examples where they have used these functions of this class?

The error I get is:
File "DataStorage", line 78, in getVal
KeyError: 1

1 in this case means that 'science' was chosen as the random commerce type to get the bonus that turn. If I get rid of the random element it seems to work fine.


Thanks,
Roger Bacon

Kidinnu
Jan 26, 2006, 04:26 AM
You're increasing a *random* commerce by 2?

You'll need to check with Bhruic, or look at the internals of his class, but it sounds like what's happening is that you randomly store one of the commerce types - say, 0 - then on a future turn you try to load another - in this case, 1. 1 was never stored, and so you're getting an error.

Since you've got that random term, you aren't reading last turn's commerce, you're reading commerce from some random (long) time ago. Even if you're only going to read one commerce each turn, you need to store all three. But I really am scratching my head over the random thing.

The Great Apple
Jan 26, 2006, 08:20 AM
I didn't know Bhruic had something to do data storage. Where is it located? I've always used Stone-D's code.

RogerBacon
Jan 26, 2006, 10:54 AM
Well, I've removed the random part and simply made it three seperate things. It still seems strange that a call to getVal() would cause a problem the second time but not the first time. I suppose I'll have to loko at it some more.

Great Apple, I got it from the culture decay mod. That has an example of how to use getSingleValue() but not getVal() which seems to be a little more complicated.

Roger Bacon