Game Development Community

Questions - Please Advise

by Shakey · in Torque Game Builder · 12/14/2009 (12:11 pm) · 27 replies

Just for some understanding, looking at the code below,

function enemyShip::onLevelLoaded(%this, %scenegraph)
{
   %this.enemyMovement();
}

%this, is an instance of the enemyShip correct. So if I wanted to make an array of enemyship instances could I rewrite the onLevelLoaded () like this

function enemyShip::onLevelLoaded(%this, %scenegraph)
{
  int %i;
  %this[%i] = 10; 
  %this[%i].enemyMovement();
}

What I am trying to do is create 10 enemy characters and have each one spawn and follow a path that I have coded. Here is that code, it works for one enemy but what i want is for the enemies to spawn a half a second from each other and follow the same path

function enemy::onLevelLoaded(%this, %sceneGraph)
{
   %myPath = new t2dPath(){scenegraph = %this.scenegraph;}; 
   
   %myPath.addnode("-31.4 -36.4", 0);
   %myPath.addnode("-31.4 -21.3", 1);
   %myPath.addnode("8.5 -21.3", 2);
   %myPath.addnode("8.5 6.3", 3);
   %myPath.addnode("-38.5 6.3", 4);
   %myPath.addnode("-38.5 26.3", 5);
   %myPath.addnode("21.4 26.3", 6);
   %myPath.addnode("20.4 -25.2", 7);
   %myPath.addnode("31.4 -25.5", 8);
	%myPath.setPathType("LINEAR");
   
   
   %this.schedule(2000, "spawnPath");
}

function enemy::spawnPath(%this)
{
  %myPath.attachObject(%this, 5, 1, 0, 8, "wrap", 1, false);
}
Page «Previous 1 2
#1
12/14/2009 (1:52 pm)
If you want to spawn them half a second apart you need a FOR loop or you could even use a WHILE loop so something like this.

for(%i=1;%i=10;%i++)
{
schedule(%spawntime, 0, "SpawnEnemy");
%spawntime = %spawntime+500;

}
#2
12/15/2009 (12:27 am)
Wow this is turning out to be one big two week headache, I am just about to give up and look else where. I have read again again and again on how to do this, but it just not clicking,

I am trying to create a tower defense game and have enemies spawn one after the other. I have created a path and the object is attached to it, but it is only the one object running the path at run time. All i would like is some one to give me advice or code on making this happen if I want 10 enemies following the same path one after the other. The code snippets aren't helping me
#3
12/15/2009 (6:17 am)
What are you doing to create the one guy that runs along the path? Is it a behavior or is it code?

If it's just code, just use Steve D's code, and put your function name that creates an enemy into the "SpawnEnemy" spot.

// Calls your function every 1 second, starting now
for( %i = 0; %i < 10; %i++ )
  schedule( 1000 * %i, 0, "yourFunctionHere" );

I've played too many tower defense games to know exactly how you want your creatures to spawn: previous wave done, fixed timing, fixed and fast timing, and early calls.

I'd steer clear of behaviors for the spawning of waves in a tower defense game. You may want to mix types which will require you to attach the behavior to an arbitrary object. It's just better to write the code in TorqueScript to do the actual spawning.
#4
12/15/2009 (10:28 am)
Thanks

Steve and William, I looked over and over what I was doing wrong, and I figured out why, only one instance of my enemy was spawning. After reading your reply, William, you stated set up the for loop then call the function that creates the enemy object, Well in the level builder I only had one instance of the object created, so this was the only one spawning.

I created copies of the enemy and placed them outside the camera's viewing area and now they spawn one after the other. So I guess It was a matter of placing the objects in the scene.

This is great and all. So what I really want is to place one enemy object in the level and use it as a template instead of having 10 to 20 enemies lined up right after the other in the scene. I am thinking of using a scene object and have them spawn from there, or just code the enemy objects in torque script using the one as a reference. Would this possible.
#5
12/15/2009 (4:30 pm)
You can use "clone" or "cloneWithBehaviors". That will make a copy of the object.

As a side, this is my 500th post! Yippee!
#6
12/21/2009 (1:36 pm)
Could someone tell me what I am doing wrong, I really thank you guys for helping me out, I know William said I could use clone(). Here is the code below. I want to clone an object and I know I am missing something, cause it only makes one object, the one from the level builder that I set up, but I do not want to creat, say ten of the same object in the level builder.

function EnemyClass::onLevelLoaded(%this, %sceneGraph)
{
      %this.clone();
      
      for(%i = 0; %i < 10; %i++)
      {
         %this.schedule(1000 * %i, "followPath");
      }
}

function EnemyClass::followPath(%this)  
{  

    // Define a new path for the enemies
   %myPath = new t2dPath(){scenegraph = %this.scenegraph;};   
   
   // Start, between and end nodes for the path the enemy is to follow   
   %myPath.addnode("-31.4 -36.4", 0);
   %myPath.addnode("-31.4 -21.3", 1);
   %myPath.addnode("8.5 -21.3", 2);
   %myPath.addnode("8.5 6.3", 3);
   %myPath.addnode("-38.5 6.3", 4);
   %myPath.addnode("-38.5 26.3", 5);
   %myPath.addnode("21.4 26.3", 6);
   %myPath.addnode("20.4 -25.2", 7);
   %myPath.addnode("31.4 -25.5", 8);
   %myPath.setPathType("LINEAR");
   
   %myPath.attachObject(%this, 8, 1, 0, 8, "wrap", 1, false); 
}

This is the only object that attaches to the path I created, but I want ten consecutively to follow the same path. Please help.
#7
12/21/2009 (1:50 pm)
In your onLevelLoaded() method, you're repeatedly calling schedule() on the original enemy object, but you should be calling it on the clones instead, somthing like this:

function EnemyClass::onLevelLoaded(%this, %sceneGraph) {
    for(%i = 0; %i < 10; %i++) {
        %theClone = %this.clone();
        %theClone.schedule(1000 * %i, "followPath");
    }
}

(Typed in my browser, not tested, your mileage may vary, no warranty express or implied, etc.)
#8
12/21/2009 (2:07 pm)
Following that advice, I assigned the %theClone variable the instance of my enemy, so now nothing spawns on the path. When the followPath function is called, it is still passed the enemy instance and not the variable %theClone, is this correct, to still pass in the %this instance, and if you look at the attachObject method it references %this, but in all nothing attahces to the path and move along as previously with the one object
#9
12/21/2009 (2:21 pm)
%this is a local variable that exists only inside a method, and refers to the object on which the method was called. It makes no difference at all that the same object is referred to elsewhere as %theClone.

You can verify this (no pun intended) by adding a few echo()s:

function EnemyClass::onLevelLoaded(%this, %sceneGraph) {
    echo("Original enemy ID:" @ %this);
    for(%i = 0; %i < 10; %i++) {
        %theClone = %this.clone();
        echo("Copy #" @ %i @ " ID:" @ %theClone);
        %theClone.schedule(1000 * %i, "followPath");
    }
}

function EnemyClass::followPath(%this) {
    echo("followPath() called for enemy ID:" @ %this);
}
#10
12/21/2009 (2:34 pm)
Thanks for all your help Sherman, I put the echo statements into the functions and it turns out that the followPath function is not being called, as I have put and echo statement there and no echo statements generated from that function, here is what I have

Quote:
Original enemy ID:1315
Copy #0 ID:1316
Copy #1 ID:1317
Copy #2 ID:1318
Copy #3 ID:1319
Copy #4 ID:1320
Copy #5 ID:1321
Copy #6 ID:1322
Copy #7 ID:1323
Copy #8 ID:1324
Copy #9 ID:1325

SO I think it is problem with the way I am calling the followPath function
#11
12/21/2009 (3:09 pm)
Nothing jumps out at me as being obviously wrong with how you're scheduling the call to followPath() - is there anything that looks relevant in console.log?
#12
12/21/2009 (3:14 pm)
No I have commented out the call to schedule and it still gives me the results above, it is like it is not even calling followpath function. It copies all the enemy instances and nothing, with or without schedule being called.
#13
12/21/2009 (3:58 pm)
This is all I am getting in the console log
//-------------------------- 12/21/2009 -- 13:56:38 -----
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/profiles.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/cursors.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/messageBoxOk.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/messageBoxYesNo.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/messageBoxYesNoCancel.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/messageBoxOKCancel.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/messageBoxOKCancelDetailsDlg.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/messagePopup.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/options.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/remap.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/console.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/NetworkMenu.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/startServer.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/joinServer.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/waitingForServer.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/helpDlg.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/messageBox.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/help.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gui/chatGui.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/screenshot.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/metrics.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/Gui/FrameOverlayGui.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/scriptDoc.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/keybindings.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/options.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/levelManagement.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/projectManagement.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/projectResources.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/common/gameScripts/align.cs.

Torque Game Builder (v1.7.4) initialized...
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/managed/datablocks.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/managed/persistent.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/managed/brushes.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/gameScripts/datablocks.cs.
Compiling C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/behaviors/attachPath.cs...
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/behaviors/attachPath.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/gameScripts/guiProfiles.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/gui/mainScreen.gui.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/gameScripts/game.cs.
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/gameScripts/enemy.cs.
Activating DirectInput...
DirectInput joystick failed to enable!
Loading compiled script C:/Users/Shakey/Documents/MyGames/VectorizeTD/game/data/levels/levelOne.t2d.
Original enemy ID:1314
Copy #0 ID:1315
Copy #1 ID:1316
Copy #2 ID:1317
Copy #3 ID:1318
Copy #4 ID:1319
Copy #5 ID:1320
Copy #6 ID:1321
Copy #7 ID:1322
Copy #8 ID:1323
Copy #9 ID:1324
DirectInput deactivated.
Shutting down the OpenGL display device...
Making the GL rendering context not current...
Deleting the GL rendering context...
Releasing the device context...
#14
12/21/2009 (4:44 pm)
Figured it out, instead of using clone() I used clonedWithBehaviors(), all enemies spawned on my path. Looking at the enemies on the path I noticed that the first enemy that is spawned starts first then it seems like the schedule call kicks in the enemies are not spaced apart according to how I want them to uniformly follow the path. Any ideas.
#15
12/21/2009 (5:41 pm)
Yeah I don't think "clone()" copies the class field from one object to the new one. "clone(1)" MIGHT work.

As for their spacing... what's wrong with it exactly? Is just the first guy out of alignment or are all of them? Your description there doesn't really help much. It'd also probably help if you put up your actual new code since it seems to be quite different from your last code post.

#16
12/21/2009 (6:01 pm)
Well the first one spawns then the consecutive ones but looking at the spacing between the objects from the second to the tenth all are schedule appropriately, it is just between the first and the second that isn't scheduling right.

Here is the code below

function CircleEnemy::onLevelLoaded(%this, %sceneGraph)
{
             
      
      echo("Original enemy ID:" @ %this);  
         
      for(%i = 0; %i <= 10; %i++)
      {
         %theClone = %this.clone(1);    //Clone the instance of the enemy and assign it to a variable  
         echo("Copy #" @ %i @ " ID:" @ %theClone); 
         %theClone.schedule(1000 * %i, "followPath");
       
      }
}

function CircleEnemy::followPath(%this)  
{  
    echo("followPath() called for enemy ID:" @ %this); 
    
    // Define a new path for the enemies
   %myPath = new t2dPath(){scenegraph = %this.scenegraph;};   
   
   // The nodes in the path for the enemy is to follow   
   %myPath.addnode("-31.4 -36.4", 0);
   %myPath.addnode("-31.4 -21.3", 1);
   %myPath.addnode("8.5 -21.3", 2);
   %myPath.addnode("8.5 6.3", 3);
   %myPath.addnode("-38.5 6.3", 4);
   %myPath.addnode("-38.5 26.3", 5);
   %myPath.addnode("21.4 26.3", 6);
   %myPath.addnode("20.4 -25.2", 7);
   %myPath.addnode("31.4 -25.5", 8);
	%myPath.setPathType("LINEAR");
   
   %myPath.attachObject(%this, 8, 1, 0, 8, "wrap", 1, false); 
    
}
#17
12/21/2009 (6:34 pm)
All I can think of is...

1) - Where is the original enemy that everyone is cloned from -- offscreen?

2) - Maybe it doesn't like your "schedule(0," on the first guy... Try starting %i at 1 instead of 0 in your for loop and go to 11 instead of 10 and see if that spaces out the first guy with the rest.
#18
12/21/2009 (6:44 pm)
Tim the original that everyone is spawned from is off screen, it is the only one I created in the level builder and everyone else spawns from that location.

I did change the %i variable from 0 to 1, but same results.
#19
12/21/2009 (7:14 pm)
I'm about a million percent sure your problem stems from the fact that you're calling it in OnLevelLoaded.

OnLevelLoaded takes place waaay early and screws with schedules and timing pretty badly.

Try it this way:

function CircleEnemy::onLevelLoaded(%this, %sceneGraph)
{
      echo("Original enemy ID:" @ %this);  
      $enemyTemplate = %this;   

}
function CircleEnemy::followPath(%this)  
{  
    echo("followPath() called for enemy ID:" @ %this); 
    
    // Define a new path for the enemies
   %myPath = new t2dPath(){scenegraph = %this.scenegraph;};   
   
   // The nodes in the path for the enemy is to follow   
   %myPath.addnode("-31.4 -36.4", 0);
   %myPath.addnode("-31.4 -21.3", 1);
   %myPath.addnode("8.5 -21.3", 2);
   %myPath.addnode("8.5 6.3", 3);
   %myPath.addnode("-38.5 6.3", 4);
   %myPath.addnode("-38.5 26.3", 5);
   %myPath.addnode("21.4 26.3", 6);
   %myPath.addnode("20.4 -25.2", 7);
   %myPath.addnode("31.4 -25.5", 8);
	%myPath.setPathType("LINEAR");
   
   %myPath.attachObject(%this, 8, 1, 0, 8, "wrap", 1, false); 
   
   echo("ID: " @ %this @ " path is ID: "@ %myPath);
    
}

function sendwave()
{
      for(%i = 0; %i <= 10; %i++)
      {
         %theClone = $enemyTemplate.clone(1);
         echo("Copy #" @ %i @ " ID:" @ %theClone); 
         %theClone.schedule(1000 * %i, "followPath");
       
      }	
}

Then open up the console and type sendwave.
#20
12/21/2009 (7:23 pm)
Incidentally, on a completely unrelated note, wouldn't it be better to create a single path to which all of the enemies are added, instead of creating a new one every time followPath() is called? Like this:

function CircleEnemy::onLevelLoaded(%this, %sceneGraph) {
      echo("Original enemy ID:" @ %this);  
      $enemyTemplate = %this;
}

function CircleEnemy::followPath(%this, %aPath) {
    echo("followPath() called for enemy ID:" @ %this);
   %aPath.attachObject(%this, 8, 1, 0, 8, "wrap", 1, false); 
}

function sendwave()
{
    // Define a new path for the enemies
    %myPath = new t2dPath(){scenegraph = %this.scenegraph;};   
   
    // The nodes in the path for the enemy is to follow   
    %myPath.addnode("-31.4 -36.4", 0);
    %myPath.addnode("-31.4 -21.3", 1);
    %myPath.addnode("8.5 -21.3", 2);
    %myPath.addnode("8.5 6.3", 3);
    %myPath.addnode("-38.5 6.3", 4);
    %myPath.addnode("-38.5 26.3", 5);
    %myPath.addnode("21.4 26.3", 6);
    %myPath.addnode("20.4 -25.2", 7);
    %myPath.addnode("31.4 -25.5", 8);
    %myPath.setPathType("LINEAR");
   
    for(%i = 0; %i <= 10; %i++) {
        %theClone = $enemyTemplate.clone(1);
        echo("Copy #" @ %i @ " ID:" @ %theClone); 
        %theClone.schedule(1000 * %i, "followPath", %myPath);
    }
}

This approach would allow you to create multiple paths, to which your enemies could be added - like in Galaga for instance, where there are several paths the bugs can take when they're entering the screen at the start of each level.

Like I said, I doubt this is related to the problem you're having - I think Tim may be on the right track with that - but it's something worth thinking about.
Page «Previous 1 2