LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

&& versus AND in BUTTON()

Root / FAQs / [.]

SpaceKadetaCreated:
How do you use the && symbol if ur checking for buttons. In smileBasic do you have to do a binary or hex conversion in order to check the diaganols of the d-pad? and For handling large amounts of data like say an array of 40 numbers by 40 numbers, how would you build a drawing using those 1600 numbers to draw say 400 lines? Is that possible? And least importantly, can you use button() && truth testing inside of a for loop?

use AND:
B=BUTTON()
IF B AND #A THEN ...
for diagonals of the d-pad:
B=BUTTON()
IF B AND #UP+#LEFT THEN ...

12Me21 is right, AND is the right one to use here, && will not work. All && does is determine if both variables aren't 0. For instance:
A = -8493
B = 12

PRINT "The result of A && B: ";A && B
Output:
The result of A && B: 1
Also, you can put whatever you want into a FOR loop, a while loop, or any kind of loop.

&& gives off a true or false value AND gives off the value of the two added together if both are not false (00)

AND gives off the value of the two added together if both are not false (00)
That is not entirely true. AND is a bitwise operator so if both bits are true, then that bit will be true in the final value. Here's an example:
A=&B0001101 '13
B=&B0011001 '25

PRINT BIN$(A AND B)
PRINT A AND B
OUTPUT
1001
9

How do you use the && symbol if ur checking for buttons.
You can't use the && operator when checking for button presses. && is a boolean operator (sorta) so all it does is compare two variables to see if they are "true". In the case of SmileBASIC, true mean not zero.
In smileBasic do you have to do a binary or hex conversion in order to check the diaganols of the d-pad?
I don't exactly know what you mean by this, but let me try to answer it anyways. Let me just explain how reading button input works. Each button has a value associated with it. Each value represents a bit (as in binary). For instance:
UP    = 1 which is 00000001 in binary.
DOWN  = 2 which is 00000010 in binary.
LEFT  = 4 which is 00000100
RIGHT = 8 which is 00001000
And so on and so forth for all the buttons. So this means that if someone is pressing left on the control pad, then the value of BUTTON() will be 4, or 00000100 in binary. If they are pressing down and left then the value of BUTTON() will be 6 (2 + 4) or 00000110 (00000100 + 00000010). Now seeing this you might think "oh, then I can just write IF BUTTON() == 6 THEN GOTO @DOWNLEFT", but what if someone was pressing DOWN + LEFT + A? It would not equal 6. This is why you must use AND. Onto more explaining. Let's say someone is pressing DOWN, LEFT and A (A is 16 or 00010000), than BUTTON() would equal 22 (00010110). We only need to know if the left and down bits are on, so we use AND. Here's an illustration:
00010110 <- This is the value of BUTTON()
AND
00000110 <- Bits we need to check
--------
00000110 <- Result of AND

or

22 AND 6 = 6
Now you might think "oh, then I can just write IF BUTTON() AND 6 THEN GOTO @DOWNLEFT" but what if they are only pressing DOWN? Then it would equal 4 (00000100) and SmileBASIC would still think it to be "true", thus performing the THEN task when you didn't want it too. This is where the final part comes in. Let's say someone is pressing DOWN and A, then BUTTON() would equal 18 (00010010):
00010010 <- This is the value of BUTTON()
AND
00000110 <- Bits we need to check
--------
00000110 <- Looks like only DOWN is being pressed

or

18 AND 6 = 4
We don't want it to equal 4 (00000010), because that mean only down is being pressed. We want both buttons to be pressed before we perform the action. This can be easily remedied: IF (BUTTON() AND 6) == 6 THEN GOTO @DOWNLEFT And that's how it works. Now you don't necessarily need to do this with one IF statement. In fact it might be better to use separate IF statements for each button instead of making combined diagonal button IF statements to make it a bit less complicated. Here's an example of something you might include in your loop:
B = BUTTON(0)
IF B AND #UP THEN GOSUB @UP
IF B AND #DOWN THEN GOSUB @DOWN
IF B AND #LEFT THEN GOSUB @LEFT
IF B AND #RIGHT THEN GOSUB @RIGHT
(Note that I am using # values for each of the buttons. These are the same values as the button values I've been using in the rest of this post. SmileBASIC includes them as a convenience.)
For handling large amounts of data like say an array of 40 numbers by 40 numbers, how would you build a drawing using those 1600 numbers to draw say 400 lines? Is that possible?
You could make an array filled with color values and then you could use GLOAD, but why would you use an array that is 40x40 to draw something that is 4x400?
And least importantly, can you use button() && truth testing inside of a for loop?
Yes. You can put anything you want to in a FOR loop.

This difference has been noted for integration into TinySB in the near future.

Thx a lot legomaister you really have a helped me alot with The Diaganol d-pad recognition.

You could make an array filled with color values and then you could use GLOAD, but why would you use an array that is 40x40 to draw something that is 4x400?
I'm sure this is wrong, Idk am I going a sorta correct direction?
DIM ARR[0],EMPTY[2000]
FOR I=0 TO 1999
EMPTY[I]=0
NEXT
WHILE X<10
FOR I=1 TO 10
FOR II=1 TO 10
GCLIP 1, 10*I,10*I, 10+10*I, 10+10*I
GCOLOR RGB(
ARR[]=EMPTY[]
GSAVE ARR[0], , 10*I,10*I, 10+10*I, 10+10*I
NEXT 
NEXT
COLORCK I
FOR I=1 TO 100
PUSH ARRAY[0]= ARR[I]
NEXT
FOR I=1 TO 10
PUSH COLORARRAY[4,I], PALLETE[]
NEXT
INC X
WEND
GLOAD ARRAY

DEF COLORCHK CHK
FOR I=0 TO COLORCOUNTER-1
IF CHK && 1 THEN
 PUSH (PALETTE[4,I] )
ELSE PALETTE[I]=0
END

So are you trying to draw an image using a limited color palette?

So are you trying to draw an image using a limited color palette?
Yeah, I am, but I only don't understand how to associate the pixel ranges with colors from the palette. I'm trying make a Greyscale Drawing, and a GREEN palette and work from there

GLOAD allows you to use a palette. GLOAD [X,Y,Width,Height,]Image array,Palette array,1 (It's hard to type on a phone, I'll help you more later today)

I have progress, credit to Algebra A/b So, simplifying with palette might make my job a lot easier. oh and LEGOMEISER I'm using the %b and &b in union now, It's so cool!

GLOAD allows you to use a palette. GLOAD [X,Y,Width,Height,]Image array,Palette array,1 (It's hard to type on a phone, I'll help you more later today)
GSAVE
PALLETE DEF over
FOR an array of colors of gray 
WHILE into an array of a limited color WEND
 NEXT
GLOAD
I'm trying to do simply >>>COLORPICKER START <--#GRAYSCALE AND -->#RED -->#BLUE -->#GREEN -->#YELLOW END