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 »

Thanks! ^^

kikito's require_tree.lua looks really cool!

It's interesting how it changes the "/"s in the path to "."s before it's required (the reason for the "/"s in the first place is because love.filesystem.isDirectory works with "/"s and not "."s). They both seem to work with require. Is there any difference?

And I was thinking the same thing about that pattern, but apparently you can actually load a file with the name ".lua". :ultraglee:
require ""

And it's not completely impossible on Windows. ;)

From the command line...
notepad .lua
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 »

Santos wrote:They both seem to work with require. Is there any difference?
Yes, . is the correct way, / only works because of the way the strings are interpreted by require, so this is an implementation detail (shared by love and lua).
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Code walkthroughs

Post by Santos »

Ah, I see, thanks! I'll have to update it (and/or replace everything with kikito's require_tree.lua).
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 »

Code: Select all

local fonts = setmetatable({}, {__index = function(t,k)
local f = love.graphics.newFont(k)
   rawset(t, k, f) -- not that much required, could simply be t[k] = f
   return f
end })
Folks,
Speaking of... The snippet above will help to assign Font. But it copes only with Vera.ttf, Löve's default font, isn't it ?
So, I can make some slight adjusment to be able to load some other *.ttf fonts.

Code: Select all

local LoaderFontPath = 'fonts/'
local loader = {}	
loader.Font = setmetatable({}, {__index = function(self,k)
  local f = love.graphics.newFont(type(k) == 'number' and k or LoaderFontPath .. k ..'.ttf')
	self[k] = f
	return f
end })
It will load a *.ttf, but with a default size of 12...
So, could it be possible to tackle this ? So that we can load external ttf font while specifying the size ?
Just wondering...

Yay..500th post. :megagrin:
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Code walkthroughs

Post by kikito »

Roland_Yonaba wrote:Nice work!There's a similar recursive loader (though a bit more verbose) in kikito's battle cry. I loved this.
Thanks :)
Roland_Yonaba wrote: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
That's the main reason my require_tree is a bit more verbose. It builds a (local) table with the results of loading each file. If you load, for example, a folder that has a subfolder called fruits, with two files, fruits/lemon.lua and fruits/orange.lua, each returning something local, you get a table like this { fruits = { lemon = {...}, orange = {...} } }, which is very convenient.
Roland_Yonaba wrote: I dunno, but, on Windows, we can't create a without without at least one character before the dot (.)
Anyway.
Outside of windows, you create files begining with dots all the time. On windows, you might not be able to create them via the regular windows GUI, but you can probably create them via the console, or via raw code. I'm reasolably certain that I have seen them lying around on windows.
When I write def I mean function.
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Code walkthroughs

Post by Santos »

Happy 500th post! :ultraglee:

Hmmm... I'm not sure about a neat way to specify the file and the size... it's like you'd need an __index within an __index... __indexception.

recursiveRequire now uses dots instead of slashes, and Proxy now doesn't use rawset (I misunderstood what rawset did, thanks for pointing out that it wasn't necessary!)
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Code walkthroughs

Post by coffee »

Santos wrote:Happy 500th post! :ultraglee:

Hmmm... I'm not sure about a neat way to specify the file and the size... it's like you'd need an __index within an __index... __indexception.

recursiveRequire now uses dots instead of slashes, and Proxy now doesn't use rawset (I misunderstood what rawset did, thanks for pointing out that it wasn't necessary!)
I usually use the recursive function of the enumerate entry in Wiki (https://love2d.org/wiki/love.filesystem.enumerate). It's was for me very handy to get all level files, collect data and keep all in a directory structure. Is was a nice idea to use recursive function also to do requires. It's a question of taste do or not dot the requires because sometimes we don't need to load/require data and just get some info data and later load the level we really need. However targeting the level we want to require instead of file load it's also a good alternative.
User avatar
Codex
Party member
Posts: 106
Joined: Tue Mar 06, 2012 6:49 am

Re: Code walkthroughs

Post by Codex »

I love this thread. Bookmarking it for future reference.

Bartbes the inifile was exactly what I needed for something later on, glad to randomly come across it... :ultrahappy:
Anickyan
Prole
Posts: 27
Joined: Sun Aug 05, 2012 8:42 am

Re: Code walkthroughs

Post by Anickyan »

The Proxy function is very simple, but yet pretty genius. But why not check if a field in the table already exists before creating a new one. Or atleast take a boolean as a parameter for overwrite current field or not?

I know that you can easily implement this when copying the code, but for someone without alot of Lua experience, this might be better(because of the speed).
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 »

Anickyan wrote:The Proxy function is very simple, but yet pretty genius. But why not check if a field in the table already exists before creating a new one. Or atleast take a boolean as a parameter for overwrite current field or not?

I know that you can easily implement this when copying the code, but for someone without alot of Lua experience, this might be better(because of the speed).
Actually it already does thanks to the metatable's __index metamethod

Code: Select all

function Proxy(f)
    return setmetatable({}, {__index = function(self, k)
        local v = f(k)
        self[k] = v
        return v
    end})
end
Or am I missing something ?
Post Reply

Who is online

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