What techniques that everyone should know?
- master both
- Party member
- Posts: 262
- Joined: Tue Nov 08, 2011 12:39 am
- Location: Chile
Re: What techniques that everyone should know?
I didn't knew that existed thanks for improving the function.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: What techniques that everyone should know?
A better way to go about things would be to make sure your sequences never have holes in the first place.
Help us help you: attach a .love.
Re: What techniques that everyone should know?
actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack. (also, the 5.2 definition of table length does allow for non-numeric keys, assuming you still consider that an "array")kikito wrote:As far as I know, table.getn and table.setn were deprecated in Lua 5.1 and removed in 5.2, as well as using a key called 'n' to handle the length. The "standard" definition of array does not include any non-array values any more (except for backwards-compatibility with Lua 5.0 libraries), so I don't think this is an issue.This is really clever, I like it, with the exception that something that is array-like wouldn't fit in here. That is to say, if you keep 'n' as the length of the array as a member of the table, then isArray is false.
Re: What techniques that everyone should know?
I have some little tricks that are really nice.
If a function only take a table or a string you can exclude the "()" between arguments:I use this a lot, it's one actually nice thing that I got in my entity system.
Also, having shortcuts for the love modules is really nice:
I actually had some more, but I can't remember them hahaha.
If a function only take a table or a string you can exclude the "()" between arguments:
Code: Select all
print"cool"
love.graphics.setColor{255, 120, 200, 150}
Also, having shortcuts for the love modules is really nice:
Code: Select all
function setUpReferences()
la = love.audio
le = love.event
lf = love.filesystem
lg = love.graphics
li = love.image
lj = love.joystick
lk = love.keyboard
lma = love.math
lm = love.mouse
lp = love.physics
lso = love.sound
lsy = love.system
lth = love.thread
lti = love.timer
lw = love.window
end
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: What techniques that everyone should know?
I would have made it return two values instead (the array + len). I do that all the time. But oh, well. Then what table.pack returns in 5.2 is not an array.Xgoff wrote:actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack
EDIT: maybe we should be talking about sequences instead of arrays. On the table.pack doc, it is mentioned that "the resulting table might not be a sequence", and the term is defined like so:
The non-numeric keys are ignored, thus. So maybe our is_array should be renamed to is_pure_sequence.We use the term sequence to denote a table where the set of all positive numeric keys is equal to {1..n} for some integer n, which is called the length of the sequence
When I write def I mean function.
Re: What techniques that everyone should know?
yeah, i wondered myself why they never did that... especially since none of the other non-deprecated table functions use that field anyway (and i hope that doesn't change)kikito wrote:I would have made it return two values instead (the array + len). I do that all the time.Xgoff wrote:actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack
Re: What techniques that everyone should know?
Switching booleans without an if-else-statement.
I'll try to explain it, but if you have any questions about what this does, see this very well written article.
So, `Boolean` would be the boolean ( `true`, `false`, `nil` ) you're checking.
Let's break it down step by step:
Both not Boolean and true have to be true. In this case, true will always be true, so now the other: not Boolean. As shown earlier in the thread, not makes true false and false true. So, if you input true, not true will be false.
Otherwise, make it false.
TL;DR/Summary:
Basically if both of the first options are true then it equal the second first option, or true in this case. Otherwise, it equals the second option, or false.
Code: Select all
function SwitchBoolean( Boolean )
return not Boolean and true or false
end
So, `Boolean` would be the boolean ( `true`, `false`, `nil` ) you're checking.
Let's break it down step by step:
Code: Select all
not Boolean and true
Code: Select all
or false
TL;DR/Summary:
Basically if both of the first options are true then it equal the second first option, or true in this case. Otherwise, it equals the second option, or false.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Re: What techniques that everyone should know?
technically you only need `not Boolean`davisdude wrote:Switching booleans without an if-else-statement.I'll try to explain it, but if you have any questions about what this does, see this very well written article.Code: Select all
function SwitchBoolean( Boolean ) return not Boolean and true or false end
So, `Boolean` would be the boolean ( `true`, `false`, `nil` ) you're checking.
Let's break it down step by step:Both not Boolean and true have to be true. In this case, true will always be true, so now the other: not Boolean. As shown earlier in the thread, not makes true false and false true. So, if you input true, not true will be false.Code: Select all
not Boolean and true
Otherwise, make it false.Code: Select all
or false
TL;DR/Summary:
Basically if both of the first options are true then it equal the second first option, or true in this case. Otherwise, it equals the second option, or false.
Re: What techniques that everyone should know?
Oh...
I feel dumb now...
EDIT: What a crummy 400th post.
I was just, uh... demonstrating ternary operations with an arbitrary example... yeah, that's it.
I feel dumb now...
EDIT: What a crummy 400th post.
I was just, uh... demonstrating ternary operations with an arbitrary example... yeah, that's it.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Re: What techniques that everyone should know?
Hahaha there's no big deal. I've already made much horrible stuff than that. 6 months ago I wrote a function for drawing "filled circles" by checking if each pixel inside the "bounding box" of the radius was closer to the center than the radius. So yeah, not quite ideal...davisdude wrote:Oh...
I feel dumb now...
EDIT: What a crummy 400th post.
I was just, uh... demonstrating ternary operations with an arbitrary example... yeah, that's it.
I also used to normalize a vector that I had created like "x, y = cos(dir)*speed, sin(dir)*speed"...
Who is online
Users browsing this forum: No registered users and 1 guest