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.
toboolean function
toboolean function
- Attachments
-
- tobooleanexample.love
- (561 Bytes) Downloaded 134 times
Re: toboolean function
[Edit 2019]: I don't know why I was being a cunt here, sorry all
Sorry for the harshness:
Your code:
Better code:
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.
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
Code: Select all
function tobool(v)
return v and ( (type(v)=="number") and (v==1) or ( (type(v)=="string") and (v=="true") ) )
end
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:
Re: toboolean function
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:
Re: toboolean function
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.
Help us help you: attach a .love.
-
- Party member
- Posts: 235
- Joined: Sat Dec 15, 2012 6:54 am
Re: toboolean function
Interesting work, though, as the others suggested, this definitely isn't the best way to go about this.
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.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.
Re: toboolean function
my `toboolean` function would simply be
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
Code: Select all
function toboolean(o) return not not o end
[de]serialization is an exception of course, but i would choose a different name for the function in that case
Re: toboolean function
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.qaisjp wrote:Sorry for the harshness:
Your code:Better 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
Moral of the story?Code: Select all
function tobool(v) return v and ( (type(v)=="number") and (v==1) or ( (type(v)=="string") and (v=="true") ) ) end
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.
Who is online
Users browsing this forum: No registered users and 82 guests