Game Development Community

Toggle Attack

by Amir Chamma · in Technical Issues · 06/08/2007 (2:39 am) · 2 replies

Hey guys,
im new to the torque engine, and im loving it so far :)))

have a question, im trying to bind an auto attack toggle function, and im having problems..

in my default.bind.cs i added this:

$setAA = 1;
function autoAttack(%val)
{

%toggleAttack = $setAA;

if (%val)
{
%sel_obj = ServerConnection.getSelectedObj();
if (%sel_obj != -1)
%sel_obj_ghost = ServerConnection.GetGhostIndex(%sel_obj);
else
%sel_obj_ghost = -1;
}

if (%toggleAttack == 0)
{
commandToServer('DisableAA', %sel_obj_ghost);
%toggleAttack = 1;
}
if (%toggleAttack == 1)
{
commandToServer('EnableAA', %sel_obj_ghost);
%toggleAttack = 0;
}

}

and on commands.cs on the server ive added servercmd function EnableAA and DisableAA,
the autoAttack() is bound to the key 'Q'

my problem is that everytime i run this and press Q, it only runs EnableAA, everytime i press my attack key..

ive tryed echoing %toggleattack and it does get set to 0, so im guessing im doing something completely wrong..
can anyone help me? :)

#1
06/08/2007 (6:59 am)
You have a global $setAA = 1; then %toggleAttack = $setAA;. Delete that define for $setAA = 1; and try:

If it is set then disable it; If it's not set then set it; You don't have to define it first.

if ($setAA == 1)
{
commandToServer('DisableAA', %sel_obj_ghost);
$setAA = 0;
}
else
{
commandToServer('EnableAA', %sel_obj_ghost);
$setAA = 1;
}
#2
06/14/2007 (11:01 pm)
Just to point out here:
I noticed a big flaw in his code. You fixed it in your example above but never mentioned this flaw. If this guy is new to coding and doesn't realize this it could cause many headaches in future... Allow me to explain...

if (%toggleAttack == 0)
{
commandToServer('DisableAA', %sel_obj_ghost);
%toggleAttack = 1;
} 
if (%toggleAttack == 1)
{ 
commandToServer('EnableAA', %sel_obj_ghost);
%toggleAttack = 0;
}

Follow the code step by step...
Allow me to put your mistake into an English example:

If %toggleAttack = 0, then set it to 1;
Now that it IS MOST DEFINATELY 1, check if it is 1 and set it back to 0;

To fix this mistake, simply put an ELSE between your two functions
Instead of doing:
if (a > b)
{
}
if (a < b)
{
}

Simply do this:
if (a > b)
{
}
else if (a < b)
    {
    }

It is a simple mistake to make but it will cause you HUGE headaches later on because this is a VERY easy mistake to make and an incredibly hard one to spot later on because it doesn't give you an error, it simply doesn't do what you want it to...

Old Chinese Wise Man say:
"First rule of programming is that a program will never, ever do what you want it to do, but it will absolutely always do what you tell it to do"