Global Variables
by David Ravel · in Torque Game Builder · 02/19/2009 (1:36 pm) · 3 replies
I'm trying to set up a system of triggers that change this global variable when you enter them. I'm using the global variable as a way to represent different states the player is in and a number of functions will look to the global variable to set the player's speed and eventually animations, environment, and a number of different things. I think I'm going about it all wrong because everything I've done so far results in the scripts getting random parse errors at the ends of statements or before comment lines. I can't figure out where these problems are originating because they just move around as I try to change different things. Here are the script files I've made for this purpose. I hope somebody can help me figure out what I'm doing wrong. I'm also not sure how to initialize my global variable. Wherever I put it in scripts or in datablocks I wind up just getting parse errors and it becomes unusable.
function potionA::onEnter( %this )
{
// If the player is in either a neutral or depressed state increase it into a (more) depressed state.
if ($potionState >= 0 < 3)
{
$potionState++;
}
// If the player is in a hyper-depressed state do nothing.
if ($potionState >= 3)
{
return;
}
// If the player is in a stimulated state bring him to the neutral state and then to a depressed state.
if ($potionState < 0)
{
$potionState = 1;
}
}
function potionB::OnEnter(%this)
{
// If the player is in a neutral state bring him to a stimulated state.
if ( $potionState = 0 )
{
$potionState - 2;
}
// If the player is in a stimulated state increase it.
if ( $potionState < 0 > -3 )
{
$potionState--;
}
// If the player is in a hyper-stimulated state do nothing.
if ( $potionState = -3 )
{
return;
}
// If the player is in a depressed state bring him to the neutral state and then to a depressed state.
if ( $potionState > 0 )
{
$potionState = -2
}
}
function PotionAEnd::OnEnter(%this)
{
// If the player is in a depressed state reduce it
if ( $potionState > 0)
{
$potionState--;
}
}
function PotionBEnd::OnEnter(%this)
{
//If the player is in a neutral or depressed state do nothing.
if ( $potionState >= 0 )
{
return;
}
//If the player is in a stimulated state decrease it.
if ( $potionState < 0 > -3)
{
$potionState++;
}
//If the player is in a hyper-stimulated state greatly decrease it.
if ($potionState = -3)
{
$potionState + 2;
}
}
function Player::PotionState(%this)
{
if ($potionState = 0)
{
player.runSpeed = 16;
}
if ($potionState = 1)
{
player.runSpeed = 12;
}
if ($potionState = 2)
{
player.runSpeed = 8;
}
if ($potionState = 3)
{
player.runSpeed = 4;
}
if ($potionState = -1)
{
player.runSpeed = 12;
}
if ($potionState = -2)
{
player.runSpeed = 24;
}
if ($potionState = -3)
{
player.runSpeed = 32;
}
}
Thanks a lot for any help you can provide. I'm new and I'm not trying to do anything that complicated, but that doesn't seem to help.
function potionA::onEnter( %this )
{
// If the player is in either a neutral or depressed state increase it into a (more) depressed state.
if ($potionState >= 0 < 3)
{
$potionState++;
}
// If the player is in a hyper-depressed state do nothing.
if ($potionState >= 3)
{
return;
}
// If the player is in a stimulated state bring him to the neutral state and then to a depressed state.
if ($potionState < 0)
{
$potionState = 1;
}
}
function potionB::OnEnter(%this)
{
// If the player is in a neutral state bring him to a stimulated state.
if ( $potionState = 0 )
{
$potionState - 2;
}
// If the player is in a stimulated state increase it.
if ( $potionState < 0 > -3 )
{
$potionState--;
}
// If the player is in a hyper-stimulated state do nothing.
if ( $potionState = -3 )
{
return;
}
// If the player is in a depressed state bring him to the neutral state and then to a depressed state.
if ( $potionState > 0 )
{
$potionState = -2
}
}
function PotionAEnd::OnEnter(%this)
{
// If the player is in a depressed state reduce it
if ( $potionState > 0)
{
$potionState--;
}
}
function PotionBEnd::OnEnter(%this)
{
//If the player is in a neutral or depressed state do nothing.
if ( $potionState >= 0 )
{
return;
}
//If the player is in a stimulated state decrease it.
if ( $potionState < 0 > -3)
{
$potionState++;
}
//If the player is in a hyper-stimulated state greatly decrease it.
if ($potionState = -3)
{
$potionState + 2;
}
}
function Player::PotionState(%this)
{
if ($potionState = 0)
{
player.runSpeed = 16;
}
if ($potionState = 1)
{
player.runSpeed = 12;
}
if ($potionState = 2)
{
player.runSpeed = 8;
}
if ($potionState = 3)
{
player.runSpeed = 4;
}
if ($potionState = -1)
{
player.runSpeed = 12;
}
if ($potionState = -2)
{
player.runSpeed = 24;
}
if ($potionState = -3)
{
player.runSpeed = 32;
}
}
Thanks a lot for any help you can provide. I'm new and I'm not trying to do anything that complicated, but that doesn't seem to help.
#2
One thing that I'm totally lost on is creating the global variable. I know you don't have to create and define it before you use, Torque will do it for you, but if that's so will it start out at 0?
02/19/2009 (2:26 pm)
haha thanks, of course it's going to a number of weird little problems, I'm still learning the language. I'll make the changes and hopefully that'll help.One thing that I'm totally lost on is creating the global variable. I know you don't have to create and define it before you use, Torque will do it for you, but if that's so will it start out at 0?
#3
Also, just a thought. Take a look at the "switch" statement instead of using all those "if" statements.
It makes for easy to read code when all your "if" statements are just testing for alot of specific values.
May not work when you are testing < or > instances but will work for bottom half of your code.
02/19/2009 (6:51 pm)
I would make it a habit to go ahead and set it to 0 when you initialize or startup game... Can't hurt and then you "know for sure" since computers can be sneaky..Also, just a thought. Take a look at the "switch" statement instead of using all those "if" statements.
It makes for easy to read code when all your "if" statements are just testing for alot of specific values.
May not work when you are testing < or > instances but will work for bottom half of your code.
Torque Owner Dustin Sims
if ($potionState >= 0 < 3)
You may want to try this
if ($potionState >= 0 && $potionState < 3)
Not sure, but I don't think torquescript will like how you had it set up because it looks like your saying that 0 < 3 which is always the case.
edit:
And these if ($potionState = 0) probably need to be if ($potionState == 0) or the if statement will assign 0 to $potionState instead of comparing it to 0..