Code walkthroughs

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Code walkthroughs

Post by Santos »

Doh! :rofl: Cheers bartbes, and I'm happy you like it! :awesome: And thank you for writing such fun to read code!
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Code walkthroughs

Post by Lafolie »

I also enjoyed reading this. This approach to studying code yields a ton more information that your average article. It's well written too.

Here's hoping you make more.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Code walkthroughs

Post by bartbes »

I just read through the first one you wrote, and I figured I'd explain why love.event.quit behaves the way it does.
Well, love.event.quit sends, as its name indicates, a "quit" event to love. The interesting thing about the way love works, though, is that events come right back out untouched and unhandled. At this point you're probably thinking that you've never touched the events directly before, correct, the default love.run does this for you.

Let's sketch love.run:

Code: Select all

love.load()
while true do
    process_events()
    love.update(dt)
    love.draw()
end
(This is a pseudo-code approximation of the real thing.)

If you look at that, you might see what happens, when love.load sends a quit event, it only gets handled after it finishes. Not just that, this also means that if you send a quit event in love.update, love.draw will still run once! (Or more often, depending on how you write love.quit.)

(There's actually a pretty cool event system in love, that's rarely used by lovers, but it can be used to send your own events, if you care to handle them.)
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Code walkthroughs

Post by Santos »

Aaah, thank you! :)

The walkthrough and the code have now been updated to use a return to jump out of love.load when an error occurs. Awesome.
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: Code walkthroughs

Post by josefnpat »

I'm a bloody idiot. I didn't realize I was being notified on github. its completely different. Thanks for the pull request! (six days late :( )
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Code walkthroughs

Post by Santos »

You're welcome! You might want to check out the optimization branch too. It's less cool, but seems to be around twice as fast. And feel free to revert any changes I made, some were kind of... arbitrary. :ultraglee:

Proxy by vrld has been added. Thanks to Headchant for introducing me to this function!
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Code walkthroughs

Post by Lafolie »

Add these to the wiki. NAO!
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Code walkthroughs

Post by Roland_Yonaba »

Santos:
Image

Thanks for introducing this. Very useful, indeed, Feel like i'm going to use this.
Well, this is obvious, but there are stupid people that will think this is a magic trick that can load things from anywhere.
So maybe you can add only one single line to explain that using this requires to adopt a specific deployment for resources files in the project folder. ^^

Actually, vrld used a similar system for font loading, in Quickie.

Code: Select all

local fonts = setmetatable({}, {__index = function(t,k)
local f = love.graphics.newFont(k)
   rawset(t, k, f)
   return f
end })
Maybe you can tak about that, too.

EDIT : And oh, suggestion. Maybe you could host a page on your website, where you'll list all these useful lectures with proper redirection ?
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Code walkthroughs

Post by Santos »

Hehehe, I'm glad you think they're worthy of the wiki Lafolie! :nyu: I dunno though... there are some awesome tutorials which aren't linked to from the wiki, and these walkthroughs so far aren't completely LÖVE related. If someone who is more in tune with the vision for the wiki wants to add them though, that's totally cool with me!

And thanks Roland! :ultrahappy: That's a good point you make. I hope that the comments which are like "-- Creates an new image from 'img/rainbow.png' and draws it." explains it, but I didn't really explicitly explain it. I don't want to incorrectly estimate the reader's level of knowledge... it's not really a super basic piece of code, and I don't want to explain things which would be completely obvious to them. That said, I also don't want to incorrectly estimate the reader's level of knowledge the other way, and I'm completely okay if a lot of readers are like "*groan*, I know this!" if there are also some readers who are like "wow, didn't know that!".

So basically, thanks for pointing that out! I'll read through all these sometime while imagining I didn't write them and see what I feel needs to be changed.

That's an interesting system for loading fonts, it's like a less general version of the proxy function. It might even be cool to start off with explaining how that works, and then be like "but wouldn't it be cool if it could load ANYTHING?!?" and then the reader's like " :o Whaaaat, load ANYTHING?!?!" and then BAM! Hit 'em with Proxy.

And I dunno, I think this forum thread is good enough for now. Don't want to overengineer things. :D


recursiveRequire has been added, a function for recursively requiring Lua files in folders, inspired by functions made by Headchant and bartbes.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Code walkthroughs

Post by Roland_Yonaba »

Nice work!There's a similar recursive loader (though a bit more verbose) in kikito's battle cry. I loved this.

EDIT: Actually, I was wondering if the pattern matching string shouldn't be

Code: Select all

local pathWithoutDotLua = path:match("(.+)%.lua$")
In order to be more explicit.
I dunno, but, on Windows, we can't create a without without at least one character before the dot (.)
Anyway.

To be more general, this recursive loader assumes that all Lua files required add their contents in the global env, _G.
Might not be suitable to people (as me) who usually write their external *.lua in a manner they keep their own stuff local (private) and then yield what is publicly needed in the main part. :P
Post Reply

Who is online

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