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?
$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?
About the author
#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.
Torque 3D Owner Tom Perry
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.