Strange AIPlayer Function Problem
by Jonathan Noyola · in Torque Game Engine · 03/19/2010 (4:57 pm) · 0 replies
Basically I'm working on a fancy 3d character selection, which uses an aiplayer for each choice. The mission begins with the aiplayers in a circle around the camera. When you hit a key, the aiplayers rotate around the camera, either left or right.
My code:
serverside:
clientside:
I have tested the code, and all of the functions seem to be called at the right times, but it's still not working right.
I'm having two problems, one of which I think might be caused by the other:
1) When the mission first loads, and I hit the first key, the $NewGame::selectedCharacterIndex changes, but the aiplayers don't move (well, they actually move to the spots they're already in).
2) When I switch the direction that the aiplayers are rotating in, the numbers change correctly, but the players move in the wrong direction.
Feel free to run this code on your machine to see if you get the same results (however you would have to change the game type, which is currently "NG" (for NewGame)). Any help would be greatly appreciated.
- Jon
My code:
serverside:
function NGGame::setUpMission(%game, %client)
{
%cam = %client.getControlObject();
%xCam = getWord(%cam.getTransform(), 0);
%yCam = getWord(%cam.getTransform(), 1);
%zCam = getWord(%cam.getTransform(), 2);
$NewGame::playerIDs = "";
$NewGame::xPos = "";
$NewGame::yPos = "";
for (%i = 0; %i < $NewGame::NumOfChoices; %i++)
{
//eval("%p" @ %i @ " = new AIPlayer() { dataBlock = " @ getWord($NewGame::DataBlocks, %i) @ "; };");
%player = new AIPlayer() {
dataBlock = getWord($NewGame::DataBlocks, %i);
scale = "2.3 2.3 2.3";
index = %i;
};
MissionCleanup.add(%player);
$NewGame::playerIDs = $NewGame::playerIDs @ %player @ " ";
$NewGame::xPos = $NewGame::xPos @ (%xCam - ($NewGame::distFromCharacter * mCos(((2 * 3.1415) / $NewGame::NumOfChoices) * %i))) @ " ";
$NewGame::yPos = $NewGame::yPos @ (%yCam + ($NewGame::distFromCharacter * mSin(((2 * 3.1415) / $NewGame::NumOfChoices) * %i))) @ " ";
%player.setTransform(getWord($NewGame::xPos, %i) SPC getWord($NewGame::yPos, %i) SPC %zCam SPC "1 0 0 0");
%player.setAimObject(%cam);
}
}
function serverCmdNGGameMoveLeft(%client)
{
echo("left");
for (%i = 0; %i < $NewGame::NumOfChoices; %i++)
{
%j = ($NewGame::NumOfChoices - $NewGame::selectedCharacterIndex) + %i + 1;
if (%j >= $NewGame::NumOfChoices)
{
%j -= $NewGame::NumOfChoices;
}
// Needed again
if (%j >= $NewGame::NumOfChoices)
{
%j -= $NewGame::NumOfChoices;
}
echo(%i SPC %j);
eval(getWord($NewGame::playerIDs, %i) @ ".setMoveDestination("" @ getWord($NewGame::xPos, %j) SPC getWord($NewGame::yPos, %j) SPC getWord(%client.getControlObject(), 2) @ "", false);");
}
}
function serverCmdNGGameMoveRight(%client)
{
echo("right");
for (%i = 0; %i < $NewGame::NumOfChoices; %i++)
{
%j = ($NewGame::NumOfChoices - $NewGame::selectedCharacterIndex) + %i - 1;
if (%j < 0)
{
%j += $NewGame::NumOfChoices;
}
if (%j >= $NewGame::NumOfChoices)
{
%j -= $NewGame::NumOfChoices;
}
echo(%i SPC %j);
eval(getWord($NewGame::playerIDs, %i) @ ".setMoveDestination("" @ getWord($NewGame::xPos, %j) SPC getWord($NewGame::yPos, %j) SPC getWord(%client.getControlObject(), 2) @ "", false);");
}
}clientside:
$NewGame::NumOfChoices = 6;
$NewGame::DataBlocks = "SoldierMaleArmor SoldierMaleArmor SoldierMaleArmor SoldierMaleArmor SoldierMaleArmor SoldierMaleArmor";
$NewGame::selectedCharacterIndex = 0; // Don't change
$NewGame::distFromCharacter = 7;
// Begins new game and launches character selection
function startNewGame()
{
loadNewGameMission();
}
function loadNewGameMission()
{
tge.buildServer("SinglePlayer", "newGame", "NG");
%conn = new GameConnection(ServerConnection);
RootGroup.add(ServerConnection);
%conn.setConnectArgs(getField($pref::Player, 0), getField($pref::Player, 1));
%conn.setJoinPassword($Client::Password);
%conn.connectLocal();
}function newGameCmd(%function)
{
switch$ (%function)
{
case "left":
$NewGame::selectedCharacterIndex++;
case "right":
$NewGame::selectedCharacterIndex--;
}
if ($NewGame::selectedCharacterIndex < 0)
$NewGame::selectedCharacterIndex += $NewGame::NumOfChoices;
if ($NewGame::selectedCharacterIndex >= $NewGame::NumOfChoices)
$NewGame::selectedCharacterIndex -= $NewGame::NumOfChoices;
echo($NewGame::selectedCharacterIndex);
switch$ (%function)
{
case "left":
commandToServer('NGGameMoveLeft');
case "right":
commandToServer('NGGameMoveRight');
}
}
if(isObject(newGameMap))
newGameMap.delete();
new ActionMap(newGameMap);
newGameMap.bindCmd(keyboard, "escape", "", "escapeFromGame();");
newGameMap.bindCmd(keyboard, a, "newGameCmd(\"left\");", "");
newGameMap.bindCmd(keyboard, "left", "newGameCmd(\"left\");", "");
newGameMap.bindCmd(keyboard, d, "", "newGameCmd(\"right\");");
newGameMap.bindCmd(keyboard, "right", "newGameCmd(\"right\");", "");I have tested the code, and all of the functions seem to be called at the right times, but it's still not working right.
I'm having two problems, one of which I think might be caused by the other:
1) When the mission first loads, and I hit the first key, the $NewGame::selectedCharacterIndex changes, but the aiplayers don't move (well, they actually move to the spots they're already in).
2) When I switch the direction that the aiplayers are rotating in, the numbers change correctly, but the players move in the wrong direction.
Feel free to run this code on your machine to see if you get the same results (however you would have to change the game type, which is currently "NG" (for NewGame)). Any help would be greatly appreciated.
- Jon
About the author
I've been programming for 10 years now. Proficient in many programming languages. Stanford '16: BS CS, BS Math, MS CS (Artificial Intelligence). Favorite games: GW2, LoL, Borderlands, NWN2