Game Development Community

Different objects...same behavior.

by Thomas Stankevich · in Torque Game Builder · 04/27/2008 (8:08 pm) · 2 replies

I seem to be having a problem with assigning the same behavior to different objects. I'm making a behavior to teleport the player from one object to another by pressing the space bar. Here's what I have so far:



$teleX = 0;
$teleY = 0;
$teleNow = 0;



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

%template.friendlyName = "Teleport";
%template.behaviorType = "Gameplay";
%template.description = "Teleports to a defined object.";


%template.addBehaviorField(object, "The object to teleport to.", object, "", t2dSceneObject);
%template.addBehaviorField(teleportKey, "The key that allows player to teleport", keybind, "keyboard space");

}


function Teleport::onBehaviorAdd(%this)
{
moveMap.bindObj(getWord(%this.teleportKey, 0), getWord(%this.teleportKey, 1), "teleporting", %this);
}

function Teleport::onAddToScene(%this)
{
%this.owner.enableUpdateCallback();
}


function Teleport::onUpdate(%this)
{
$teleX = %this.object.getPositionX();
$teleY = %this.object.getPositionY();
}


function Teleport::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{

if($teleNow == 1)
{
%dstObj.setPositionX($teleX);
%dstObj.setPositionY($teleY);
}

}

function Teleport::teleporting(%this, %val)
{
$teleNow = %val;
}







The problem I'm coming up with is that the object the player gets teleported to is whatever i defined as the last place to teleport. When I have a teleport from A to B and from B to A the player will always teleport to A.
Any suggestions?

#1
04/28/2008 (2:17 am)
As you are using global variables they are being altered by every object with this behaviour. So while your first behaviour is setting $teleX and $teleY to somthing the second behaviour is overwriting them. I'm not actually quite sure how this behaviour works.... Pressing space bar causes Teleport::teleporting, but the object only teleports onCollision? Heres another way of doing it not using global variables:

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


   %template.friendlyName = "Teleport";
   %template.behaviorType = "Gameplay";
   %template.description = "Teleports to a defined object.";


   %template.addBehaviorField(object, "The object to teleport to.", object, "", t2dSceneObject);
   %template.addBehaviorField(teleportKey, "The key that allows player to teleport", keybind, "keyboard space"); 

}


function Teleport::onBehaviorAdd(%this)
{
   moveMap.bindObj(getWord(%this.teleportKey, 0), getWord(%this.teleportKey, 1), "teleporting", %this);
}

function Teleport::teleporting(%this, %val)
{
   if(isObject(%this.object)
   {
      %pos = %this.object.getPosition();
      %this.owner.setPosition(%pos);
   }
   else
      error("No object to teleport to!");
}

As this doesn't use global variables one behaviour wont effect all the others. Now when you press space the object with the behaviour should teleport. I'm not sure if this is the required behaviour, considering you are using onCollision in your code.
#2
04/28/2008 (6:06 pm)
Thanks for the response. I realized at work today that i was using a global variable. The reason it's teleporting on a collision is that the player uses a hole to go down. So they can only teleport when they are on top of the hole.