Issue Switching Player Datablock
by Nicolai Dutka · in Torque Game Engine Advanced · 07/27/2008 (4:08 pm) · 1 replies
I have a trigger zone with an turret (object) in it. When the player enters the zone, they are able to push the 'useobject' button on the joystick which does the following:
This effectively removes the turret from the level and makes the player become the turret. This all works great!
Here is where I run into the problem. I am trying to switch back to the original player. Here is the code:
As you can see in the TPControls() function, I am trying to stop the player from jumping and rotating. The problem I have is that half the time I use joystick button2 while in first person mode to switch back to the default player and third person mode, the player will start jumping uncontrollably by themselves. Very occasionally, he will also rotate uncontrollably, but slowly.
After messing around with it a LOT, I have concluded that I cannot get the player under proper control once I switch back to the original player.
I've pretty much got this solved. Here is my solution:
function useTurret()
{
$player.setVelocity("0 0 0");
%d = getWord(%t,6);
$player.lastXform=$player.gettransform();
$turret = $player.useObject;
$turretPos = $turret.getTransform();
$turretName = $turret.getName();
$turret.delete();
$player.setDatablock(turretObject);
$player.setTransform($turretPos);
toggleFirstPerson(1);
fpControls();
arm($player,"Turret");
}
function FPControls()
{
$controls = "fp";
moveMap.bind( keyboard, a, turnleft );
moveMap.bind( keyboard, d, turnright );
moveMap.bind( keyboard, w, panup );
moveMap.bind( keyboard, s, pandown );
moveMap.bind( keyboard, q, "" );
moveMap.bind( keyboard, e, "" );
moveMap.bindCmd( keyboard, space, "exitfp();", "" );
moveMap.bind( joystick, button6, turnLeft );
moveMap.bind( joystick, button7, turnRight );
moveMap.bind( joystick, button3, mouseFire );
moveMap.bindCmd( joystick, button2, "exitfp();", "" );
moveMap.bind( joystick, xaxis, "D", "-0.23 0.23", gamePadYaw );
moveMap.bind( joystick, yaxis, "D", "-0.23 0.23", gamePadPitch );
}This effectively removes the turret from the level and makes the player become the turret. This all works great!
Here is where I run into the problem. I am trying to switch back to the original player. Here is the code:
function exitfp()
{
new AIPlayer($turretName) {
canSaveDynamicFields = "1";
Enabled = "1";
position = $turretPos;
rotation = "0 0 1 175.325";
scale = "1 1 1";
dataBlock = "turretObject";
mountVehicle = "1";
};
$player.setVelocity("0 0 0");
$player.setTransform($player.lastXform);
$player.setDatablock(defaultPlayerData);
toggleFirstPerson(1);
schedule(200,0,tpcontrols);
arm($player,"weap");
}
function TPControls()
{
$mvTriggerCount2 = 0;
$mvYawRightSpeed = 0;
$mvYawLeftSpeed = 0;
$controls = "tp";
moveMap.bind( keyboard, a, moveleft );
moveMap.bind( keyboard, d, moveright );
moveMap.bind( keyboard, w, moveforward );
moveMap.bind( keyboard, s, movebackward );
moveMap.bind( keyboard, q, turnLeft );
moveMap.bind( keyboard, e, turnRight );
moveMap.bind( keyboard, space, jump );
moveMap.bind( joystick, button6, turnLeft );
moveMap.bind( joystick, button7, turnRight );
moveMap.bind( joystick, button3, mouseFire );
moveMap.bind( joystick, button2, jump );
moveMap.bindCmd( joystick, button1, "useObject();", "" );
moveMap.bindCmd( joystick, upov, "makeCrate(1);", "" );
moveMap.bindCmd( joystick, rpov, "makeCrate(2);", "" );
moveMap.bindCmd( joystick, dpov, "makeCrate(3);", "" );
moveMap.bindCmd( joystick, lpov, "testBot();", "" );
moveMap.bindCmd( joystick, button4, "makeBarrel(1);", "" );
moveMap.bindCmd( joystick, button5, "makeBarrel(2);", "" );
moveMap.bindCmd( joystick, button11, "makeBarrel(3);", "" );
moveMap.bind( joystick, xaxis, "D", "-0.23 0.23", gamePadMoveX );
moveMap.bind( joystick, yaxis, "D", "-0.23 0.23", gamePadMoveY );
}As you can see in the TPControls() function, I am trying to stop the player from jumping and rotating. The problem I have is that half the time I use joystick button2 while in first person mode to switch back to the default player and third person mode, the player will start jumping uncontrollably by themselves. Very occasionally, he will also rotate uncontrollably, but slowly.
After messing around with it a LOT, I have concluded that I cannot get the player under proper control once I switch back to the original player.
I've pretty much got this solved. Here is my solution:
function useObject()
{
if($player.useAction$="opening")
{
echo("run openDoor on: "@ $player.useObject);
openDoor($player.useObject);
return;
}
if($player.useAction$="turret")
{
echo("player is trying to use a turret");
if($player.getDatablock().getName()$="turretObject")
{
exitTurret();
}
else
{
useTurret();
}
//mount player to $player.useobject, toggle view, activate 1st person controls
}
echo("player is not in a 'useObject' type trigger");
}
function useTurret()
{
$player.setVelocity("0 0 0");
%d = getWord(%t,6);
$player.lastXform=$player.gettransform();
$turret = $player.useObject;
$turretPos = $turret.getTransform();
$turretName = $turret.getName();
$turret.delete();
$player.setDatablock(turretObject);
$player.setTransform($turretPos);
toggleFirstPerson(1);
fpControls();
arm($player,"Turret");
}
function exitTurret()
{
new AIPlayer($turretName) {
canSaveDynamicFields = "1";
Enabled = "1";
position = $turretPos;
rotation = "0 0 1 175.325";
scale = "1 1 1";
dataBlock = "turretObject";
mountVehicle = "1";
};
statics.add($turretName);
$player.setVelocity("0 0 0");
arm($player,"weap");
$player.setDatablock(defaultPlayerData);
$player.setTransform($player.lastXform);
toggleFirstPerson(1);
tpcontrols();
}
Torque 3D Owner Nicolai Dutka
When you use the turret, then exit, the player's pitch gets stuck at what it was when using the turret. I don't understand... before switching datablocks to turret form, I mark the player's transform using $player.lastXform = $player.getTransform and when done, I set it back using $player.setTransform($player.lastXform).
If I use the turret, look up in the air, and then exit the turret, my player gets stuck shooting his bullets up in the air. I echo out the player's transform and it is EXACTLY what I set it to using $player.lastXform, but yet the bullets are flying up into the air!
This may also be related to this issue, and I will need a solution to both: http://garagegames.com/mg/forums/result.thread.php?qt=77528