x


switching booleans

i have this code

var ispaused : boolean = false;
function Update () {
    if(Input.GetKeyDown("p")){
        Switch(ispaused);
    }

}

function Switch (bool){
    if(bool == false){
        bool = true;
    }
    else{
        bool = false;
    }
    return bool;
}

the code is supposed to switch the var ispaused, but it does not. Any help on the matter would be very nice.

more ▼

asked Jun 03 '10 at 07:21 PM

3dDude gravatar image

3dDude
2.6k 66 76 103

@3dDude, just FYI, you can move the Checkmark to an Answer, even after using it. Consider moving it to @Eric5h5's answer, it really is a better way of solving your problem.

Jun 04 '10 at 02:33 AM Cyclops
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I'm not that familiar with Javascript, is it passing the Boolean by reference, or value? If it's by value, then you need to say:

ispaused = Switch(ispaused);
more ▼

answered Jun 03 '10 at 07:34 PM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

cool that works thanks!

Jun 03 '10 at 07:39 PM 3dDude
(comments are locked)
10|3000 characters needed characters left

You can actually remove the function entirely, and use this instead:

if(Input.GetKeyDown("p")){
    isPaused = !isPaused;
}
more ▼

answered Jun 03 '10 at 07:59 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

efficiency is king.

Jun 03 '10 at 08:16 PM equalsequals

Sigh... why do I even bother answering questions? :)

Jun 03 '10 at 08:56 PM Cyclops

@Cyclops: well, you actually answered the question. ;) I didn't really answer the question, I proposed something entirely different (albeit more efficient)...the correct answer is useful even if the particular function in question isn't in this case.

Jun 03 '10 at 09:28 PM Eric5h5

@Eric5h5: so - I gave an Answer that was technically correct, yet still managed to be inferior to an Answer that didn't even address the Question. Thanks, I feel better now... :)

Jun 04 '10 at 02:31 AM Cyclops

@Cyclops: Nah...if 3dDude runs into a similar problem with a completely different function, he'll know what to do because of your answer, so it's not inferior. ;)

Jun 04 '10 at 03:41 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3570
x249
x241

asked: Jun 03 '10 at 07:21 PM

Seen: 1583 times

Last Updated: Jun 03 '10 at 07:21 PM