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".
require ""
And it's not completely impossible on Windows.
From the command line...
notepad .lua
Code walkthroughs
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Code walkthroughs
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 wrote:They both seem to work with require. Is there any difference?
Re: Code walkthroughs
Ah, I see, thanks! I'll have to update it (and/or replace everything with kikito's require_tree.lua).
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Code walkthroughs
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 })
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 })
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.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Code walkthroughs
ThanksRoland_Yonaba wrote:Nice work!There's a similar recursive loader (though a bit more verbose) in kikito's battle cry. I loved this.
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: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.
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.Roland_Yonaba wrote: I dunno, but, on Windows, we can't create a without without at least one character before the dot (.)
Anyway.
When I write def I mean function.
Re: Code walkthroughs
Happy 500th post!
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!)
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
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.Santos wrote:Happy 500th post!
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
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...
Bartbes the inifile was exactly what I needed for something later on, glad to randomly come across it...
Re: Code walkthroughs
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).
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).
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Code walkthroughs
Actually it already does thanks to the metatable's __index metamethodAnickyan 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).
Code: Select all
function Proxy(f)
return setmetatable({}, {__index = function(self, k)
local v = f(k)
self[k] = v
return v
end})
end
Who is online
Users browsing this forum: No registered users and 3 guests