Showcase your libraries, tools and other projects that help your fellow love users.
-
TurtleP
- Party member
- Posts: 147
- Joined: Thu Mar 22, 2012 9:20 pm
-
Contact:
Post
by TurtleP » Sun Feb 03, 2013 3:07 am
Hey guys,
Not sure if anyone has done this before, but I made one.
Tired of having saved boolean to a text file and then need that boolean for your game once it loads? Well, toboolean does what it says, turns the value into a boolean!
I hope you guys enjoy this. You don't have to credit me if you do use it, but whatever.
-
Attachments
-
tobooleanexample.love
- (561 Bytes) Downloaded 100 times
-
qaisjp
- Party member
- Posts: 491
- Joined: Tue Sep 04, 2012 10:49 am
- Location: United Kingdom
-
Contact:
Post
by qaisjp » Mon Feb 04, 2013 4:57 pm
[Edit 2019]: I don't know why I was being a cunt here, sorry all
Sorry for the harshness:
Your code:
Code: Select all
function toboolean(value)
if type(value) == "string" then
if value == "true" then
return true
else
return false
end
elseif type(value) == "number" then
if value == 1 then
return true
elseif value == 0 then
return false
else
return nil
end
end
end
Better code:
Code: Select all
function tobool(v)
return v and ( (type(v)=="number") and (v==1) or ( (type(v)=="string") and (v=="true") ) )
end
Moral of the story?
Don't be attention seeking and release simple stuff like this into a .love or in the forum. Use the wiki to document this snippet.
Last edited by
qaisjp on Sun Sep 08, 2019 11:14 am, edited 2 times in total.
Lua is not an acronym.
-
Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
-
Contact:
Post
by Roland_Yonaba » Mon Feb 04, 2013 5:54 pm
I'd like to point out that, to Lua, 0 is not false.
-
Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
-
Contact:
Post
by Robin » Mon Feb 04, 2013 6:01 pm
Even better: for strings, use foo == "true", for numbers, use foo != 0. If you don't know what type you're dealing with, there is probably something wrong with your design.
-
scutheotaku
- Party member
- Posts: 235
- Joined: Sat Dec 15, 2012 6:54 am
Post
by scutheotaku » Mon Feb 04, 2013 9:01 pm
Interesting work, though, as the others suggested, this definitely isn't the best way to go about this.
qaisjp wrote:
Moral of the story?
Don't be attention seeking and release simple stuff like this into a .love or in the forum. Use the wiki to document this snippet.
Not to go o/t, but this seems a little
too harsh... I mean, why assume that he was "attention seeking"? Isn't it far more likely that he either wasn't aware of the snippets part of the wiki or didn't think of putting it there? Besides, this is definitely not the only "snippet" posted in Projects and Demos, so it's conceivable that he saw those and assumed that this was the place to post such things. Please don't take this as flaming or self-righteousness or anything like that - this just seemed overboard to someone who (like me) is a relative newcomer.
-
Xgoff
- Party member
- Posts: 211
- Joined: Fri Nov 19, 2010 4:20 am
Post
by Xgoff » Mon Feb 04, 2013 10:18 pm
my `toboolean` function would simply be
Code: Select all
function toboolean(o) return not not o end
lua already has specific semantics for objects being converted to boolean values, so peeking into the actual value to determine truthiness is dodgy
[de]serialization is an exception of course, but i would choose a different name for the function in that case
-
TurtleP
- Party member
- Posts: 147
- Joined: Thu Mar 22, 2012 9:20 pm
-
Contact:
Post
by TurtleP » Sat Feb 09, 2013 7:49 pm
qaisjp wrote:Sorry for the harshness:
Your code:
Code: Select all
function toboolean(value)
if type(value) == "string" then
if value == "true" then
return true
else
return false
end
elseif type(value) == "number" then
if value == 1 then
return true
elseif value == 0 then
return false
else
return nil
end
end
end
Better code:
Code: Select all
function tobool(v)
return v and ( (type(v)=="number") and (v==1) or ( (type(v)=="string") and (v=="true") ) )
end
Moral of the story?
Don't be attention seeking and release simple stuff like this into a .love or in the forum. Use the wiki to document this snippet.
Thanks I guess, but I'd also check for a "false" string type. But like what scutheotaku said, that was harsh. I'm definitely not seeking attention.
Users browsing this forum: Google [Bot] and 29 guests