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.

P#73115 2020-02-15 00:12

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

P#73120 2020-02-15 10:28
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.

P#73146 2020-02-16 04:25 ( Edited 2020-02-16 20:06)

that works thankyou

P#73163 2020-02-16 18:34

Another way is to use the and and or operators:

local axisy = btn(1) and 1 or btn(2) and -1 or 0
P#73165 2020-02-16 21:01 ( Edited 2020-02-16 21:02)

You could use this:

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

P#73166 2020-02-16 21:19

@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
P#73167 2020-02-17 01:05

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

P#73176 2020-02-17 16:40

@dw817

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

P#73186 2020-02-17 21:28 ( Edited 2020-02-17 21:38)

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.

P#73210 2020-02-18 18:37

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 14:05:52 | 0.038s | Q:34