Sections of code are unreached

BlitzMax Forums/BlitzMax Beginners Area/Sections of code are unreached

abelian_grape(Posted 2009) [#1]
So because everyone here is just so awesome with the help I decided to post yet another problem my game in development seems to be having. Here's the deal.

I have a penguin character which belongs to a Type called TCharacter. I would like this penguin to fire some snowballs. So my idea was, if the user presses the Left key and the F key simultaneously, make him fire a snowball in the left direction. If the user presses the right key and the F key, make him fire a snowball in the right direction.

So I made the snowball a type of its own, called a TObject. One of the TObject's fields is "orientation" which I set when a snowball is created which is triggered by the simultaneous pressing of a directional key and the F key. Then, whenever a snowball is created, add it to a list. Then I have my snowball throwing method within the character type run through the list of snowballs, check their orienation, and draw them if they're still on screen.

The problem is that the penguin will only fire snowballs to the right, and they are being drawn. I decided to check whether or not the code was actually making it to the "left" situation by telling it to DrawText("SNOWBALL FIRED LEFT") or DrawText("SNOWBALL FIRED RIGHT") depending on the situation, and I don't even get the text for the "snowball fired left." I honestly have no idea why this will only work in the right direction. Any ideas?

In the first code box I put in the code of interest. The second code box is the complete running code. Any and all help is greatly appreciated.








_Skully(Posted 2009) [#2]
Try using Debuglog instead... the text might be there and gone so quick you don't even see it. I dont have time right now to debug


TaskMaster(Posted 2009) [#3]
When you call KeyHit(Key_F) you are removing it form the queue. So, when you call it the first time for the Right side check, it gets cleared, so it is returning false when you check for the left side.

Change that piece of code to this:




therevills(Posted 2009) [#4]
Also use SuperStrict or at least Strict, it will help in the long run with your coding...


Jesse(Posted 2009) [#5]
your problem is with your logic. It lies in the way you are using the key_hit.
the key_hit checks for a key and clears the key after checking if the specified key was hit. That means that the first time it checks for the "F" key it comes out true then it clears the key from the buffer. By the time it reaches the second key_hit the "F" has already been cleared from the buffer and is why it never comes out true for the second "F" key test.
for the solution you can do something like this:
if "F" was hit

   if left was pressed 
          shoot left
   endif
   if right was pressed 
         shoot right
   endif
endif



abelian_grape(Posted 2009) [#6]
Awesome, it worked! Thank you all so much yet again :) And therevills, I'll have to read up on the Strict/SuperStrict commands. I have no idea what that is. Just started programming in BASIC about 2 months ago.


therevills(Posted 2009) [#7]
Basically Strict/SuperStrict will force you to code "better".

With Strict, you must declare your variables before you use them, whereas SuperStrict you must define the type of variable as well.

For example, this code wont compile using Strict
x = 5

You need to do this:
Local x
x = 5

And in SuperStrict, you need to do this:
Local x:Int
x = 5


So you know the scope and type of the variable, it makes debugging so much easier!


xlsior(Posted 2009) [#8]
So you know the scope and type of the variable, it makes debugging so much easier!


It also makes the program execution slightly faster, since you pre-determine the datatype of each variable and the compiler won't have to generalize / second guess as much.


schilcote(Posted 2009) [#9]
How much faster? Is there a noticeable difference if, say, I have to iterate through 1,000 type objects with a whole bunch of fields and have to perform some operation (like drawing to the screen) on them?


Czar Flavius(Posted 2009) [#10]
I don't know by how much, but Strict mode is win-win, so use it recruit!

*expects sir:TPerson, yes sir:TPerson*