What's a better way of typing this?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

What's a better way of typing this?

Post by Jasoco »

I find myself doing this a lot...

Code: Select all

if variable == true then
  variable = false 
else
  variable = true 
end
There has to be a better, simpler way. But I must be missing it. Keep in mind I want to preserve boolean.
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: What's a better way of typing this?

Post by Tesselode »

That's what I always do. And it seems just fine to me!

The only thing that would make that better is combining it into one line. :P

Code: Select all

if variable==true then variable==false else variable==true end
Short and sweet.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: What's a better way of typing this?

Post by Jasoco »

I always combine it actually. But I thought there was some better simpler way than typing it all out every single time.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: What's a better way of typing this?

Post by vrld »

Code: Select all

variable = not variable
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Simtex
Prole
Posts: 39
Joined: Sun Dec 14, 2008 5:31 am

Re: What's a better way of typing this?

Post by Simtex »

First of all in Lua any variable other than "nil" and "false" is considered true, so you can shorten your if-statement to:

Code: Select all

if variable then
   variable = false
else
   variable = true
end
That's a useful tip in general, but in this specific case the following is probably the easiest solution:

Code: Select all

variable = not variable
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: What's a better way of typing this?

Post by bartbes »

Or in general those simple checks like

Code: Select all

if some_condition then
   x = a
else
   x = b
end
can be turned into this

Code: Select all

x = some_condition and a or b
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: What's a better way of typing this?

Post by Tesselode »

Wouldn't using variable = not variable just make it false?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: What's a better way of typing this?

Post by bartbes »

No, because not false is true.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: What's a better way of typing this?

Post by Jasoco »

It's like making a negative number negative.

Code: Select all

variable = -(-1)
variable = 1
Thanks Bartbes, it'll be a lot easier to type that than the other way.

if debugVar == true then debugVar = false else debugVar = true end

to

debugVar = not debugVar

Saves a lot of typing.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: What's a better way of typing this?

Post by Jasoco »

How about this?

Code: Select all

variable = variable - dt
if variable < 0 then variable = 0 end
Is that as simple as it would get?

I've tried:

Code: Select all

if variable > 0 then varible = variable - dt end
But then I end up with variable going below 0. And the alternative check would be to just add that second if/then line again. Is there something possible with the use of math.max?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 62 guests