Game Development Community

More errors!

by Philip Gregory · in Torque 2D Beginner · 03/22/2013 (8:14 pm) · 4 replies

Edit: I did not copy/paste the comments at the top of my file. Error 109 in my file is on line 87 bellow (and I have marked it inside the code, in case you copy and paste it to try for yourself).

I have succesfully created an Action Adventure Toy (based off the MoveToToy) and used the Knight Animation to move east and west. Now I wanted the toy to let you pick the hero (From Knight, Barbarian, Dwarf, and Wizard) with the default being the Knight. I got the list to populate (with some help from the Aquariam Toy) but I can't get any sprite to render now. The error message I recieve (per my log) is that there is a parse error on line 109 of main.cs.

Here's my code so far after some tinkering:
function ActionAdventureToy::create( %this )
{
    // Activate the package.
    activatePackage( ActionAdventureToyPackage );    

    exec("./scripts/HeroSelection.cs");

    // Initialize the toys settings.
    ActionAdventureToy.moveTime = 1000;
    ActionAdventureToy.setSelectedHero = "ToyAssets:TD_Knight_MoveWest";
    
    // Add the custom controls.
    addNumericOption("Move time", 100, 10000, 100, "setMoveTime", ActionAdventureToy.moveTime, true, "Sets the time it takes to move to the target position.");
    addSelectionOption(getHeroList(), "Hero Selection:", 4, "setSelectedHero", false, "Please select your hero.");
    
    // Reset the toy initially.
    ActionAdventureToy.reset();        
}

//-----------------------------------------------------------------------------

function ActionAdventureToy::destroy( %this )
{
    // Deactivate the package.
    deactivatePackage( ActionAdventureToyPackage );
}

//-----------------------------------------------------------------------------

function ActionAdventureToy::reset( %this )
{
    // Clear the scene.
    SandboxScene.clear();
    
    // Create background.
    %this.createBackground();

    // Create target.
    //%this.createTarget();
    
    // Create Hero.
    %this.createHero();
}

//-----------------------------------------------------------------------------

function ActionAdventureToy::createBackground( %this )
{    
    // Create the sprite.
    %object = new Sprite();
    
    // Set the sprite as "static" so it is not affected by gravity.
    %object.setBodyType( static );
       
    // Always try to configure a scene-object prior to adding it to a scene for best performance.

    // Set the position.
    %object.Position = "0 0";

    // Set the size.        
    %object.Size = "100 75";
    
    // Set to the furthest background layer.
    %object.SceneLayer = 31;
    
    // Set an image.
    %object.Image = "ToyAssets:highlightBackground";
    
    // Set the blend color.
    %object.BlendColor = Bisque;
            
    // Add the sprite to the scene.
    SandboxScene.add( %object );    
}

//-----------------------------------------------------------------------------

function ActionAdventureToy::createHero( %this )
{
    // Create the sprite.
    %hero = new Sprite();
    
    // Set the Hero object.
    ActionAdventureToy.HeroObject = %hero;
    
BEGIN ERROR!!!    // Set the Hero's animation.
(109) if ActionAdventureToy.setSelectedHero = ( "ToyAssets:TD_Knight_MoveWest" )
      {
         %hero.animation = ToyAssets:TD_Knight_MoveWest;
      }
    else if ActionAdventureToy.setSelectedHero = ( "ToyAssets:TD_Barbarian_WalkWest" )
      {
         %hero.animation = ToyAssets:TD_Barbarian_WalkWest;
      }
    else if ActionAdventureToy.setSelectedHero = ( "ToyAssets:TD_Dwarf_WalkWest" )
      {
         %hero.animation = ToyAssets:TD_Barbarian_WalkWest;
      }
    else if ActionAdventureToy.setSelectedHero = ( "ToyAssets:TD_Wizard_WalkWest" )
      {
         %hero.animation = ToyAssets:TD_Wizard_WalkWest;
      }
    else
      {
         echo("Crtical error!");
      }
        
    // Set the blend color.
    //%object.BlendColor = Lime;
    
    // Set the transparency.
    %hero.setBlendAlpha( 0.5 );
    
    // Set a useful size.
    %hero.Size = 10;
    
    // Set the sprite rotating to make it more interesting.
    //%object.AngularVelocity = -90;
    
    // Add to the scene.
    SandboxScene.add( %hero );    
}

//-----------------------------------------------------------------------------

//function ActionAdventureToy::createTarget( %this )
//{
    // Create the sprite.
    //%object = new Sprite();

    // Set the target object.
    //ActionAdventureToy.TargetObject = %object;
    
    // Set the static image.
    //%object.Image = "ToyAssets:Crosshair3";
    
    // Set the blend color.
    //%object.BlendColor = DarkOrange;
    
    // Set a useful size.
    //%object.Size = 20;
        
    // Set the sprite rotating to make it more interesting.
    //%object.AngularVelocity = 60;
    
    // Add to the scene.
    //SandboxScene.add( %object );    
//}

//-----------------------------------------------------------------------------

function ActionAdventureToy::setMoveTime( %this, %value )
{
    %this.moveTime = %value;
}

//-----------------------------------------------------------------------------

package ActionAdventureToyPackage
{

function SandboxWindow::onTouchDown(%this, %touchID, %worldPosition)
{
    
    //Original Hero X Coordinate
    %herox = ActionAdventureToy.HeroObject.getPositionX();
    
    
    // Set the target to the touched position.
    ActionAdventureToy.TargetObject.Position = %worldPosition;
    %targetx = ActionAdventureToy.TargetObject.getPositionX();
    
    // Diagnostics    
    echo( %knightx );
    echo( %worldPosition );
    
    // Move the Hero to the touched position.
    if ( %herox < %targetx )
    {
      ActionAdventureToy.HeroObject.setFlipX (true);      
      ActionAdventureToy.HeroObject.MoveTo( %worldPosition, ActionAdventureToy.moveTime );
    }
    else // Flip the knight and go the other way
    {
       ActionAdventureToy.HeroObject.setFlipX (false);       
       ActionAdventureToy.HeroObject.MoveTo( %worldPosition, ActionAdventureToy.moveTime );
    }
}
    

function ActionAdventureToy::setSelectedHero(%this, %value)
{
    %this.selectedHero = %value;
}
};

To add to things, I'm occasionally getting a buffer overflow error when I try to reload/restart the toy. Only happened 3 times though.

About the author

Yes... Those are ribs. Current projects: Breaking out of the box in T2D, devoting efforts and talents to Middle School student tech projects (including basic Python programming!)


#1
03/23/2013 (12:31 am)
I'm not sure about why you see a buffer overflow but you have many syntax errors at the location you reported.
if ActionAdventureToy.setSelectedHero = ( "ToyAssets:TD_Knight_MoveWest" )  
{  
   %hero.animation = ToyAssets:TD_Knight_MoveWest;  
}  
if ActionAdventureToy.setSelectedHero = ( ...
...
It seems that you are trying to compare the currently selected hero and assign an appropriate animation?

The primary problem is that you do:
if ActionAdventureToy.setSelectedHero = ( "ToyAssets:TD_Knight_MoveWest" )
This isn't a correct syntax. For starters, and "if" statement looks like this:
if ( %age == 21 )
{
   echo( "Party Time!" );
}
if ( %name $= "Philip" )
{
   echo( "Hey Phillip!" );
}
You kind of use a method call of "setSelectedHero" but then have an assignment operation "=" in-between the method and its arguments "( "ToyAssets:TD_Knight_MoveWest" )"

Looking at your code I believe what you want is...
if ( ActionAdventureToy.selectedHero $= "ToyAssets:TD_Knight_MoveWest" )  
{  
   %hero.animation = "ToyAssets:TD_Knight_MoveWest";
}

In reality, all of those "if/else" statements can be reduced to:
// Assign the animation to be the currently selected hero.
%hero.animation = ActionAdventureToy.selectedHero;

I would highly recommend you look at the TorqueScript Syntax document.
#2
03/23/2013 (9:33 am)
Thanks for the Syntax heads-up. I was half asleep when I was doing this last night and ironically, I had the TS manual pulled up on the second monitor. The program is running smooth compared to last night, but I'm still getting the "Cannot Render" sprite as opposed to the sprites I have set in the option. The paths to them are right, and the knight worked before I started the selection coding. Any ideas?

Edit: I take that back, some are right, some are wrong. But the one(s) that are right still do not render.
#3
03/23/2013 (9:36 am)
If you're getting the cannot render then there's a problem with the asset you're specifying. As to what the problem is, obviously I have no way of knowing.
#4
03/24/2013 (4:28 pm)
I got it. After tweaking other things, I realized that I didn't have the program set to reset when I changed classes. I still don't get a default class, but I'm sure I'll find out the reasoning soon enough. Thanks for the help Melv!