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.

Dark GDK / How to Toggle a Variable ?

Author
Message
Ninjateck
16
Years of Service
User Offline
Joined: 27th Feb 2008
Location:
Posted: 10th Jan 2009 00:23
how do you keep a variable at a certain value until a key press ?
i have a light switch that i want to turn on by a key press and turn off by the same key press and stay on until the second key press


Through the Fire and the flames we carry on!
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 10th Jan 2009 01:46
Just keep track of the state it was in last frame, if it was down the last frame and not down the current frame, then light_on = !light_on.
action 9000
16
Years of Service
User Offline
Joined: 27th May 2008
Location: Alberta, Canada
Posted: 10th Jan 2009 01:57 Edited at: 10th Jan 2009 02:08
I didn't test it but this is basically the kind of logic needed. That's how I like to do it, anyway. I find this makes the most sense.




Question:
is

exactly the same functionality as

because that's pretty awesome if it is.
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 10th Jan 2009 18:06
yes, the ! mark just means opposite of a boolean value.
Swampert
15
Years of Service
User Offline
Joined: 17th Dec 2008
Location: Somewhere in the Interwebs
Posted: 11th Jan 2009 23:38
! just instantly means false right for bools? Cuz in the 999 C++ tutorials i've read thats what it says xD

GDA - Game Developers Anonymous - Get in touch with your creative side.

Yeah I like Pokemon... so what?
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 12th Jan 2009 00:36 Edited at: 12th Jan 2009 00:40
No. ! is the unary negative. If your bool is true

bool test = true;

then the expression !test is false. It does not change the value of test. Only the expression "!test" means false.

If

bool test = false;

then "!test" evaluates to true but does not change the falseness of test itself.

Edit: If you want to flipflop the value of a bool you'd have to do something like

test = !test;

Because ! is a unary operator you can't shortcut the assignment.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 12th Jan 2009 17:52
swampert, i can see why you thought that. If you want to test if a bool is false, you use if(!var). But this is getting the opposite of the variable, and testing if it is true.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 13th Jan 2009 03:57
And many of those C++ tutorials recommend (I don't always do it myself but) for ease of reading your code later - doing this instead for readability:

if ( MyBool == FALSE ) { // blah blah

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 13th Jan 2009 05:33
In fact, as with other type variables, they recommend putting the literals first.

if (false == MyBool) {//blahty, blah blah

If you get into the habit of doing this then if you slip and put

if (false = MyBool)

the compiler yaks at you about not being able to assign a value to a literal. Modern compilers may be on the lookout for this mistake regardless of the order and at least generate a warning.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 13th Jan 2009 21:56
Wow i never heard of that done before, but I can see the use of it, because im forever putting = instead of ==. I don't think I'll adopt explicitly stating true and false though, as anyone who knows C++ should be able to read it.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 14th Jan 2009 03:06
I have already adopted that - I Still use != spaced out ... but I'm doing the if(false==MyBool) thing now - because I Like FANCY C++ tricks you can do - but that one has caused more than a headache or two.


when I do something like if(a=b){ on purpose its one thing - but when I do if(MyBool=true) on accident - UGH.

Thanx folks - good tips.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 15th Jan 2009 22:21
I always do if (a){//so on. Much easier if you ask me.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 16th Jan 2009 00:01 Edited at: 16th Jan 2009 00:04
@Zuka - True... however for prefetch optimizations, you should tailor your if statements so the most frequent "condition" happens first in the if block. So...

if(MyBool){

Works fine... it is easier... but if MyBool is usually false, you might want to get in the habit of checking it for false first or changing the boolean's "name" and definition so you're "trues" are always the more common condition and placing them first works.

This is why we get into the if(!Bool) situation... and for readability we talk of if(MyBool == false) for ease of reading but furthering it with Lilith's recommendation of if(false==MyBool) to prevent accidently assigning a value during the test due to a typo. This is the only way I know of in C++ to really catch that in manner allowing you to verify if its what you wanted to true or not ( I made a boolean funny )

Trust me I'm all for easier... and frankly - what's a clock cycle or two in a prefetch cache? Bleech.. BUT... these habits over time (throughout thousands of lines of code) add up to a gain that is hard to go back and rectify later if you didn't do it from the start. Turns out for me its easier and faster in the long run (code wise and code editing) to do it this way: if( MyBool ) or if(false==MyBool)

--Jason

[edited some err's]

Ziaden
17
Years of Service
User Offline
Joined: 22nd Aug 2007
Location:
Posted: 19th Jan 2009 03:20
A neat trick I often use to TOGGLE a variable can be applied here.
When toggling a var between two values, you can use a single line that sets the var to the current value minus the absolute value of the total of the two values. I know that sounds confusing. Here is an example. Suppose you want to toggle an int between 5 and 8 (this is arbitrary, for example sake)
This one line will do it:

I learned this trick from Dana Gruber of Sierra.

In your case, you could set up your value as 0 and 1 and toggle it like this:


I hope you find this useful.

The world's first MMOW
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 19th Jan 2009 05:37
Maybe its just me... but that kinda "Hides" the task at hand from the casual glance, and isn't a a trick that gains speed or anything else but obfusciation. In fact it causes a function code or extra inline code to be executed where a simple branch instruction will do...

For a boolean toggle:

MyBool = (!MyBool);


For Non bool... say the 5, 8 example...
if(8==MyVar){MyVar=5;}else{MyVar=8;};

But for those who like some C++ tricks in their midst but still want to be able to read that code later without to much effort trying to figure it out:

(MyVar==8)?MyVar=5:MyVar=8;


--Jason

jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 19th Jan 2009 20:24
In your code:

braces are unnecessary. Could jsut use:
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 20th Jan 2009 01:07
Really? I didn't know the else would be considered like that .. cool!

Login to post a reply

Server time is: 2024-09-30 15:35:48
Your offset time is: 2024-09-30 15:35:48