How to undraw something?

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
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: How to undraw something?

Post by Xgoff »

tentus wrote:In Lua, you don't have to say == true
usually you don't, but yes you don't need this in general

i've tried ternary logic once or twice with events being called on objects, where you do want to make that distinction sometimes:
  • e == true: event ran successfully
  • e == false: event exists, but didn't run successfully
  • e == nil: event doesn't exist
or without those specific checks then it just collapses into "did happen" or "didn't happen", which is also useful

could also be useful for certain types of configuration settings (true: enable; false: disable; nil: use last/default setting)
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How to undraw something?

Post by MarekkPie »

The problem is...Lua makes nil return false.

Example:
Tnzu6.png
Tnzu6.png (25.09 KiB) Viewed 270 times
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: How to undraw something?

Post by Xgoff »

MarekkPie wrote:The problem is...Lua makes nil return false.

Example:

Image
my post probably wasn't very clear on it but you would test for true/false/nil explicitly (or at least the latter two) if you needed the distinction
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How to undraw something?

Post by MarekkPie »

Interesting. Just tested and it worked.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to undraw something?

Post by bartbes »

Actually, not nil is true, so it never reaches the third option, re-ordering them should do the trick.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How to undraw something?

Post by MarekkPie »

bartbes wrote:Actually, not nil is true, so it never reaches the third option, re-ordering them should do the trick.
Yup, that did it. Now you got the best of both worlds: succinct code and ternary testing.

Code: Select all

function nilTest(a)
	if a then
		print("true")
	elseif a == nil then
		print("nil")
	elseif not a then
		print("false")
	end
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to undraw something?

Post by Robin »

The below is equivalent:

Code: Select all

function nilTest(a)
	if a then
		print("true")
	elseif a == nil then
		print("nil")
	else
		print("false")
	end
end
If you can see why, you've got the hang of booleans.
Help us help you: attach a .love.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How to undraw something?

Post by MarekkPie »

I just wanted to be verbose.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How to undraw something?

Post by kikito »

Well, succinct is pretty much the contrary of verbose, isn't it? :)

Little known fact: "nil and true" returns nil, not false. "false and true" returns false. "<Everything else> and true" returns true. With that knowledge, you an do this:

Code: Select all

function nilTest(a)
   print(a and true)
end
When I write def I mean function.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How to undraw something?

Post by MarekkPie »

I get the "false and true" because of short-circuiting. But that's interesting about nil and true. And makes it even more succinct (or is it verbose? Maybe succbose? :shock: )
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 3 guests