The Objective Class

From WiCWiki

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


About the Objective class

The objective class was created to make it easier to work with objectives. The class will take care of things like if the objective have been completed or failed, the counter if the objective have one etc, you don’t need to have a bunch of variables to store everything which is really nice.

There are two types of objectives, primary and secondary. The difference between the two types is that you add the objective in different place in the juice file, the code for creating the objective looks a little bit different and in the game the markers will have a different look and behavior. However, code wise they are the same after we have created the objective.

An objective can have none, one or several objective markers. The marker can be place on a position or follow a unit.

Create an objective

First we need to save the objective in a variable somewhere in the file so we have a global reference to the objective. It should look something like this.

objTest = None

We must have an objective in the mapname_singleplayerdata.juice file as well. You’ll find how to do this in the chapter about objectives and the objectives browser.

To create an objective we do like this, preferable in the file in for example the EventNameSetup function.

mapvars.objTest = Objective( ‘Village’, ‘CP_Village’, ‘primary’ )

- The first parameter is the name you gave the objective in the juice file.

- The second parameter is if we want the objective to have an objective marker, if set to none there won’t be any marker. In this example

we use a position where we want the marker to be. If you want the marker to follow a unit, send in the id of the unit. An objective can also have sub markers, but more about that later.

- The third parameter is if the objective should be a primary or secondary.

There is a fourth parameter you can use. It controls if the objective should have a counter or not. If you don’t want a counter, you don’t have to send in anything, the default value is -1 which means no counter. If you want a counter send in the total value of the counter. More about counter objectives later in this chapter or in the chapter about secondary objectives.

To make the objective active and visible in game we have to add it like this, preferably in the file for example in the EventNameStart function.


Or if you use an action queue it looks like this.

queMyQue.AddObjective( mapvars.objTest )

Update an objective

An objective can be updated from one objective in the mapname_singleplayerdata.juice to another. The objective text is the only thing that will be changed; the marker and a possible counter will stay the same. It can be nice to update an objective for example if the player first should take a command point and then hold it secured for a couple of attack waves.

You cannot change the objective type, secondary to primary or from a counter to non-counter objective.

You update an objective like this.

mapvars.objTest.Update( ‘objTest2’ )

The parameter is the name, in the juice file, for the new objective.

In an action queue it looks like this.

queMyque.AddObjectiveUpdate( mapvars.objTest, 'objTest2' )

- The first parameter is the ‘old’ objective, the one you want to update.

- The second parameter is the new objectives name, the one you gave it in the mapname_singleplayerdata.juice file.

You don’t add the updated part of the objective as a new one in python like you do with the starting ‘main’ one. When you trigger the objective to fail or complete it is the ‘main’ objective you use, in this example the ‘objTest’ not the ‘objTest2’. It will look like this.

AddObjectiveCompleted( mapvars.objTest)

Complete, fail and test the state of an objective

You can’t complete or fail an objective that’s not added to the game nor if they are not active. An objective can’t be active if it hasn’t been added to the game and won’t be active if it’s completed or failed, this to prevent failing an objective that already have been completed or vice versa.

There are one method to fail the objective and one for complete it and they look like this.


Or, if you use the action queue it looks like this.

queMyque.AddObjectiveFailed( mapvars.objTest )
queMyque.AddObjectiveCompleted( mapvars.objTest )

The only parameter is the objective you want to fail or complete.

There are three methods, which return a Boolean, to test if the objective is in a certain state.


To get the state the objectives are in we use the GetState method. This will return a string with the state. There are four states the method can return and they are: 'created', 'active', 'completed' and 'failed'.


The code will look like this:

if mapvars.objTest.GetState() == 'active':
    [The code that will be executed if the test is true will go here]


A counter can be nice to add to an objective if the objective contains more than one task, for example if there is a bunch of Anti Air units that must be killed before the objective counts as completed. The counter always starts on zero and can be increased or decreased.

Each time the counter changes, it will post an event. These events will be posted so you can use them to trigger things. The objective will not be complete automatically when it reaches the decided counter limit, but it will post a special event you can use to trigger when the objective is completed.

Here are two examples on how you can use the posted events to trigger things.

mapvars.objTest = Objective( 'Test', None, 'secondary', 3 ) 
RE_OnCustomEvent( 'Test _2', Action( DebugMessage, 'The counter has reach 2 ')  )
RE_OnCustomEvent( 'TestCompleted', Action( mapvars.objTest.Completed )  )

In the first example we first add an objective which is a secondary objective with a counter assigned to 3. The second line is an action that will print a debug message that will be triggered when the objectives counter reaches 2.

- The first parameter in RE_OnCustomEvent is the string that will be posted. It will always start with the objectives name from the mapname_singleplayerdata.juice file.

- The second parameter in RE_OnCustomEvent is an action or a list of actions that will be executed.

When we want to change the counter, increase or decrease it, we use the method AddToCounter like this.

mapvars.objTest.AddToCounter( 1 )

The value you send in is how much you want to change the counter with; the value can be either positive or negative.

In our test map, europeTest in the secondary objective, we don’t use this. The main reason for that is that we remove the objectives sub marker each time an enemy dies. We use the method

RemoveSubMarker( aSubName, aTarget, aAutoCounter = False, aUnit = -1, aPlayerId = 1) (which we will cover in the coming paragraph) instead with the flag aAutoCounter set to True. This means it will take care of the counters incensement itself. If you for example have a counter without sub markers it is a better idea to use the AddToCounter( 1 ).

To get the current count on the objective for some reason, do like this.


Sub-objective marker

An objective can have more than one objective marker, so called sub markers. You can move the sub markers to a new position.

It will make your life easier if you don’t add a ‘normal’ marker to the objective if you want to use sub markers. You simply write None as argument instead of a position when you create the objective. You can add sub markers to the objective even if it not is added or active.

To add a sub marker this is the way.

mapvars.objTest.AddSubMarker( aSubName, aTarget, aAutoCounter = False, aUnit = -1, aPlayerId = 1)

- The first parameter must be a unique name, the name will be used as reference when we remove or want to move the position of the marker.

- The second parameter is the position where to put the marker.

- The third controls if the counter should be decreased.

- The fourth parameter is if the marker should follow a unit and it takes a unit id.

- The fifth parameter is the id of the player that should get the marker.

In one example from the EuropeTest test map it looks like this.

mapvars.objDestroyAA.AddSubMarker( 1, 'areaAASpawn1' )

When we want to remove the sub marker, we do like this.

mapvars.objTest.RemoveSubMarker( aSubName, aAutoCounter = False, aPlayerId = 1, isUnitMarker = False )

- The first parameter is the name of the sub marker.

- The second parameter controls if the counter should be increased.

- The third is the id of the player that has the marker.

- The fourth parameter is if the marker follows a unit.

In one example from the EuropeTest test map it looks like this.

mapvars.objDestroyAA.RemoveSubMarker( 2, True )

Extra Chapter 2: Reactions the long version < > Extra Chapter 4: Common mistakes and bugs
Personal tools
User Created Content