Mission scripts

I am not yet at this point with my game, but do think I’ll get there. I just wanted to ask opinions on basic coding strategy to accomplish this:

(remember this is a squad-type RTS game)

Two approaches I’ve thought of so far:

  1. Mission params are in text (or PY) files, but I write some simpler parser code to interpret a limited set of commands that can be used in the scripts.,
    i.e.
    map is jungle_1
    squad_character 1 says “hello”
    squad_character 2 moves to (3,4,5)
    etc.etc.

or

  1. I have seen some commercial games which allow modding by having the user write mission scripts in python (in fact, this is how i first became familiar with python).

The game engine is told where to look for the PY file for a specific mission. The script needs to contain 2 specific defs Start(): and End(): , but everything else in the script can be basic python code. IF you know what object names to code for, you can manipulate anything in the game. Im actually not 100% sure how this is implemented.

any thoughts?

One solution could be to use inheritance to create your level scripts by sub-classing from a main Level class. For instance, create a class ‘Level’ that handles the basic needs that every level will require such as asset loading, start level, end level, playing cut scenes, etc. Then, create a ‘LevelOne’ class that inherits from the ‘Level’ class and handles everything specific for that level. Each level class could have logic for dealing with certain actions, such as a character moving to a specific location and triggering some event…

I’d use python for that.

So in the individual mission scripts, I would just instantiate my Level class?

Is there a way I could add custom defs to that instance of the class?

How would I call/use the mission script from my game class?

Custom defs are easy to add

FirstClass:
    def method1(self):
        print "Hi!"
    def method2(self):
        pass

SecondClass(FirstClass):
    def method1(self):
        print "I override Firstclass method1"
    def method3(self):
        print "I am a new method"

There are a couple of ways to use your script.

One method is to keep the same script name and store them in different folders:
Levels\Level1\level.py
Levels\Level2\level.py

Another method is some sort of conflig/ini file which is responsible for mapping what levels to load and what order to load them in.

Another method is using a campaign script which connects the levels together (same as above but uses code)

For my irc bot I use the first method described for loading plugins. Here is the code for that if you find it useful. I edited out some extraneous code so forgive me if it breaks. List of plugins to load is stored in a list and is is the folder name of that plugin.

import imp
import glob
    def loadAllPlugins(self):
        # Unloading anythign we have
        self.plugins = {}
        self.aliases ={}
        for plugin_name in self.config['plugins']:
            self.loadPlugin(plugin_name):
    
    def loadPlugin(self, plugin_name):        
        try:
            filepath, pathname, description = imp.find_module(plugin_name, plugins.__path__)
            moduleSource = pathname +'/plugin.py'
            moduleSource = moduleSource.replace ( '\\', '/' )
            handle = open ( moduleSource )
            module = imp.load_module ( plugin_name, handle, ( moduleSource ), ( '.py', 'r', imp.PY_SOURCE ) )
            # This batch of code will be good for loading plugins in the user directory (which will happen after main ones)
            #moduleSource = 'plugins/'+plugin_name+'/plugin.py'
            #name = moduleSource.replace ( '.py', '' ).replace ( '\\', '/' ).split ( '/' ) [ 1 ]
            
            # Populate the main database
            self.plugins[plugin_name.lower()] = module
            
            log.info("Plugin",plugin_name,"loaded.")
            return reboot
        except:
            log.warning("I couldn't load",plugin_name + '. I placed a report in the errlog.txt file for you.')

To run a plugin I use: self.plugins[plugin_name].run_plugin(), which is a function in plugin.py. You will probably want to change this to match what you are doing like:
self.level = self.level[level_name].LevelClass()

Disclaimer This is probably way too complex and not well explained. It is just one way I used, there may be better

croxis, acutally that’s very helpful!

The imp code is what I was looking for. Going to run some tests now, but I think this would do what I need to.

Thanks!

whoops, my code takes were broken, should be a bit more readable now.