Testing for active packages
by Noah Dyer · in Torque Game Engine · 11/01/2005 (9:04 pm) · 3 replies
I adjusted the key bindings in tutorial.base (default.bind.cs) in preparation for the game I'm desiging. I got rid of all mouse control of the player, and also eliminated pitch (i.e. the player always looks straight). But when I'm in camera mode I'd like to have all the regular controls back, (as it is I can't look down at the player or terrain). I thought the best way to do this would be with packages. i.e. when you press alt+c to switch to camera control it activates a package with all the original key bindings, and when you press it again to switch to player control it deactivates the package. The problem is that I need to be able to test if the package is active or not, so that I can switch it correctly when the camera is toggled. I traced down the activation and deactivation of packages to consoleInternal.cc, but saw no code for testing for an active package already. I also found no resources or threads regarding it. Is there any danger in writing a console function that will return the name of the active package (something along the lines of: return mActivePackages[mNumActivePackages];)?
Incidentally, I noticed that the code for activating packages seems to exit if you try and activate a package that was already activated (but may be 3 or 4 packages deep) without moving that package to the top (I don't know how common that is). If that is the case, that is something that could be added to the scripting documentation on packages, as I didn't see it said there explicity. If someone verifies that my understanding , I'll be happy to post that suggestion in the appropriate forum, I just wasn't sure I was reading the code right.
Thanks.
Incidentally, I noticed that the code for activating packages seems to exit if you try and activate a package that was already activated (but may be 3 or 4 packages deep) without moving that package to the top (I don't know how common that is). If that is the case, that is something that could be added to the scripting documentation on packages, as I didn't see it said there explicity. If someone verifies that my understanding , I'll be happy to post that suggestion in the appropriate forum, I just wasn't sure I was reading the code right.
Thanks.
#2
I am actually having 2 problems, one that I consider to be a really strange phenomenon (perhaps a problem with TorqueScript?).
1) The not so strange problem, but one I do need a solution to is that once a key is bound, that I can't unbind it. I found the ConsoleMethod "unbind" in actionmap.cc, but when it gets called in the above functions, the console says I am passing the incorrect amount of perameters. I also tried calling moveMap.push() and moveMap.pop() from withing the toggleCamera function, to no avail. Can you see what I'm doing wrong?
2)This is wierd. When I start the game, everything goes fine, with the exception of #1 above. If I test for the value of $freeCamera with an echo I get 0 as expected. The first time I press Alt+C(calling the toggleCamera function), everything still goes fine. The echo string in the freeCamera package gets called, and If I type echo($freecamera) I get 1 as expected. But when I press Alt+C after that, it calls both the if AND the else statement every time I press Alt+C, so I always end up with $freeCamera equal to zero, as that's the statement that executes second. Is there something I'm doing that would cause Torque to just plow through the if-else blocks without testing for the condition?
11/03/2005 (10:21 pm)
Thank you for thinking! It looks like I was overcomplicating things a bit. So I implemented your suggestion with code that looks like this:$freeCamera = false;
function toggleCamera(%val)
{
if (%val)
commandToServer('ToggleCamera');
if(!$freeCamera)
{
$freeCamera = true;
ActivatePackage(freeCamera);
setBindings();
}
else
{
$freeCamera = false;
DeactivatePackage(freeCamera);
setBindings();
}
}For reference sake, here is the other relevant code:function setBindings()
{
echo("\c2Setting bindings for player view");
moveMap.unbind( keyboard, a); //no strafing
moveMap.unbind( keyboard, d); //no strafing
moveMap.bind( keyboard, w, moveforward );
moveMap.bind( keyboard, s, movebackward );
//moveMap.bind( keyboard, space, jump ); //no jumping
moveMap.bind( keyboard, a, turnleft);
moveMap.bind( keyboard, d, turnright);
moveMap.bind( keyboard, space, spaceTrigger);
//no mouse functionality
moveMap.unbind( mouse, xaxis, yaw );
moveMap.unbind( mouse, yaxis, pitch );
moveMap.unbind( mouse, button0, mouseTrigger );
}
setBindings();
package FreeCamera
{
function setBindings()
{
echo("\c2Setting bindings for camera view");
//allow strafing
moveMap.unbind( keyboard, a);
moveMap.bind( keyboard, a, moveleft );
//allow strafing
moveMap.unbind( keyboard, d);
moveMap.bind( keyboard, d, moveright );
moveMap.bind( keyboard, w, moveforward );
moveMap.bind( keyboard, s, movebackward );
//moveMap.bind( keyboard, space, jump ); //no jumping
moveMap.bind( keyboard, a, turnleft);
moveMap.bind( keyboard, d, turnright);
//moveMap.bind( keyboard, space, spaceTrigger);
//restore mousefunctionality
moveMap.bind( mouse, xaxis, yaw );
moveMap.bind( mouse, yaxis, pitch );
moveMap.bind( mouse, button0, mouseTrigger );
}
};I am actually having 2 problems, one that I consider to be a really strange phenomenon (perhaps a problem with TorqueScript?).
1) The not so strange problem, but one I do need a solution to is that once a key is bound, that I can't unbind it. I found the ConsoleMethod "unbind" in actionmap.cc, but when it gets called in the above functions, the console says I am passing the incorrect amount of perameters. I also tried calling moveMap.push() and moveMap.pop() from withing the toggleCamera function, to no avail. Can you see what I'm doing wrong?
2)This is wierd. When I start the game, everything goes fine, with the exception of #1 above. If I test for the value of $freeCamera with an echo I get 0 as expected. The first time I press Alt+C(calling the toggleCamera function), everything still goes fine. The echo string in the freeCamera package gets called, and If I type echo($freecamera) I get 1 as expected. But when I press Alt+C after that, it calls both the if AND the else statement every time I press Alt+C, so I always end up with $freeCamera equal to zero, as that's the statement that executes second. Is there something I'm doing that would cause Torque to just plow through the if-else blocks without testing for the condition?
#3
Unfortunately, fixing that didn't fix the second problem. I'm sure there's something obvious in my code that's causing this, but I've looked at the code over and I can't spot it, so I'd appreciate that second pair of eyes! Thanks.
11/06/2005 (8:10 pm)
Well, I figured out problem 1. [sheepish]I WAS calling the wrong the number of perameters[/sheepish]. You'll see that in the "unpackaged" setBindings function when I call the unbindings for the mouse functionality, I was passing 3 perameters instead of two. So for those of you looking at this thread down the road, just make sure to delete the "command" perameter (the 3rd one) if you are cutting and pasting :)Unfortunately, fixing that didn't fix the second problem. I'm sure there's something obvious in my code that's causing this, but I've looked at the code over and I can't spot it, so I'd appreciate that second pair of eyes! Thanks.
Torque Owner Blake LaPierre