View Full Version : Multiple pieces of custom data (Python)


The Great Apple
Dec 05, 2005, 01:20 PM
Right, time for my daily "help I can't understand python" thread.

Is it possable to attach multiple custom pieces of data to an object (in my case, a unit). Looking at Bhruic's SettlerReligion Mod I see that he has attched the religion to the settlers as the leave the city.

What I am trying to do is to have multiple integer peices of data attached to every unit, which can be changed on events, and which determine the probabilites of other events occuring to that unit. At the moment I see that I can use the setScriptData() function to attach one non-integer value to the unit... but that's not what I want to do!

Looking at the function in the API, it refers to a file called pickle. I've found this file, but unfortunetely it's a .pyc file in the "system" folder, so I can't meddle with it.

Can it be done?

c.fe
Dec 05, 2005, 07:33 PM
In the Greek World Scenario it's done the following way:

def setupScriptData( self ):
"""Initialise the global script data dictionary for usage."""
# random variables
scriptDict = { 'iPunicFate': -1,
'iJerusalemFate': -1,
'iPersianFate': -1,
'iAlexanderFate': -1
}
gc.getPlayer(0).setScriptData( pickle.dumps(scriptDict) )

def getPunicFate( self ):
"""Returns PunicFate."""
scriptDict = pickle.loads( gc.getPlayer(0).getScriptData() )
return scriptDict['iPunicFate']

This is from GreekWorld.py. I've no idea, what exactly the pickle thingy does, but it should be fairly easy to adapt this one to your problem. Hope this helps.

Requies
Dec 05, 2005, 07:49 PM
Right, time for my daily "help I can't understand python" thread.

Is it possable to attach multiple custom pieces of data to an object (in my case, a unit). Looking at Bhruic's SettlerReligion Mod I see that he has attched the religion to the settlers as the leave the city.

What I am trying to do is to have multiple integer peices of data attached to every unit, which can be changed on events, and which determine the probabilites of other events occuring to that unit. At the moment I see that I can use the setScriptData() function to attach one non-integer value to the unit... but that's not what I want to do!

Looking at the function in the API, it refers to a file called pickle. I've found this file, but unfortunetely it's a .pyc file in the "system" folder, so I can't meddle with it.

Can it be done?

Pickle is Python's native way of storing objects in a string format. Do a search with python and pickle to get a better idea of what it does.

Req

Rayanth
Dec 05, 2005, 08:23 PM
pickles are basically a compressed representation of an object (regardless of type of object) -- it's an easy way to transfer data around between python scripts.

What the OP might be looking for is a tuple though -- basically a list. Tuples are type-generic so you can have different types of data stored in a tuple, and attach the tuple to the unit.

tuple1 = ( "item 1", "item 2", "item 3")

above is a tuple of 3 strings. the parenthesis define a tuple, its items are seperated by commas.

(note: I'm just diving in to python myself, but i believe the above to be true... you might want to do a sanity check before using it.)

The Great Apple
Dec 05, 2005, 08:51 PM
Ace! I knew there must be a way. If I combine what everybody has told me, with what is in Bhruic's SettlerReligion Mod and add a few functions here and there then hopefully it'll work.

EDIT: Ok, looking at it it would appear that tupes and directories are in GBMs tutorial! Ooops... RTT!