Game Development Community

Missing something with datablocks

by Nathan Morton · in Torque Game Builder · 06/02/2011 (9:29 pm) · 5 replies

I have the following in my datablock.cs (in the gamescripts folder) please ignore my commenting code out, thought the variables were causing problems so I decided to hard code values for now.

//********ENEMIES********

//enemyDrones
datablock t2dSceneObjectDatablock(enemyDroneDatablock)
{
     //scenegraph = $scenegraph;
     class = "enemyShip";
     Layer = "11";
     size = "6.102 5.768";
     WorldLimitMode = "kill";
     WorldLimitMin = "73.748 -67.575";
     WorldLimitMax = "33.359 76.630";
     WorldLimitCallback = "1";

     //speedY = getrandom(3,7)*2;
     //speedX = getrandom(-5,5);
     //x = getrandom(-40,40);
     //health=3;
     position = "0, -45";
     //LinearVelocity = "speedX, speedY";
     //Position = "0, -45";
     LinearVelocity = "0, 5";
     playAnimation = "RedDroneAnim";
     CollisionActiveReceive = "1";
     CollisionActiveSend = "1";
     CollisionPhysicsReceive = "1";
     CollisionPhysicsSend = "1";
     CollisionCallback = "1";
};

I have the following in my enemy.cs (where I'm creating these guys)

//********Drone Spawning********
function spawnDrone()
{
     echo("Spawning Drone");
     %enemyDrone = new t2dAnimatedSprite(){
          scenegraph = $scenegraph;
          /*class = enemyShip;
          Layer=1;
          size = "6.102 5.768";*/
          datablock = enemyDroneDatablock;
     };
     echo(%enemyDrone);
     echo(%enemyDrone.getPosition());

     /*%speed = getrandom(3,7);
     %x= getrandom(-40,40);
     %enemyDrone.playAnimation("RedDroneAnim");
     %enemyDrone.health=3;
     %enemyDrone.setPosition(%x, -45);
     %enemyDrone.setLinearVelocityY(%speed*2);
     %enemyDrone.setLinearVelocityX(getrandom(-5,5));
     %enemyDrone.setCollisionActive( true, true );
     %enemyDrone.setCollisionPhysics(false, false);
     %enemyDrone.setCollisionCallback(true);
     %enemyDrone.setWorldLimit( kill, "-73.748 -67.575 33.359 76.630" );*/
     %this = %enemyDrone;
     
     //Commenting out as there's just too much weapon fire.
     if ($game == true)
     {
          $droneSpawn = schedule (getrandom(500,1000),0,spawnDrone);
     }

     if ($game == true)
     {
          %this.schedule(1000, fireMissile);
     }
}

I spawn a bunch of these guys obviously, and I was hoping to use datablocks for at least some of the setup of my enemies, however while the enemies will be created, it's like the datablock isn't being set and for the life of me I can't figure out why.

Any help would be greatly appreciated.

Nathan

#1
06/03/2011 (3:34 am)
There is no 'datablock' field under SceneObject, use either 'config' field or setConfigDatablock() method.
#2
06/03/2011 (6:54 am)
Minor question here...

what is %this supposed to be connected to?
The Function is not Linked to any object like this example:

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

The above code is called by an object that is an DemoPlayer object

Your function:
//********Drone Spawning********  
function spawnDrone()  
{  
     echo("Spawning Drone");

Has nothing Linked to %this, so %this is nothing more then a local
variable.. BUT I never tried that before and it may actually cause
problems as %this should not be used unless inside a function like
I put above.. It shouldn't be a problem, but good Idea not to get
yourself confused by using it when nothing has been passed..

%this gets sent by the C++ code when it calls such a function
and is a pointer to the DemoPlayer
#3
06/03/2011 (10:37 pm)
Rpahut - How would I use the config field? I tried the following

config = enemyDroneDatablock;

and

config = "enemyDroneDatablock";

with no success.

When I do that it's as if none of the stuff in my datablock actually gets assigned to %enemyDrone.

John - that's left over code from the previous version - this isn't originally mine. Seeing as how %this is set to %enemyDrone and the code below it is working just fine, I'm going to say that while it's probably not technically correct, it is functional.

One of these days I'll clean that up too.
#4
06/04/2011 (2:42 pm)
Variable named %this is just a convention among programmers, it is okay to use as TorqueScript doesn't really care how you name your variables, but it can cause confusion on the human side.

Assigning config datablock should work fine both ways, even though I would prefer the second one for it giving a nice color highlight to the value being assigned.

I can see a few fields like position = "0, -45"; within drone datablock - that comma shouldn't be there, single space is used as a separator between words. I can't tell what kind of effect it can cause, may be anythig from not assigning this particular value to invalidating entire datablock.

Also you may want to clean up your code first. Assigning config to the object is a trivial task, so if it causes troubles, it's just because the code with all those left overs and commented pieces is out of your control.
#5
06/05/2011 (2:59 am)
thanks for your help guys, I've gotten at least this little section cleaned up and working nicely.

I hadn't heard about the %this variable being a convention, but then again I've never done game design before, only application design, and then it's only been VB and VB .net