Increase a variable in a per Turn loop

tesb

Emperor
Joined
Jan 16, 2010
Messages
1,593
I currently have an event that happens at every turn and i need a variable that increases inside the turn (for every loop of the event, i.e. it increases by 1 for every turn), but can be set to some arbitrary value.

The problem i have is that i need to give this variable an initial value, but if i do this it will reset the variable to this initial value every turn. How can i add a variable that i can give an initial value outside the per turn loop and toy with it inside the loop as i want?
 
and how would i do that in python?

sorry i am new to python, i only use fortran and matlab regularly.
 
Ah, that explains a bit :).

And i see it's even easier.

If you take a look at CvRandomEventInterface.py, at the top, then this part:
PHP:
import CvUtil
from CvPythonExtensions import *

gc = CyGlobalContext()
localText = CyTranslator()

the part is a) global and b) only loaded directly after game start.
You just have to define your variable somewhere there, nothing else.
 
so basically i do the following:

Code:
variable = initial value

event loop
'called every turn'

   if some condition
      variable = arbitrary value
   else
      variable = variable +1

this way the variable goes from the initial value and increases every turn by 1, except when the condition is met, then it is set to an arbitrary value. Is that correct?

(Sounds much like global variables stored in modules in fortran)


edit:
1) must i store this initial variable in CvRandomEventInterface.py or can i use my own python file?
2) if i must write it in CvRandomEventInterface.py, can i still access it from my own python file?

i want to keep things modular :)
 
Inside the function, you have to declare the variable as global. It's pretty much the only time you will declare a variable in Python. You only have to declare it as global if you are going to write to it. You can always read it.

Code:
foo = 7

def doThing()
	global foo  # want to be able to change global variable

	foo += 1  # increment global variable foo
 
fyi:

i asked sephi and he mentioned that i cannot use global python variables, because they are not written into a savegame. i gave me the the flowing functions to work with:

pPlayer.getPurityCounterCache1()
pPlayer.setPurityCounterCache1(value)

but i do not know if this helps players who do not use MoM. anyways my code works fine now. many thanks.
 
Custom values can be stored to save files, as script data, but is then attached to some properties (which is not really that important).

To save:
PHP:
    DataPlot = CyMap().plot(XValue,YValue)
    DataPlot.setScriptData(pickle.dumps(MyCustomValue))

To load:
PHP:
        DataPlot = CyMap().plot(XValue,YValue)
        MyCustomValue = pickle.loads(DataPlot.getScriptData ())

In this case, the info is attached to a plot, but like said, doesn't really matter what it is.
Edit: You have to import the module pickle to make it work.


But well, if it works, then there's no reason to change it :).
 
Back
Top Bottom