Need Help - Adjusting a clone's speed
by Nicolai Dutka · in Torque Game Builder · 03/31/2008 (11:58 am) · 2 replies
I have a template "enemy" object and a "spawn" object which spawns the template enemy. The template creature has a behavior attached which tells it how to move (including at what speed). Everything works great.
Now, I am wanting to dynamically change the CLONED object's speed as depicted by its attached behavior. I tried passing a variable to the spawn function for the spawner and got mixed results.
I have no problems passing the speed variable to the spawn function.
If the spawned clone gets attached to a path, I can easily use the variable I passed in to set the enemy's speed on its path. This works no problem.
If the spawned clone is NOT attached to a path, the speed variable I gave it does NOTHING!
So here is the code for spawning a creature:
You can see at the end of the code, I check to see if the object has a patrolPath, and if so, attach the object to its assigned path at %speed. I seperated out the lines I have to set the clone's speed and they have been commented out because they were ineffective.
I also tried using: %clone.minSpeed = %speed;
No luck there either...
Any ideas? Keeping in mind it has to be done on spawn. I am not trying to adjust their speeds after they are created.
Now, I am wanting to dynamically change the CLONED object's speed as depicted by its attached behavior. I tried passing a variable to the spawn function for the spawner and got mixed results.
I have no problems passing the speed variable to the spawn function.
If the spawned clone gets attached to a path, I can easily use the variable I passed in to set the enemy's speed on its path. This works no problem.
If the spawned clone is NOT attached to a path, the speed variable I gave it does NOTHING!
So here is the code for spawning a creature:
function SpawnAreaBehavior::spawn(%this,%speed)
{
if (!isObject(%this.object))
return;
%clone = %this.object.cloneWithBehaviors();
%xPos = 0;
%yPos = 0;
%spawnLocation = %this.spawnLocation;
%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;
////////////////////////////////////////////////////
//Speed Code
//
//%clone.setMinLinearVelocity(%speed);
//%clone.setMaxLinearVelocity(%speed);
//
///////////////////////////////////////////////////
%this.spawnCount++;
if (%this.spawnCount < %this.count)
{
%minTime = (%this.spawnTime - %this.spawnVariance) * 1000;
%maxTime = (%this.spawnTime + %this.spawnVariance) * 1000;
%spawnTime = getRandom(%minTime, %maxTime);
%this.schedule(%spawnTime, "spawn");
}
if(isObject(%this.patrolPath))
{
echo("Trying to get patrol path: "@ %this.patrolpath);
%this.patrolPath.attachObject(%clone, %speed, 0.0, 9, 10,"WRAP", 2, true);
}
}You can see at the end of the code, I check to see if the object has a patrolPath, and if so, attach the object to its assigned path at %speed. I seperated out the lines I have to set the clone's speed and they have been commented out because they were ineffective.
I also tried using: %clone.minSpeed = %speed;
No luck there either...
Any ideas? Keeping in mind it has to be done on spawn. I am not trying to adjust their speeds after they are created.
#2
2. THANKS!! I'll give that a try ASAP.
03/31/2008 (7:42 pm)
1. Yes and no. I had that highlighted for your viewing pleasure AND i have it commented out in my code cuz it wasn't doing anything. (As you said would happen since it was more of a 'setting' than a 'command')2. THANKS!! I'll give that a try ASAP.
Torque Owner Kevin James
Are you highlighting this for our viewing pleasure, or have you actually commented this out in your code? Either way, those functions do not set any actual, tangible speed on an object -- they only state the min and max speed that the object can travel at.
%speed only modifies movement when an object is attached to a path because that is the only place in which you use %speed in a tangible way.
You could apply some linear velocities: