DOCUMENTATION FOR MISSIONOBJVAR MODULE, v.1.18 Mission and Object Variable Functions, with Examples

By Stumpy
The general idea is pretty straightforward: Ever write a script with a nice list of things you are tracking and changing during the mission and it all works fine until someone saves in the middle of the mission and comes back and this list isn't there?  These are functions to assign and retrieve that sort of data in a way that doesn't get lost between saves and reloads.  As such, they are just like Mission_SetAttr(), Mission_GetAttr(), Object_SetAttr(), and Object_GetAttr(), except that they can assign and retrieve any variable type instead of just floats.  These are mission and object variables, instead of attributes.

 

Mission Variable Functions

Mission_SetVar(varName,val)

Set the mission variable varName to have value valval can be a variable of most any type: int, float, string, list of the former, dictionary of the former, etc.

Mission_GetVar(varName,defaultVal=None,setToDefault=0)

Return the value for mission variable varName.  If no such mission variable exists, the value given as defaultVal is returned.  If the setToDefault flag is 1, then the mission variable is created and given the value defaultVal and that value is returned.

Mission_VarExists(varName)

Does the mission variable varName exist? 

Example using all three functions:
# Our ubervillian occasionally deigns to inflict a long-term curse on the pesky heroes
	def OnCurse(victim):
    if Mission_VarExists('cursed_heroes'):
        cursedlist = Mission_GetVar('cursed_heroes')
    else:
        cursedlist = []
    cursedlist.append(victim)
    Mission_SetVar('cursed_heroes',cursedlist)

Simpler example using the defaultVal parameter to streamline the code. (This is functionally equivalent to the above code.):
# Our ubervillian occasionally deigns to inflict a long-term curse on the pesky heroes
	def OnCurse(victim):
    cursedlist = Mission_GetVar('cursed_heroes',[])
    cursedlist.append(victim)
    Mission_SetVar('cursed_heroes',cursedlist)

Mission_AttrExists(attrName)

Returns 1 if the mission attribute (not variable) attrName exists, returns 0 otherwise.  Just a handy utility function.

 

Object Variable Functions

Object_SetVar(object,varName,val)

Set the object variable varName for object to have the value val.

Assign a bit of silly data:
Object_SetVar('lar_gand','girlfriend','tasmia_mallor')
	Object_SetVar('querl_dox','girlfriend','kara_zor_el')
	Object_SetVar('salu_digby','girlfriend','ayla_ranzz ')
	lars_kinfolk = {'del_gand':'brother',
                'laurel_gand':'great_great_great_great_niece',
                'eltro_gand':'great_great_great_great_nephew',
                'kal_el':'adopted_brother'}
	Object_SetVar('lar_gand','relatives',lars_kinfolk)

Object_GetVar(object,varName,defaultVal=None,setToDefault=0)

Return the value of object variable varName for object.  If no such object variable exists, the value given as defaultVal is returned.  If the setToDefault flag is 1, then the object variable is created and given the value defaultVal and that value is returned.  If no object named object exists, then the default value defaultVal is returned, but no attempt is made to create the object variable (or object).

Taunt some heroes:
for hero in getAllHeroes():
    if Object_VarExists(hero,'girlfriend'):
        print '%s and %s, sitting in a tree, K-I-S-S-I-N-G!' \
              % (hero,Object_GetVar(hero,'girlfriend'))

Object_VarExists(object,varName)

Does object have the variable varName?

 

Module Notes:

(These notes are primarily of interest to those wanting to use missionobjvar in missions or Rumble Room sessions that are not using FFX 3.2 or later.)

The module has undergone some changes since previous versions.

First, for much better performance, caching is integrated into the module and the caching is dependent on mission type. That means that, for campaign missions, the mission and object variables use write-through caching and are saved-game safe (if you save a game and reload it, the same mission and object variables will be present and will have the values they had when saved). For Rumble Room sessions where games cannot be saved, simulated mission and object variables are used and no time is wasted reading from or writing to game attributes.

A very important change is that the module now does proper object variable retirement, both in cached and uncached versions. That means that we avoid crashes and game interruptions when trying to access object variables for objects that no longer exist in the game. For this to work, the mlogreader module must be running and the activation MLOG module started. That is true by default if FFX 3.2 or greater is running.

Those changes mean that the module requires the presence of python files (modules) mlogreader.py, readsavedattribs.py, and compiled python module _missionvar.pyd somewhere in the python path, most likely the same directory as missionobjvar.py itself. The chunkstring module is no longer required for missionobjvar (though it may still be needed for other modules to work).