Game Development Community

Behavior template variable troubles and drawing lines

by Alpha-Kand · in Torque Game Builder · 03/19/2012 (1:04 pm) · 4 replies

(I shall call Behavior Template files "behavior files", functions/methods != behavior) :)
I am working with Torque 2D.

I made up a fairly small behavior file that allows the user to (in TGB) to supply it with 4 keys and a friction value to simplify making games that involve using the arrow keys or such to move an object in all directions. The friction value makes the object quickly slide to a stop instead of an immediate halt.

Anyway, with the code for moving 'untouchable' from the rest of the game, so I can copy&paste it into a different game with ease, I need a function supplied with the behavior file to get which of the four keys are being pressed (to check for collisions or whatnot). This function must be called OUTSIDE of the behavior but access variables INSIDE the behavior.

//In this function we have a couple directional variables initialized
function B4DWF::onBehaviorAdd(%this)
{
	if(!isObject(moveMap))
		return;
		
	//Bind our keys on the keyboard
	moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), moveUp, %this);
        //Blah blah the real code has a couple more lines

        %this.up = false;
        %this.down = false;
        %this.left = false;
        %this.right = false;
}

//Function binded to the up button
function B4DWF::moveUp(%this)
{
	if(%this.up == true)//THIS HAS A VALUE WHILE RUNNING
	{
                //...Real code has more lines
        }	

}

/*
In the end I would like this to return a string containing data about pressed keys
"0 0 0 0" = no keys pressed
"1 0 0 0" = up key pressed
"0 1 1 0" = down key and left key pressed
"1 1 1 1" = all keys pressed
*/

function B4DWF::getDirKeysPressed(%this)
{
        //THIS VARIABLE DOESN'T HAVE A VARIABLE WHILE RUNNING. TORSION SAYS IT HAS "YYYY" as it's value...
	if(%this.down)
		//Do something I haven't figured out yet
	else
		//Do something I haven't figured out yet
		
	return %pressed;//I will handle this later
}

But later in other code I have this...

function catPlayer::onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points){
	if(%dstObject.class $= "wall"){
                //simple test to set if collision has occurred
		%srcObject.setFlipY(true);
		
		%wallX = getWord(%normal,0);
		%wallY = getWord(%normal,1);
		
                //This actually calls the function but the variables are weird
		%collisions = B4DWF::getDirKeysPressed();//Problem here...

                //...
}

Any ideas on how to get the variables to work without global variables?

SECOND QUESTION

I couldn't see anything obvious in the Torque documentation that could allow me to draw lines or other shapes with code. For example: Like a simulated projectile arc involving sine a cosine and...stuff.

P.S. Although I am fairly new at Torque I am not real new with programming and C++. I can figure out fairly advanced stuff although I am human and need help.

About the author

I have always loved video games and computer games so decided to try making my own. Spent a little more than a year on a program simply named Game Maker which really got me interested. Finally decided to get "professional" by getting Torque 2D.


#1
03/19/2012 (8:29 pm)
Functions should be called like "B4DWF.getDirKeyPressed()".

I keep a texture in all projects that's just at 16x16 white box. You can then size it to be a line (say 200x2) and place it at the center of the line to create a line segment.

If you really want curves, I'd highly recommend that you create a custom scene object to handle that. It's easiest to look at the t2dStaticSprite to learn how to make custom scene objects. But for your case, you might want to look at t2dShapeVector to see how to draw lines.
#2
03/19/2012 (9:45 pm)
Awesome! That did it. It makes sense. Calling the behavior's function that it's working with instead of randomly calling a detached function...if that even makes sense.

As for the shapes I'll look into the things you mentioned.
Thank you!
#3
03/20/2012 (9:27 pm)
No problem and good luck!

=== Begin Aside ===

Colons are barely special characters. You can call functions with colons in them, but you must supply all of the parameters. As in

$A = new ScriptObject() { class = "Tiger"; };

function Tiger::play( %this )
{
  echo( "Tiger rolls around in dirt." );
}

$A.play(); // is the same as
Tiger::play( $A ); // but the first way is the intended way.

This, of course, extends in weird ways:
// Let's name the object instead of giving it a class.
new ScriptObject( Tiger );

function Tiger::play( %this )
{
  echo( "Tiger rolls around in dirt." );
}

// Both of these are the same
Tiger.play();
Tiger::Play( Tiger );

Just a fun fact!

=== End Aside ===
#4
03/20/2012 (10:41 pm)
Hmm...I'll try to remember that.

Actually the second option MIGHT be the way to go because although I declared the "%this.up" variable in the "onBehaviorAdded" function the "moveUp" function was actually creating its OWN %this.up and killing it when it went out of scope (it at least appears this way because it always reverted to false). Luckily the code still worked but it aroused the problem of "%this.up" in the "getDirKeysPressed" function was always false (but at least it wasn't a junk value) whether or not the "moveUp" function worked. I fixed it by changing ALL of the "%this.up"'s to B4DWF.up but I just noticed that that is a global variable in disguise. :( Oh well... but since it's obvious affiliate it shouldn't get messed up by another variable named "up" by accident.