Page 3 of 4

Re: Code walkthroughs

Posted: Wed Aug 29, 2012 10:04 am
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

Re: Code walkthroughs

Posted: Wed Aug 29, 2012 10:14 am
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).

Re: Code walkthroughs

Posted: Wed Aug 29, 2012 10:30 am
by Santos
Ah, I see, thanks! I'll have to update it (and/or replace everything with kikito's require_tree.lua).

Re: Code walkthroughs

Posted: Wed Aug 29, 2012 12:16 pm
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:

Re: Code walkthroughs

Posted: Wed Aug 29, 2012 3:23 pm
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.

Re: Code walkthroughs

Posted: Wed Aug 29, 2012 4:06 pm
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!)

Re: Code walkthroughs

Posted: Wed Aug 29, 2012 4:21 pm
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.

Re: Code walkthroughs

Posted: Fri Sep 07, 2012 12:15 am
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:

Re: Code walkthroughs

Posted: Fri Sep 07, 2012 7:06 am
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).

Re: Code walkthroughs

Posted: Fri Sep 07, 2012 9:52 am
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 ?