Our Python files and Event Structure

From WiCWiki

Revision as of 11:55, 12 December 2007 by kowalski (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Contents

Python files

There are two python files that we need to have, server.py and client.py, for the game to start.

Apart from those, you can split up your code in as many python files as you want. A python file is an unformatted text file with the .py extensions that you can create in notepad. The python files must be located at wicFolder\maps\mapName\python\.


When we made the single player campaign, we use two more files, mapvars and allunits. Those are not really necessary to get the game to work but to save global variables you must have a mapvars file. We use the allunits.py for pretty much anything regarding units.


We also use to divide the mission into a number of events in the server.py file. It’s really neat to have all the code in different events that are freestanding, which means we can run and test separate events without playing the whole mission, but that just us you can do it on any way you want.


The Server file

The server.py is the main python file, if you don’t have a server file that imports serverimports the game won’t even start. All the client side call-backs from the game are executed in the client file. All the call-backs are listed in the serverimports python file.


A basic server.py setup.

Let’s take a look at and setup a good basic server file that can be used in all forthcoming missions that you make.


For the game to work we have to import the serverimports file.

from serverimports import *

Then we import the mapvars.py file, so we get access to all the global variables, and the allunits.py file, where we are going to create all the units.

import mapvars, allunits

A function that we have to have is the OnGameStarted(), the game will execute this when the map has finished loading and the player clicks the “Click to continue” button. That’s the function that will start our mission specific code.

def OnGameStarted():

We have a check to see if the map is started in single or multi player, so that we can use the same map in both modes.

if IsSinglePlayer():

We write a little message to the debug file so that we can see that the map starts as it should.

DebugMessage( "OnGameStarted:: Singel" )              

If we don’t want the map to use the multiplayer game mode we have to pause it, it will hide the domination flag and the rest of the multi player specific stuff.

theGame.PauseGameMode( )

By default all players drop zone markers, the green smoke, are visible. It can be fairly annoying if we are using a lot of AI players, so we hide them.

ClientCommand( 'HideAllDropZones' )

But we want the player to see his/her drop zone marker, so we’ll enable that.

EnableDropzone( True )

Sometimes it can be cool to see the enemies drop ships but as default we have it disabled.

ShowAllDropships(False )

When the map starts the screen will be black so we have to fade in to the game.

theGame.FadeIn( 1.0 )

Check if it’s a loaded game.

if IsLoadedGame():

Restore the GUI, reinforcement menu, tactical aid menu and starts the reaction manager after a game has been loaded.

RestoreAfterLoad()

And if it isn’t a loaded game, we want to start the mission from the beginning.

else:

All the GUI chunks, mini map, reinforcement and tactical aid menu, are hidden by default, so we have to specify what chunks we want to be visible.

theGame.ShowGUIChunk( 'reinforcements' )              
theGame.ShowGUIChunk( 'support' )                        
theGame.ShowGUIChunk( 'minimap' )

Now we can start running our mission code.

# Start the mission here.

All the code should look like this.

from serverimports import *
import mapvars, allunits


def OnGameStarted():

    if IsSinglePlayer():
        DebugMessage( "OnGameStarted:: Singel" )                   

        # Disable multiplayer game modes
        theGame.PauseGameMode( )
        
        # Hides all DropZone markers
        ClientCommand( 'HideAllDropZones' )

        # Show the players DropZone marker
        EnableDropzone( True )
 
        # Set if we want to see the enemy’s teams drop ship.
        ShowAllDropships( False )
       
        # Fade in to the game.
        theGame.FadeIn( 1.0 )

        # Check if its a loaded game.
        if IsLoadedGame():
 
            # Restores the GUI and starts the reaction manager after a game has been loaded. 
            RestoreAfterLoad()
 
        else:
            # All the GUI chunks, minimap, reinforcement and ta meny, are hidden by default, so we #have to specify what chunks we want to be visible.
            theGame.ShowGUIChunk( 'reinforcements' )              
            theGame.ShowGUIChunk( 'support' )                        
            theGame.ShowGUIChunk( 'minimap' )
                                   
            # Start the mission.
             
    return 1

Adding the events.

Now that we have a good basic server file, we start to add some mission specific code.


Like I said before we split our missions into events and every event about a primary objective usually have three standard functions, EventNameSetup, EventNameStart and EventNameEnd. If the objective has an objective camera we have an EventNameCam to.

Event concerning secondary objectives often only has the setup and start function.


‘EventNameSetup’ function.

In the setup function we usually create the objective, a timer object if the objective use a timer, and setup how the event should be started. The setup has an argument, aKickStart, which controls if the event should be kick started. A kick start is if you want to start an event separately instead of start the map from the beginning. It’s really handy and can save you lots of valuable time. Sometimes it requires some extra code to make it work but it’s totally worth it.


‘EventNameCam’ function.

The ‘EventNameCam’ function is optional, but if you have an objective camera and use this function it can be nice to activate command points, start the objective camera, show message boxes and write code that will setup what should happen if the player escapes the camera, in it.



‘EventNameStart’ function.

The rest of the events game code goes here, add the objective, the complete and fail conditions and so on.



‘EventNameEnd’ function.

Here we can have some clean up code and post that the event is done.


The map in this tutorial has following event, Village, Mansion, LastStand, DestroyAA, RescueArty and DestroyHideouts.


So let’s see how the fundamental code of the event looks like, it’s just the four functions and some debug messages.


##---------------------------------------------------------------
#---------------------------  Village ---------------------------       
 

def VillageSetup( aKickStart = False ):      
    DebugMessage( "VillageSetup::" )

    if aKickStart:
        DebugMessage( "VillageSetup::KickStart" )
 

def VillageCam( ):
    DebugMessage( "VillageCam::" )
 

def VillageStart( ):
    DebugMessage( "VillageStart::" )


def VillageEnd():
    DebugMessage( 'VillageEnd::' )

    PostEvent( 'VillageEnd' )


The other events from our test map look almost the same except that the LastStand event doesn’t have any objective camera so it doesn’t need the LastStandCam function. DestoyAA, RescueArty and DestoyHideouts are all secondary objectives and have the setup and start functions only. So now it’s just for you to add the reaming events to the server file or use the one we made.


Before we are done with the events we have to make sure that the event setups are executed when the map starts. So we need to define a new function we usually call Intro. Place it under OnGameStarted, which will run all the event setups. During this tutorial we will add more code to the Intro function.


def Intro( ):

    # Setup all Events.
    VillageSetup( )
    MansionSetup(  )
    LastStandSetup( )
    DestroyAASetup(  )
    RescueArtySetup( )
    DestroyHideoutsSetup(  )

And then we add the Intro function to OnGameStarted, after the start the mission comment, so it will look like this.


# Check if its a loaded game.
if IsLoadedGame():

    # Restores the GUI and starts the reaction manager after a game has been loaded. 
    RestoreAfterLoad()

else:

    # All the GUI chunks, minimap, reinforcement and ta meny, are hidden by default, so we have to
    # specify what chunks we want to be visible.
    theGame.ShowGUIChunk( 'reinforcements' )              
    theGame.ShowGUIChunk( 'support' )                        
    theGame.ShowGUIChunk( 'minimap' )

    # Start the mission.
    Intro( )


If you have done everything that you should, the basic event structure of the tutorial map is done.

The Client file.

The client.py is the main python file, if you don’t have a client that imports the clientimports file the game won’t even start. All the client side call-backs from the game are executed in the client file. All the call-backs are listed in the clientimports python file.

The main thing we use the client file to is run the objective cameras.


The only thing that needs to be in client file is clientimports.

from clientimports import *


The Allunits file.

The allunits.py file is not necessary to have but we’ve put all the unit creation in that file, this to keep the server files smaller and less cluttered.


To get a working allunits that we can create units in and give them to the AI if we want this is what is needed.


For the game to work we have to import the serverimports file.

from serverimports import *

To be able to use the AI we import the ai.py.

from wicgame.ai import *

Then we import the mapvars so we can use all stored variables.

import mapvars

The allunits file should look like this. Now you can add your code.

from serverimports import *
from wicgame.ai import * 
import mapvars            


The Mapvars file.

Mapvars should contain all the global variables used in the map. The main reason for this is so that they get saved in the save file which is very important if you want everything to work when you load the game. It will also make them easier to access.

There should not be any import to the mapvars file as we only will store variable in it.


To use the mapvars file it has to be imported in every file where you will use mapvars variables.

import mapvars

The variables in the mapvars.py file.

The declaration of the variable is very simple. Almost every variable is declared with a ‘None’ as value like this.

grpPlayer1 = None

There are some exceptions for example if we want a timer to have a value from the beginning like this timer from the test map.

TIME_MANSION = 120

Then to access a variable in the mapvars file you do like this.

mapvars. grpPlayer1


The server_autogen file.

Do not edit this file. The Server_autogen file is auto generated each time the map is saved in WICEd. It contains functions to create command points in multi player and for save/load.

Image:Extra_Chapter1_Python_001.jpg


Chapter 19: The first whole event < > Extra Chapter 2: Reactions the long version
Personal tools
User Created Content