HowTo:Edit Missions:Python:Missions
XML Integration | Editing Missions | AI Scripts |
Python Inheritance In Missions
Ok lets dig further into the privateer.py module:
It is a very short module written in more or less pure python... I say more or less for a very specific reason: it inherits from a C++ class!
Director is a C++ class exported to python. All missions must inherit from director.... in this case the mission is very short as it uses other (pure python) modules. random_encounters.random_encounters, difficulty.difficulty and ,trading.trading all do NOT inherit from C++ as this class takes care of the C++ inheritance.
The constructor __init__ takes the arguments that we passed in from the XML file and passes them on to the various submodules. The only really really important thing you do in the constructor is call
Director.Mission.__init__ (self)
If you do not do this, Vegastrike will not load your module and will silently fail (this calls the C++ constructor, which actually triggers the binding of your module to the python mission).
Each mission module must have an execute function so that something can happen every physics frame. In this case it just calls execute on all the submodules... not very interesting.
Missions also may have 2 functions:
- Pickle (self) which returns a string with a XML mission file that will load the mission properly as well as a string representing the mission in serialized form... if the mission were reloaded that mission would be loaded and its corresponding python would be run.
- Then Unpickle (self, str) would be called (with str being a string) and the expectation that the mission would be reloaded. modules/newmission.py has an example of this. FIXME LINK IS WRONG!! - OLD DATA MODULE REFERENCE
You may also choose to save data in a float by float format as follows (the advantage to this format is that it is saved on a PER PLAYER basis... so that each player may have a different value):
There are 4 functions at your disposal for this older way to save missions:
This function adds the key for later retrieval (even after VS quits ) and pushes the float to the end of the hashtable indexed by stringkey:
myindex=Director.pushSaveData(int whichplayer, stringkey,floatVal);
This function modifies a index in a key that has already been pushed back at one point:
Director.putSaveData (intWhichPlayer, stringkey, myIndex, floatVal);
This function gets the number of float values for a given player:
Director.getSaveDataLength (int whichplayer, string key)
This function gets save data saved at that number:
Director.getSaveData(int whichplayer, stringkey, int num)
You can make up stringkeys and save only 1 float per key if you wish... each key is allowed to have an vector of possible values. Use as you wish! For the privateer mission I save things with key 31337ness and the one value in there is the difficulty that I write out.... If a campaign is more global rather than player specific you may just choose to pickle it...however if something is on a per-player basis, it may be best going in these SaveData float vectors.
With the exception of AI modules from here on out it's straight python. I will go over some of the functions you can call inside Vega Strike in a minute.
XML Integration | Editing Missions | AI Scripts |