Game Development Community

Following a path in TGB

by Shakey · in Torque Game Builder · 12/08/2009 (8:26 pm) · 2 replies

Well after all day trying to figure this out, and playing with the demo, I probably won"t have much hair tomorrow.

What I am trying to do is create a simple path in TGB, well not that simple. I have a path I want a wave of players to follow but cant seem to get or understand it, here is a sample.

www.xtremeware.net/picts/screenshot.png


I want the player to spawn at the top, the beginning in black and follow it all the way around to the right.

I have followed the astar tutorial and tried it, but I think the problem is with my playerobject and the schedule call. as long as the schedule function is there it will continue to spawn. Now all I want is a wave of 10 enemy characters following one after the other.

see the code below. I tried to modify and practice with the astar tutorial.

function Spawner::onLevelLoaded(%this, %sceneGraph)
{
// register with our possible destinations list
   if (!isObject(SpawnerSet) )
   {
      $Spawner = new SimSet(SpawnerSet);
   }
   SpawnerSet.add(%this);
}

function Enemy::findSpawn(%this)
{
   // find a random spawn spot
   if (!isObject(SpawnerSet))
   {
      // we should have spawners in our level
      error("No available spawners in level!");
      return "0 0";
   }

   %SpawnCount = SpawnerSet.getCount();
   %spawnIndex = getRandom(1, %SpawnCount) - 1;
   %Spawner = SpawnerSet.getObject(%spawnIndex);
   %startCoords = %Spawner.getPosition();
   return %startCoords;
}

//function startSpawnCycle(%timeBetweenSpawns)
//{
 	//spawnRandom(%timeBetweenSpawns);
//}

function spawn()
{
	   // create a random enemy
       %newEnemy = EnemyTemplate.cloneWithBehaviors();
  
       %newEnemy.moveSpeed = %newEnemy.baseSpeed;
    	
       %startCoords = %newEnemy.findSpawn();
       %newEnemy.setPosition(%startCoords);
       %newEnemy.destinationCoords = %newEnemy.findDestination();
       %newEnemy.startPath(%startCoords, %newEnemy.destinationCoords);
      
	
	schedule(2000, 0, spawn);

Here is the game.cs file where the spawn function gets called
function startGame(%level)
{
   Canvas.setContent(mainScreenGui);
   Canvas.setCursor(DefaultCursor);
   
   new ActionMap(moveMap);   
   moveMap.push();
   
   $enableDirectInput = true;
   activateDirectInput();
   enableJoystick();
   
   sceneWindow2D.loadLevel(%level);
   //initializeMap($pathGrid, 0, GridObjectTemplate);

   // start off our spawn cycle
  schedule(2000, 0, spawn);

}


Another question is if I wanted to create this dynamically, and not use astar, how would I do this by just placing nodes to and from. I have been reading on the addnode(x1, x2), what does the x1, x2 represent. Does it represent the node coordinates, and if so how do I find the coordinates of the node placement in the level builder. I think this may be easier for me, as then I can use some behaviors.

Any help would be beneficial.

#1
12/09/2009 (11:22 am)
Well if you are using the above code to spawn your player, you will get more than 1 since you are scheduling your spawn at the start and then scheduling it again in your spawn function. This will create a loop. Also to make things easy you should not use the enemy spawn function for your player, instead create one specific for your player and keep enemy to itself. The best thing to do would be write down what you just said but with a bit more detail. Once you have that written down go back and create the code for that case. So if you want your player to spawn at point x,y then write code for the player to spawn at x,y. Hacking and slashing someone elses code can work but usually causes more problems and is easier to just write it fresh.

As for the .addNode function, the first parameter for it is a vector, with that being said you can pass it something like this:

%position = "4 5";
%myPath.addNode(%position);

This would create a node at 4,5 with 4 being the x position and 5 being the y position. If you are going to have more then one node, which in your case would be true, then you would set the index as well like this:

%position = "4 5";
%nodeCount = 0;
%myPath.addNode(%position, %nodeCount);

Obviously this is very raw code and you would have to decide if you want your variables to be global or local but I was just using it to show an example. If the above code is for your enemy class there are a few things you would probably want to do.

Limit the number of schedules to your spawn code. The easiest way to do this is to set a global variable with the number you want to create and reduce it by one. The reason for one less is because you can use the number for the actual count and for the index number, since 0 index is always the start. While looping you just reduce the global count by 1. You can put an if statement around the schedule call like this:

if ($enemyCount >= 0)
{
schedule(2000, 0, spawn);
$enemyCount--;
}

Again raw code for an example but I think you understand what I am trying to show you.

Well I hope that points you into a better direction. I would get rid of the old code and start a new section of code. Use the old code as an example and I would write pseudo code for what you want to do. Then I would start the actual coding process.
#2
12/09/2009 (12:00 pm)
Thanks pubily,

I figured that, one more question if you could be so kind to answer. I want to place nodes in the level editor, can these nodes be scene objects used as node, from the level builder, I think this may be the magic solution, just need a point of reference for my nodes when I start coding.