Tankbuster code questions on movement
by Jonathon Stevens · in Torque X 2D · 12/26/2006 (7:57 pm) · 5 replies
Around line 429, GameComponents.cs:
Now, if I'm reading this correctly, the bit of code here should only execute if movement is actually detected. In other words, so long as some button, key, or joystick movement is detected. The thing is, this bit of code ALWAYS executes. The reason it always executes is because .count of buttons AND sticks is 1 when I'm not hitting any keys or even moving the mouse.
if (move != null && move.Buttons.Count > 0 && move.Sticks.Count > 0)
Now, if I'm reading this correctly, the bit of code here should only execute if movement is actually detected. In other words, so long as some button, key, or joystick movement is detected. The thing is, this bit of code ALWAYS executes. The reason it always executes is because .count of buttons AND sticks is 1 when I'm not hitting any keys or even moving the mouse.
About the author
With a few casual games under his belt as CEO of Last Straw Productions, Jonathon created the increasingly popular Indie MMO Game Developers Conference.
#2
As far as the conditions, the count is supposed to only show if something was actually done. If it were merely counting if they _exist_ then the count would be higher than 1 as at least 4 sticks exist (up, down, left, right).
12/27/2006 (11:24 am)
@Ben - You misunderstood my statement. Obviously If statements themselves will always be executed. I was stating that the If statement is always evaluating to true. (although I can see from my post how it would be easy to interpret what I meant as what you took it as.. doh)As far as the conditions, the count is supposed to only show if something was actually done. If it were merely counting if they _exist_ then the count would be higher than 1 as at least 4 sticks exist (up, down, left, right).
#3
1. The move object doesn't contain only buttons and sticks that have manipulated values. It contains all buttons and sticks. Otherwise, for instance, you'd never be able to stop firing when you let up the button. A non-pressed button still has a valid value of "not pressed" and sticks are at position 0.0 when not manipulated.
2. Four sticks don't exist (up, down, left, right). ONE stick exists with positive and negative values on two axes (pos x, neg x, pos y, neg y). It is possible for you to configure each of the four to a different stick, but the most common and conventional method is to configure them as one stick and that's how tank buster does it.
3. This code is not to detect if you have pressed any controls. The code is there to protect against programmer error of not configuring the controls correctly. It essentially is protecting against subsequent null references and array index out of bounds errors.
4. The reason it is always evaluating to true is the game is configured correctly so there is never an error condition to cause it to fail.
12/27/2006 (11:51 am)
There are a few wrong assumptions you have.1. The move object doesn't contain only buttons and sticks that have manipulated values. It contains all buttons and sticks. Otherwise, for instance, you'd never be able to stop firing when you let up the button. A non-pressed button still has a valid value of "not pressed" and sticks are at position 0.0 when not manipulated.
2. Four sticks don't exist (up, down, left, right). ONE stick exists with positive and negative values on two axes (pos x, neg x, pos y, neg y). It is possible for you to configure each of the four to a different stick, but the most common and conventional method is to configure them as one stick and that's how tank buster does it.
3. This code is not to detect if you have pressed any controls. The code is there to protect against programmer error of not configuring the controls correctly. It essentially is protecting against subsequent null references and array index out of bounds errors.
4. The reason it is always evaluating to true is the game is configured correctly so there is never an error condition to cause it to fail.
#4
12/27/2006 (12:10 pm)
Ah. Now that makes more sense. Since we were assigned to 'digitalstickright', etc. I was assuming there were multiple sticks, and not 1 with 2 axis. Thanks for clearing that up!
#5
12/27/2006 (1:40 pm)
Cool. Also, if you look at the binding of the stick you'll see an int being passed in as the last parameter. This is the index of which stick or button you are binding to.
Torque Owner Ben R Vesco
move != null
Did you actually pass a move object to this function?
move.Buttons.Count > 0
Do buttons exist for me to read values from?
move.Sticks.Count > 0
Do any sticks exist for me to read values from?
It doesn't matter if the controls are pressed or moved at all, they just need to exist because the function is going to do something like stick[2].value and if stick[2] doesn't exist, the program is going to puke with some variation of a NullReferenceException. If your program doesn't rely on buttons or sticks you can remove those checks. Also, if your ProcessTick function expects two sticks then you would want to change the condition to > 1 or >= 2 or even be strict with == 2
Does it make sense?