xdrone Posted January 14, 2007 Share Posted January 14, 2007 i wanted to write something like this: if score <= 20 then gosub foo since score is some type of internal object, and not just an 8 bit int, the code does not seem to work. any idea on how i can check for a low score? thanks for your help -xdrone p.s. i'll post my crappy game soon. i think it's almost done Quote Link to comment Share on other sites More sharing options...
+batari Posted January 14, 2007 Share Posted January 14, 2007 i wanted to write something like this: if score <= 20 then gosub foo since score is some type of internal object, and not just an 8 bit int, the code does not seem to work. any idea on how i can check for a low score? thanks for your help -xdrone p.s. i'll post my crappy game soon. i think it's almost done You are correct in that the score is a special object - it's a 24-bit BCD. The best way to use the score is to dim three variables and point them to the 3 8-bit parts; i.e.: dim sc1=score dim sc2=score+1 dim sc3=score+2 Then checking for score <=20 could go: if sc1=0 && sc2=0 && sc3<=$20 then gosub foo Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted January 14, 2007 Share Posted January 14, 2007 i wanted to write something like this: if score <= 20 then gosub foo since score is some type of internal object, and not just an 8 bit int, the code does not seem to work. any idea on how i can check for a low score? thanks for your help -xdrone p.s. i'll post my crappy game soon. i think it's almost done Since the score is a 6-digit, 3-byte variable stored in BCD, bB is kind of picky about how it handles any statements that contain the keyword "score" in them. If you want to check the score, or set an individual digit of the score to a specific shape (e.g., if you customize the score graphics to include extra characters like a space, semicolon, etc.), the easiest way to do it is to dim three variables to each of the score's bytes, as follows: dim some_variable_0 = score dim some_variable_1 = score+1 dim some_variable_2 = score+2 Then you can check or set the individual digits of the score, in 2-digit pairs (i.e., some_variable_0 is the leftmost pair of digits, some_variable_1 is the middle pair of digits, and some_variable_2 is the rightmost pair of digits). Just be sure to use BCD notation. For example, if you want to check for the score being less than or equal to 20, you would do the following: if some_variable_0 > $00 then goto skip_foo if some_variable_1 > $00 then goto skip_foo if some_variable_2 <= $20 then gosub foo skip_foo In this example, the first two ifs skip over "gosub foo" if any of the first four digits of the score are greater than 0 (because that would necessarily mean that the score is greater than 20), and the third if performs "gosub foo" if the last two digits of the score are less than or equal to 20. You could also do it using an array, as follows: dim my_variable = score if my_variable[0] > $00 then goto skip_foo if my_variable[1] > $00 then goto skip_foo if my_variable[2] <= $20 then gosub foo skip_foo However, that code will use more ROM bytes and more CPU cycles than the first method. Since the second approach works (even if it does use more ROM and more CPU time), you might think that the following would also work: if score[0] > $00 then goto skip_foo if score[1] > $00 then goto skip_foo if score[2] <= $20 then gosub foo skip_foo However, when the bB compiler sees the "score" keyword in those ifs, it doesn't handle them as you might expect (i.e., as a simple variable array), but instead tries to treat them as if the special score variable were being referenced (since, after all, it is). In fact, the next examples won't work, either, for the same reason-- even though "score" is only part of the names of user-declared variables: rem * DOES NOT WORK!!! dim score0 = score dim score1 = score+1 dim score2 = score+2 if score0 > $00 then goto skip_foo if score1 > $00 then goto skip_foo if score2 <= $20 then gosub foo skip_foo rem * DOES NOT WORK!!! dim score_0 = score if score_0[0] > $00 then goto skip_foo if score_0[1] > $00 then goto skip_foo if score_0[2] <= $20 then gosub foo skip_foo On the other hand, the following examples *do* work, because "score" isn't used as the beginning of the variable names: dim ascore = score dim bscore = score+1 dim cscore = score+2 if ascore > $00 then goto skip_foo if bscore > $00 then goto skip_foo if cscore <= $20 then gosub foo skip_foo dim ascore = score if ascore[0] > $00 then goto skip_foo if ascore[1] > $00 then goto skip_foo if ascore[2] <= $20 then gosub foo skip_foo If you want to check an individual digit, you'll need to mask out the other digit in the BCD pair of digits, as in the following example: dim byte_0 = score dim byte_1 = score+1 dim byte_2 = score+2 rem * If the 4th digit (i.e., 100s place) is a 6, gosub foo a = byte_1 & $0F if a = $06 then gosub foo Note that you can't just use "if byte_1 & $0F = $06 then gosub foo" in the above example, because it won't compile as you might expect. Likewise, if you want to set a *pair* of digits, you can use something like "byte_0 = $12" and it will change the specified pair of digits without affecting the rest of the score. But if you want to set just one digit by itself, you'll need to use bit-masking, as follows: dim byte_0 = score dim byte_1 = score+1 dim byte_2 = score+2 score = 123456 rem * Now change the 3rd digit to a 9 without affecting the other digit in the middle pair a = byte_1 & $0F : rem * Drop the 3rd digit but keep the 4th digit byte_1 = a | $90 : rem * Set the 3rd digit to a 9 without affecting the 4th digit Michael Quote Link to comment Share on other sites More sharing options...
+atari2600land Posted January 15, 2007 Share Posted January 15, 2007 Those &'s and |'s Michael put in the code above should be doubled: i.e. a=byte_1 && $0F byte_1 = a || $90 Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted January 15, 2007 Share Posted January 15, 2007 Those &'s and |'s Michael put in the code above should be doubled: i.e. a=byte_1 && $0F byte_1 = a || $90 I'm not sure, I'll have to check. I know that the single & and | worked last night when I did a test while posting. There is a difference between bit AND and logical AND. The logical AND-- e.g., A AND B-- means that A must be true, and B must also be true, in order for the whole statement to be true. The bit AND-- e.g., A AND %00001111-- means that the binary value of A is ANDed with the binary value of %00001111, which means that each bit of the result is set to 0 if that bit is 0 in *either* of the first two numbers, or set to 1 if that bit is 1 in *both* of the first two numbers. I said that kind of awkwardly, but suppose that sc2=score+2 and it is equal to $56, meaning the last two digits of the score are 5 and 6. If you say a=sc2 & $0F (which is the same as %00001111), then a will contain $06. batari BASIC used to be very explicit about which was logical AND and which was bit AND-- & or &&, or && or &. I don't know if that's been changed in 0.99, but (if I remember correctly) one symbol is the bit operation (&, |), and two symbols is the logical operation (&&, ||). Michael Quote Link to comment Share on other sites More sharing options...
+batari Posted January 15, 2007 Share Posted January 15, 2007 & and | are bitwise operators, while && and || are logical operators. So technically, a=byte_1 & $0F is correct, but an && here would be detected as bitwise rather than logical and compile correctly with current versions of bB. This doesn't work the other way, i.e. using bitwise operators in a conditional statement doesn't work (yet.) There are no plans to allow boolean operators in assignments, so using && for everything should be safe for the indefinite future. Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted January 16, 2007 Share Posted January 16, 2007 & and | are bitwise operators, while && and || are logical operators. That's what I thought, but sometimes I forget and have to look it up. an && here would be detected as bitwise rather than logical and compile correctly with current versions of bB. I'd noticed that, which was one reason why I kept having to look it up. I thought maybe I was using the wrong one. This doesn't work the other way, i.e. using bitwise operators in a conditional statement doesn't work (yet.) There are no plans to allow boolean operators in assignments, so using && for everything should be safe for the indefinite future. I'd also noticed that bitwise & and | don't work in ifs. It would be nice if they did, especially when all of the user variables have already been used up in a program, since using bB's temp variables for storing the result of a bitwise operation can be risky (since bB uses them for its own purposes). Michael Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.