TGB scripting issues...Need help
by Jay Rakestraw · in Torque Game Builder · 02/24/2007 (6:02 pm) · 7 replies
Hello,
I am a student using TGB, and have stumbled upon a problem with my scripting. Our game is a top down puzzle based game. We are using objects (such as the twig referenced in code below) to solve the puzzles. I was hoping for the object to dismount directly in front of the player character, but it dismounts and radomly places. (It picks up the item just as planned, but the issue is purely dismount.)
Any suggestions?
Here is script:
Thanks.
I am a student using TGB, and have stumbled upon a problem with my scripting. Our game is a top down puzzle based game. We are using objects (such as the twig referenced in code below) to solve the puzzles. I was hoping for the object to dismount directly in front of the player character, but it dismounts and radomly places. (It picks up the item just as planned, but the issue is purely dismount.)
Any suggestions?
Here is script:
// Called by player.cs when spacebar is pressed within
// an item trigger.
function playerChar::mountItem(%this, %itemTrigger)
{
%item = %itemTrigger.getMountedParent();
%item.setCollisionActive(0,0);
%item.mount($pChar);
%itemTrigger.setCollisionActive(0, 0);
$carrying = 1;
$carryingNumber = %item;
$carryingType = %itemTrigger.itemType;
$carryingTrigger = %itemTrigger;
}
function playerChar::dismountItem(%this, %item)
{
$offsetX = 0.2;
$offsetY = 0.2;
%item.dismount();
if ($rotation $= "left")
{
%item.setPositionX($pChar.getPositionX - $offsetX);
}
else if ($rotation $= "right")
{
%item.setPositionX($pChar.getPositionX + $offsetX);
}
else if ($rotation $= "down")
{
%item.setPositionY($pChar.getPositionY + $offsetY);
}
else if ($rotation $= "up")
{
%item.setPositionY($pChar.getPositionY - $offsetY);
}
$carrying = 0;
$carryingNumber = 0;
$carryingType = "nothing";
%item.setCollisionActive(0,1);
$carryingTrigger.setCollisionActive(0, 1);
}Thanks.
#2
02/24/2007 (7:58 pm)
I actually use Tortion, but cant spot my error. It does dismount, but not like I want it to.
#3
02/24/2007 (8:34 pm)
Hmmm. So you put a break point in...then you step through this code....and you can't see what's happening? How is it you want to dismount?
#4
02/24/2007 (8:46 pm)
I was hoping to get it to drop the item directly in front of the player character in the direction he is facing.
#5
How about some screenshots, or even an avi?
02/25/2007 (1:04 pm)
I think you need to describe your problem in much more detail. I, for one, don't understand it...How about some screenshots, or even an avi?
#6
I think that should correct your positioning problem.
03/02/2007 (12:56 pm)
It looks like you forgot to put the parens on your getposition calls. TGB is treating them as dymanic vars and not method calls. Try changing your %item.setPositionX($pChar.getPositionX to %item.setPositionX($pChar.getPositionX() do this for all your getposition calls. I think that should correct your positioning problem.
#7
A little off-topic, but this seems like a good time to bring up potentially useful abuses of TorqueScript. If the following is confusing, please disregard. Because TS is typeless and everything is stored as a string, you can do wacky things with it. For example:
What's happening in the first two lines might seem a little weird to anyone with any experience with sane languages, but here's the basic breakdown of the whole shabang...
Each of the first two lines assigns the result of a subtraction to a variable. Each of the two numbers being subtracted in each case are each either 'true' or 'false' (depending on the evaluation of each comparison), which in TS equate to 1 or 0. In other words, if %this.rotation were string-equal to "right" then %directionX would end up with a value of 1 (1 - 0) and %directionY would end up with a value of 0 (0 - 0).
The third line scales the vector by a set magnitude and stores that vector in a local variable. For those not familiar with the operator, SPC is the equivalent of inserting a space into a string (an equal alternative to SPC would be: @ " " @). This results in a vector (vectors in TS are represented by two floats delimited by a space character).
The fourth line sets the position of the item based on the position of %this offset by the vector created in the above lines.
Because script-based string comparisons can become expensive (well... eventually), it's slightly more efficient to store the input values of each direction as a bool and replace the above string comparisons with individual variables that store the values of each input direction. If you wanted to go this route, here's an example...
Assuming these flags were stored on an object (any object.. -> %this) as 'left', 'right', 'up', and 'down' - the above code sample would look like this:
I hope this was helpful. I like to point out these features of TS because they're not immediately obvious to anyone with a programming background and once you understand how they work they can help you unlock the true power of TorqueScript (by which I mean "the dark art of egregious script hackery").
03/03/2007 (5:43 am)
Guy's response is solid. A little off-topic, but this seems like a good time to bring up potentially useful abuses of TorqueScript. If the following is confusing, please disregard. Because TS is typeless and everything is stored as a string, you can do wacky things with it. For example:
%directionX = (%this.rotation $= "right") - (%this.rotation $= "left"); %directionY = (%this.rotation $= "down") - (%this.rotation $= "up"); %dirVector = t2dVectorScale(%directionX SPC %directionY, %this.offsetMagnitude); %item.setPosition(t2dVectorAdd(%this.getPosition(), %dirVector));
What's happening in the first two lines might seem a little weird to anyone with any experience with sane languages, but here's the basic breakdown of the whole shabang...
Each of the first two lines assigns the result of a subtraction to a variable. Each of the two numbers being subtracted in each case are each either 'true' or 'false' (depending on the evaluation of each comparison), which in TS equate to 1 or 0. In other words, if %this.rotation were string-equal to "right" then %directionX would end up with a value of 1 (1 - 0) and %directionY would end up with a value of 0 (0 - 0).
The third line scales the vector by a set magnitude and stores that vector in a local variable. For those not familiar with the operator, SPC is the equivalent of inserting a space into a string (an equal alternative to SPC would be: @ " " @). This results in a vector (vectors in TS are represented by two floats delimited by a space character).
The fourth line sets the position of the item based on the position of %this offset by the vector created in the above lines.
Because script-based string comparisons can become expensive (well... eventually), it's slightly more efficient to store the input values of each direction as a bool and replace the above string comparisons with individual variables that store the values of each input direction. If you wanted to go this route, here's an example...
Assuming these flags were stored on an object (any object.. -> %this) as 'left', 'right', 'up', and 'down' - the above code sample would look like this:
%dirVector = t2dVectorScale((%this.right - %this.left) SPC (%this.down - %this.up), %this.offsetMagnitude); %item.setPosition(t2dVectorAdd(%this.getPosition(), %dirVector));
I hope this was helpful. I like to point out these features of TS because they're not immediately obvious to anyone with a programming background and once you understand how they work they can help you unlock the true power of TorqueScript (by which I mean "the dark art of egregious script hackery").
Torque Owner Lee Latham
Default Studio Name
Sorry I don't know the answer to your question per se, but it does seem to be a perfect example of where stepping through the code/breakpoints etc. would be extremely helpful.