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 .
Jasoco
Inner party member
Posts: 3725 Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:
Post
by Jasoco » Tue Sep 07, 2010 3:24 am
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.
Tesselode
Party member
Posts: 555 Joined: Fri Jul 23, 2010 7:55 pm
Post
by Tesselode » Tue Sep 07, 2010 4:41 am
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.
Code: Select all
if variable==true then variable==false else variable==true end
Short and sweet.
Jasoco
Inner party member
Posts: 3725 Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:
Post
by Jasoco » Tue Sep 07, 2010 5:18 am
I always combine it actually. But I thought there was some better simpler way than typing it all out every single time.
vrld
Party member
Posts: 917 Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:
Post
by vrld » Tue Sep 07, 2010 7:17 am
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.
hump |
HC |
SUIT |
moonshine
Simtex
Prole
Posts: 39 Joined: Sun Dec 14, 2008 5:31 am
Post
by Simtex » Tue Sep 07, 2010 7:20 am
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:
bartbes
Sex machine
Posts: 4946 Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:
Post
by bartbes » Tue Sep 07, 2010 2:58 pm
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
Tesselode
Party member
Posts: 555 Joined: Fri Jul 23, 2010 7:55 pm
Post
by Tesselode » Tue Sep 07, 2010 9:16 pm
Wouldn't using variable = not variable just make it false?
bartbes
Sex machine
Posts: 4946 Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:
Post
by bartbes » Tue Sep 07, 2010 9:23 pm
No, because not false is true.
Jasoco
Inner party member
Posts: 3725 Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:
Post
by Jasoco » Wed Sep 08, 2010 2:31 am
It's like making a negative number negative.
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.
Jasoco
Inner party member
Posts: 3725 Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:
Post
by Jasoco » Wed Sep 08, 2010 2:42 am
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?
Users browsing this forum: No registered users and 48 guests