spawning objects on a path
by Tom Biagioni · in Torque Game Builder · 02/18/2009 (7:54 am) · 4 replies
I just finished the Builder tutorial to get me back up to speed and have started adding my own flavor to it, the problem is this:
I've created a spawn point for the enemy just off the screen, but i can't figure out how to get the spawned object to be attached to a path...I've looked through the pathGrid2d methods but just can't see the way.
Any idea?
I've created a spawn point for the enemy just off the screen, but i can't figure out how to get the spawned object to be attached to a path...I've looked through the pathGrid2d methods but just can't see the way.
Any idea?
#2
02/18/2009 (9:49 am)
I tried editing the spawn area behavior to add this functionality to it using the attach object method, but it isn't working... I feel like its a really obvious thing that i'm just missing--as it always seems to be when I'm coding... the function in question is at the bottom, but i posted the entire behavior just in case i missed it somewhere elseif (!isObject(SpawnAreaAdvancedBehavior))
{
%template = new BehaviorTemplate(SpawnAreaAdvancedBehavior);
%template.friendlyName = "Spawn Area Advanced";
%template.behaviorType = "AI";
%template.description = "Spawns objects inside the area of this object";
%template.addBehaviorField(object, "The object to clone", object, "", t2dSceneObject);
%template.addBehaviorField(path, "Path to set enemy on", object, "", t2dSceneObject);
%template.addBehaviorField(count, "The number of objects to clone (-1 for infinite)", int, 50);
%template.addBehaviorField(spawnTime, "The time between spawns (seconds)", float, 2.0);
%template.addBehaviorField(spawnVariance, "The variance in the spawn time (seconds)", float, 1.0);
%template.addBehaviorField(spawnStart, "Time before the first spawn (seconds)", float, 2.0);
%spawnLocations = "Area" TAB "Edges" TAB "Center" TAB "Top" TAB "Bottom" TAB "Left" TAB "Right";
%template.addBehaviorField(spawnLocation, "The area in which objects can be spawned", enum, "Area", %spawnLocations);
%template.addBehaviorField(minSpeed, "The minimum speed of the object (world units per second)", float, 10.0);
%template.addBehaviorField(maxSpeed, "The maximum speed of the object (world units per second)", float, 25.0);
}
function SpawnAreaAdvancedBehavior::onAddToScene(%this, %scenegraph)
{
%this.spawnCount = 0;
%this.schedule(%this.spawnStart * 1000, "spawn");
%this.owner.setLinearVelocityPolar(270, %speed * 0.001);
}
function SpawnAreaAdvancedBehavior::spawn(%this)
{
if (!isObject(%this.object) || !%this.owner.enabled)
return;
$speed = %speed;
%clone = %this.object.cloneWithBehaviors();
%xPos = 0;
%yPos = 0;
%spawnLocation = %this.spawnLocation;
%this.spawnOnPath();
%speed = getRandom(%this.minSpeed * 1000, %this.maxSpeed * 1000);
%edges = "Top" TAB "Bottom" TAB "Left" TAB "Right";
if (%spawnLocation $= "Edges")
%spawnLocation = getField(%edges, getRandom(0, 3));
switch$ (%spawnLocation)
{
case "Area":
%xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
%yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
case "Center":
%xPos = %this.owner.position.x;
%yPos = %this.owner.position.y;
case "Top":
%xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
%yPos = getWord(%this.owner.getAreaMin(), 1);
case "Bottom":
%xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
%yPos = getWord(%this.owner.getAreaMax(), 1);
case "Left":
%xPos = getWord(%this.owner.getAreaMin(), 0);
%yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
case "Right":
%xPos = getWord(%this.owner.getAreaMax(), 0);
%yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
}
%clone.position = %xPos SPC %yPos;
%this.spawnCount++;
if (%this.spawnCount < %this.count || %this.count == -1)
{
%minTime = (%this.spawnTime - %this.spawnVariance) * 1000;
%maxTime = (%this.spawnTime + %this.spawnVariance) * 1000;
%spawnTime = getRandom(%minTime, %maxTime);
if( %spawnTime < 55 )
%spawnTime = 55;
%this.schedule(%spawnTime, "spawn");
}
}
function SpawnAreaAdvancedBehavior::spawnOnPath(%this)
{
%path = %this.path;
%path.attachObject(%this, $speed, 1, 0, 0, WRAP,0, false);
}
#3
http://www.garagegames.com/community/forums/viewthread/73622
02/18/2009 (11:54 am)
maybe this iwll workhttp://www.garagegames.com/community/forums/viewthread/73622
#4
02/18/2009 (4:50 pm)
I've been trying to use the Attach Path Behavior available on the TDN, but no go...
Torque Owner Shaderman