Log In  


what do you guys think about a implicit conversion between bools a numbers being added?

it shouldn't hurt existing cartridges but would allow for something like this..

local axisy = btn(3)-btn(2)

normally you would need to write a num function that converts bools to numbers for something like this.

1


You want to convert true to 1 and false to zero? That would be kinda wonky considering that zero is truthy in lua.


1

No need for a function. Just use a lookup table. Use it inline, costs 1 extra cycle iirc.

b2n={[false]=0,[true]=1}
...

  local axisy = b2n[btn(3)]-b2n[btn(2)]

Oh, and no, please no implicit conversions. That tends to cause hard-to-find bugs.


that works thankyou


Another way is to use the and and or operators:

local axisy = btn(1) and 1 or btn(2) and -1 or 0

You could use this:

Cart #tuteduhipo-0 | 2020-02-16 | Code ▽ | Embed ▽ | No License


@dredds

Minor point, but your code would return 1 even if both directions were pressed, which is different from the original, which returns 0 in that case.

Here's the truth table for the original expression:

 Y  (3) (2)
--- --- ---
 0   F   F
-1   F   T
 1   T   F
 0   T   T

Seems to come out ZERO here, @Felice, for no buttons are hit or both of them are:

Cart #fageyiteda-0 | 2020-02-17 | Code ▽ | Embed ▽ | No License


@dw817

That's because I was addressing dredds, not you. Your code is different from dredds' code, and yes, it will work.


Ah good then. For a moment I thought my function was in error. But yes it would be nice to have (var <>= var) calculate out to -1 or 1 and zero.

Gold star for your excellent question and observation, @Shadowblitz16.



[Please log in to post a comment]