Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Best way to flip an integer between 1 and 0

Author
Message
Glyn
6
Years of Service
User Offline
Joined: 1st Feb 2018
Location: Shropshire, UK
Posted: 21st Feb 2018 23:58
Hi there all

New to this programming language and I was wondering if there was a way to to flip a integer between 1 and 0 other than using an IF-ELSE like the following please?



Many thanks
MillaSays
6
Years of Service
User Offline
Joined: 15th Dec 2017
Location:
Posted: 22nd Feb 2018 10:39
Umm, well, AppGameKit is missing some nice things from other languages, like boolean type (you have to use integer) but the AppGameKit integer is magic, and doubles as a boolean...

So try this code:



Mind you, only an initial value of 0 will result in a 1, any other value will result in a 0.

In your code, you'd do something like this:

GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 22nd Feb 2018 10:48 Edited at: 22nd Feb 2018 10:50
Brakes_On = 1 - Brakes_On

1 - 0 = 1
1 - 1 = 0
I use this in any programming language.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 22nd Feb 2018 11:52 Edited at: 22nd Feb 2018 11:53
Depending on how often ypu do it, in T1 i can't see anything wrong with what you're doing, it'll happen perfectly quickly and I do solar all the time myself.

Gar, had to read that a ew times.....but that's a nice little trick there.
Zep
22
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 22nd Feb 2018 14:01
if GetRawKeyPressed(KEY_B)
if Brakes_On = 1
Brakes_On = 0
elseif Brakes_On = 0
Brakes_On = 1
endif
PlaySound(brake-noise)
endif

Your brake-noise is going to play if the breaks are off OR on with that code. (or any time KEY_B is pressed, basically)

I don't know...maybe that's your intention? Doesn't look right to me...

CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 22nd Feb 2018 14:12
Maybe this: (checks if the key is held down)
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 22nd Feb 2018 14:50
You don't really need else if. I usually just make some true/false constants for pseudo booleans.


Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 22nd Feb 2018 15:32
I use the same method GarBenjamin uses.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Rick Nasher
7
Years of Service
User Offline
Joined: 25th Jul 2017
Location: Amsterdam
Posted: 22nd Feb 2018 16:06
GarBenjamin's is the common practice afaik. I never used anything else since first needed it a loooong time ago.
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 22nd Feb 2018 17:15 Edited at: 22nd Feb 2018 18:09
Normally, the not method is used for a single flip, as in

myBool = not myBool

or in more mainstream C like languages

myBool = !myBool

If wanting to get fancy, one can flip an entire byte - or a single bit in a byte - using XOR, which I'm not entirely sure of how is done in AppGameKit (or why you'd do it), but in C like languages

myByte ^= 0x11111111

Where each 1 in 0x11111111 will flip a bit, and a 0 will let myByte remain at whatever value it was at in the given position. Comes in very handy when doing Arduino programming and manhandling the IO registers directly. But I suppose that is a bit out of the scope of this little discussion
Glyn
6
Years of Service
User Offline
Joined: 1st Feb 2018
Location: Shropshire, UK
Posted: 22nd Feb 2018 17:45
Thanks very much guys - I will probably use GarBenjamin's method as that cuts down on typing.

Yep it is for a parking brake, so it needs to toggle and play the sound when the user presses the B key...

Cheers
Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 22nd Feb 2018 20:50
Funny, I've always done it like this.

Brakes_On = (Brakes_On + 1) && 1

I like the look of GarBenjamin's approach though, so might use it in future.
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 23rd Feb 2018 13:18 Edited at: 23rd Feb 2018 13:19
I also do it like GarBenjamin
Quote: "foo = 1 - foo"


you can also do
Quote: "foo = not foo"

In this case,
if foo is anything <>0, let's say 1337, it will switch to 0, then toggle between 1 and 0
if foo is initially 0, it will switch to 1, then toggle between 0 and 1


PSY LABS Games
Coders don't die, they just gosub without return
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Feb 2018 16:09
I've always used x = 1 - x to toggle. It's the simplest way.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Supertino
7
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 26th Feb 2018 10:47
I normally do this;

Login to post a reply

Server time is: 2024-11-24 00:29:03
Your offset time is: 2024-11-24 00:29:03