Game Development Community

Quick question on adding behaviors

by Law Rus · in Torque Game Builder · 01/23/2008 (1:10 pm) · 1 replies

I was follow the asteroids tutorial for the score board, but I'm having trouble working out how to a behavior through script.

I can add the behavior at design time and roughly understand how it all works, however, I want to add it to any enemies that are spawned in through code.

Here is the the behavior code

if (!isObject(ScorePointsBehavior))
{
%template = new BehaviorTemplate(ScorePointsBehavior);

%template.friendlyName = "Score Points";
%template.behaviorType = "Game";
%template.description = "Gives the object a point value for scoring purposes";

%template.addBehaviorField(pointValue, "How much the object is worth", int, 10);
%template.addBehaviorField(counter, "The score counter object", object, "", t2dSceneObject);
}

function ScorePointsBehavior::onRemoveFromScene(%this, %scenegraph)
{
%counter = %this.counter.getBehavior("DisplayScoreBehavior");
if (!isObject(%counter))
return;

$currentScore = $currentScore + %this.pointValue;
%counter.updateScore();
}

so would I call 'ScorePointsBehavoir'? I read through the rest of the tutorial but I can't seem to find it anywhere.

#1
01/23/2008 (3:05 pm)
In the first section of the tutorial, when you are setting the scene up you are basically creating a "master" copy of your enemy (or in the case of the tutorial - the asteroids) with all the behaviors set up. Then through the spawn area behavior the following code is used to create copies of that master enemy:

%clone = %this.spawnObject.cloneWithBehaviors();

where %this.spawnObject is defined in the behavior field of the spawn area behavior. Hope that helps.