What techniques that everyone should know?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: What techniques that everyone should know?

Post by master both »

I didn't knew that existed thanks for improving the function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What techniques that everyone should know?

Post by Robin »

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

Re: What techniques that everyone should know?

Post by Xgoff »

kikito wrote:
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.
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.
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")
Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

Re: What techniques that everyone should know?

Post by Zarty55 »

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:

Code: Select all

print"cool"
love.graphics.setColor{255, 120, 200, 150}
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:

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
I actually had some more, but I can't remember them hahaha.
User avatar
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?

Post by kikito »

Xgoff wrote:actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack
:( 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.

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:
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
The non-numeric keys are ignored, thus. So maybe our is_array should be renamed to is_pure_sequence.
When I write def I mean function.
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: What techniques that everyone should know?

Post by Xgoff »

kikito wrote:
Xgoff wrote:actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack
:( I would have made it return two values instead (the array + len). I do that all the time.
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)
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: What techniques that everyone should know?

Post by davisdude »

Switching booleans without an if-else-statement.

Code: Select all

function SwitchBoolean( Boolean )
	return not Boolean and true or false
end
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:

Code: Select all

not Boolean and true
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

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

Re: What techniques that everyone should know?

Post by Xgoff »

davisdude wrote:Switching booleans without an if-else-statement.

Code: Select all

function SwitchBoolean( Boolean )
	return not Boolean and true or false
end
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:

Code: Select all

not Boolean and true
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

or 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.
technically you only need `not Boolean`
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: What techniques that everyone should know?

Post by davisdude »

Oh... :P
I feel dumb now... :P
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
Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

Re: What techniques that everyone should know?

Post by Zarty55 »

davisdude wrote:Oh... :P
I feel dumb now... :P
EDIT: What a crummy 400th post.
I was just, uh... demonstrating ternary operations with an arbitrary example... yeah, that's it.
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...

I also used to normalize a vector that I had created like "x, y = cos(dir)*speed, sin(dir)*speed"...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest