Game Development Community

Script compatability with older versions

by __._ · in Torque Game Engine · 05/29/2007 (9:36 am) · 9 replies

I have a question related to some tutorials I found on the web. These tutorials use Torque 1.3 (I think) and one of them describes bot pathfinding. Unfortunately, when I exec the same code in the latest version of Torque, the program crashes as soon as it starts to load a mission.

It seems to crash on the datablock itself (the rest of the code is compiled fine):
datablock PlayerData(MyBot:PlayerShape)
{
	// TO DO: Add extra stuff here to manage new A.I. functionality...
	patrol = true;
	attack = false;
};

The tutorials come from:
http://www.codesampler.com/torque.htm

What is wrong with that bit of code?

Thanks in advance

#1
05/29/2007 (9:36 am)
The full code:
//-----------------------------------------------------------------------------
// To create a Bot or A.I. driven player, we'll simply create a new PlayerData
// datablock called "MyBOT" which derives all of its functionality from the
// "PlayerShape" datablock. This way our Bot can do anything that a human 
// player can do. The only difference is, our Bot is being controlled by the 
// computer so we need to add a few extra functions so it can think for it 
// self.
//-----------------------------------------------------------------------------

datablock PlayerData(MyBot:PlayerShape)
{
	// TO DO: Add extra stuff here to manage new A.I. functionality...
	patrol = true;
	attack = false;
};

function MyBot::onReachDestination( %this, %obj )
{
	// Moves to the next node on the path.
	if( %obj.path !$= "" )
	{
		if( %obj.currentNode == %obj.targetNode )
			%this.onEndOfPath( %obj, %obj.path );
		else
			%obj.moveToNextNode();
	}
	else
		echo( "MyBot::onReachDestination warning - Path is blank!" );
}

function MyBot::onEndOfPath( %this, %obj, %path )
{
	%obj.nextTask();
}

//-----------------------------------------------------------------------------
// AIPlayer static functions
//-----------------------------------------------------------------------------

function AIPlayer::spawn( %name, %spawnPoint )
{
	// Create the A.I. driven bot object...
	%player = new AIPlayer()
	{
		dataBlock = MyBot;
		path = "";
	};

	MissionCleanup.add( %player );
	%player.setShapeName( %name );
	%player.setTransform( %spawnPoint );

	return %player;
}

function AIPlayer::spawnOnPath( %name, %path )
{
	// Spawn a bot and place him on the first node of the path
	if( !isObject( %path ) )
	{
		echo( "AIPlayer::spawnOnPath failed - Bad Path!" );
		%this.path = "";
		return;
	}

	%node = %path.getObject(0);
	%player = AIPlayer::spawn( %name, %node.getTransform() );

	return %player;
}

function AIPlayer::followPath( %this, %path, %node )
{
   // Start the bot following a path
   %this.stopThread(0);

	if( !isObject( %path ) )
	{
		echo( "AIPlayer::followPath failed - Bad Path!" );
		%this.path = "";
		return;
	}

	if( %node > %path.getCount() - 1 )
		%this.targetNode = %path.getCount() - 1;
	else
		%this.targetNode = %node;

	if( %this.path $= %path )
		%this.moveToNode(%this.currentNode);
	else 
	{
		%this.path = %path;
		%this.moveToNode(0);
	}
}

function AIPlayer::moveToNextNode( %this )
{
	if( %this.targetNode < 0 || %this.currentNode < %this.targetNode )
	{
		if( %this.currentNode < %this.path.getCount() - 1 )
			%this.moveToNode( %this.currentNode + 1 );
		else
			%this.moveToNode( 0 );
	}
	else
	{
		if( %this.currentNode == 0 )
			%this.moveToNode( %this.path.getCount() - 1 );
		else
			%this.moveToNode( %this.currentNode - 1 );
	}
}

function AIPlayer::moveToNode( %this, %index )
{
   // Move to the given path node index
   %this.currentNode = %index;
   %node = %this.path.getObject(%index);
   %this.setMoveDestination( %node.getTransform(), %index == %this.targetNode );
}

//-----------------------------------------------------------------------------
// AIManager static functions
//-----------------------------------------------------------------------------

function AIManager::think( %this )
{
   if( !isObject( %this.player ) )
      %this.player = %this.spawn();

   %this.schedule( 500, think );
}

function AIManager::spawn( %this )
{
	// Bot_1 simply spawns without a path to follow. Since he has no path to 
	// follow, he'll simply spawn at the same spot that you do. This means that 
	// you'll have to move around to see him when the game first starts because
	// he'll be right on top of you.

	%bot = AIPlayer::spawn( "Bot_1", pickSpawnPoint() );

	// Bot_2 has a path to follow, so we'll help him out by having him spawn
	// by the Path's first Path Marker. Once he spawns, he'll begin to follow
	// the path using the A.I. helper functions defined here in this file.

	//%bot = AIPlayer::spawnOnPath( "Bot_2", "MissionGroup/myPath" );
	//%bot.followPath( "MissionGroup/myPath", -1 );

   return %bot;
}
#2
05/29/2007 (9:58 am)
I think your problem you are inherit from old datablock that has by change in the update verison.


Change this line

datablock PlayerData(MyBot:PlayerShape)

To

datablock PlayerData(MyBot : PlayerBody)
#3
05/29/2007 (10:02 am)
Unfortunately this doesn't change anything (still instant crash).
#4
05/29/2007 (2:34 pm)
Did you add in game.cs the necessary code to add in the AIManager? I think it's a new script object, so that way it'll continue to spawn the bot if something happens to it. I've used this resource, and I got it to work, though I did tweak it quite a bit since I implemented it. I can post my code up once I get access to my computer tonite. Will edit my post to reflect that, hope you can figure it out though!
#5
05/30/2007 (12:04 am)
Yes both the onmissionloaded and onmissionended functions have the code from the tutorial in them. I wasn't sure wether it had to come before startgame, or after, so I tried both. It both still led to an instant crash. :)

function onMissionLoaded()
{
   // Called by loadMission() once the mission is finished loading.
   // Nothing special for now, just start up the game play.
   startGame();
   
   new ScriptObject(AIManager) {};
   MissionCleanup.add(AIManager);
   AIManager.think();
}

function onMissionEnded()
{
   // Called by endMission(), right before the mission is destroyed

   // Normally the game should be ended first before the next
   // mission is loaded, this is here in case loadMission has been
   // called directly.  The mission will be ended if the server
   // is destroyed, so we only need to cleanup here.
   cancel($Game::Schedule);
   $Game::Running = false;
   $Game::Cycling = false;
      
   AIManager.delete();
}
#6
05/30/2007 (2:13 pm)
From the function you are showing you are using this tutorial with the starter.fps or the starter.racing. The tutorial is for the tutorial.base. The change I made works for tutorial.base, I tested it and did not crash. If you going to use tutorial for other start kit then you need to look at the console for errors and report them here. The tutorial.base is setup different, it is best to use them with the tutorial.base until you learn Torque to find the difference in each start kit. If you list your errors here I may be able to help.
#7
05/30/2007 (2:42 pm)
I took look at it. If you are using TGE 1.5.2 starter.fps then this is your problems. Tested it and I dont get crash. You dont need to put the lines in the function onMissionLoaded() and function onMissionEnded(). The lines are already there in the function startGame() about line 125 and function endGame() about line 138. You are getting a conflict. Make the change I said above and you should be good.
#8
05/30/2007 (11:11 pm)
You are right about the tutorial.base, it works there. However if I remove the lines you mentioned it still crashes, but I'll look into it some more. startGame() doesn't have the AIManager code either so I'm still looking for it. Thanks for all the help though.
#9
05/31/2007 (5:14 am)
What engine and stater kit are you using? All the starter kits does not have it but if I knew which one you are using I may be able to help. Also after it crash look into the console text file in the example folder that should tell you why it is crashing.