LUA file system.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Palmar
Prole
Posts: 23
Joined: Thu Dec 13, 2012 3:54 pm

LUA file system.

Post by Palmar »

Hi everyone. Not so long time ago i started developing wih LUA and Love.
I am trying to make a little tile editor for game, using LOVE. I created all main functionality, but now i want to save/load array, which is used for editing. Forum search did'nt help.
After pressing Save/Load it must be opened dialog winidw, like this, which is used for saving. All what i need is name of right functions or little help. Thanks in advance.
Image
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: LUA file system.

Post by Roland_Yonaba »

Hi Palmar.

A kind of file explorer, huh ?
Actually, Löve do not provide such thing.
Fact is, this would require first a GUI. And Löve don't have that feature. You can write your own GUi, or just use any of the existing ones. As far as I know, the most polished one are (in no particular order) Quickie, Gspöt, and the delicious LöveFrames. Plus, their authors are constantly hanging around and are very responsive, so you can have some help if you happen to get stuck with any of them.

Second, even with those existing Gui, you won't have what you require directly. But, you can hopefully work around that and implement your own one, as love.filesystem provides the core functions for that. love.filesystem.enumerate could be used to provide the list of files in a folder, and Lua's string pattern matching can be used to apply file type filters, for instance. But it has some limitations :it can access files in the game folder and the save folder... (in AppData directory, actually). So keep that in mind.

I would also like to add that they were a previous attempt at doing something similar, see this topic : LoveFS
Palmar
Prole
Posts: 23
Joined: Thu Dec 13, 2012 3:54 pm

Re: LUA file system.

Post by Palmar »

Roland_Yonaba wrote:Hi Palmar.

A kind of file explorer, huh ?
Actually, Löve do not provide such thing.
Fact is, this would require first a GUI. And Löve don't have that feature. You can write your own GUi, or just use any of the existing ones. As far as I know, the most polished one are (in no particular order) Quickie, Gspöt, and the delicious LöveFrames. Plus, their authors are constantly hanging around and are very responsive, so you can have some help if you happen to get stuck with any of them.

Second, even with those existing Gui, you won't have what you require directly. But, you can hopefully work around that and implement your own one, as love.filesystem provides the core functions for that. love.filesystem.enumerate could be used to provide the list of files in a folder, and Lua's string pattern matching can be used to apply file type filters, for instance. But it has some limitations :it can access files in the game folder and the save folder... (in AppData directory, actually). So keep that in mind.

I would also like to add that they were a previous attempt at doing something similar, see this topic : LoveFS
Thank you very much. I tried to do something using love.filesystem , but:

"This module provides access to files in two places, and two places only:
The root folder of the .love archive (or source directory)
The root folder of the game's save directory. "

This is not suitable for me. Thanks.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: LUA file system.

Post by Roland_Yonaba »

Can you please give more details on what's the intent ?
Maybe there's a way to work around that limitation, which is actually suitable for most of Löve users...
How do you want to mange your saved/loaded maps with your TileEditor ?

I'll also mention that some people have already worked on similar stuff (I mean, TileEditors), and they found the way to manage with the actual love.filesystem. :awesome:
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: LUA file system.

Post by Kadoba »

Using Lua's IO library you can write files to absolute locations. The problem is that there isn't a method to iterate over directories. I believe you can get around this limitation using io.popen to execute an OS list directory command and get the output. For Linux and OSX the command is "ls" and for windows it's "dir".

I found this implementation on stack overflow:
I hate having to install libraries (especially those that want me to use installer packages to install them). If you're looking for a clean solution for a directory listing on an absolute path in Lua, look no further.

Building on the answer that sylvanaar provided, I created a function that returns an array of all the files for a given directory (absolute path required). This is my preferred implementation, as it works on all my machines.

Code: Select all

-- Lua implementation of PHP scandir function
function scandir(directory)
    local i, t, popen = 0, {}, io.popen
    for filename in popen('ls -a "'..directory..'"'):lines() do
        i = i + 1
        t[i] = filename
    end
    return t
end
If you are using Windows, you'll need to have a bash client installed so that the 'ls' command will work - alternately, you can use the dir command that sylvanaar provided:

Code: Select all

'dir "'..directory..'" /b /ad'
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: LUA file system.

Post by Roland_Yonaba »

Hey, thanks for that tip, kadoba.
Actually I was thinking that Lua's IO was deprecated with Löve 0.8.0 (as some file writing operations with io were causing Löve to hang up, then crash...in my earlier experiments). But this one worked flawlessly.
Palmar
Prole
Posts: 23
Joined: Thu Dec 13, 2012 3:54 pm

Re: LUA file system.

Post by Palmar »

Sorry for silly request, but can somebody give me a source of saving using IO? Little exapmle, how to do it. I tried, but it didn't work.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: LUA file system.

Post by Roland_Yonaba »

File writing with Lua IO is as simple as follows (see the Lua refman for more details on lua 5.1 IO facilities).

Code: Select all

local file = assert(io.open('aFile.txt','w'),'Could not create "aFile.txt"')
file:write('Something')
file:close()
But, actually, as I said in a previous post, it won't work with Löve 0.8.0, as file writing with Lua IO seems to be deprecated.
But you won't get any problem running this from Love 0.7.2, for instance, and the resulting file would be located near the project folder.
User avatar
slime
Solid Snayke
Posts: 3160
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: LUA file system.

Post by slime »

Roland_Yonaba wrote:But, actually, as I said in a previous post, it won't work with Löve 0.8.0, as file writing with Lua IO seems to be deprecated.
But you won't get any problem running this from Love 0.7.2, for instance, and the resulting file would be located near the project folder.
LÖVE does not modify, remove, or deprecate any standard Lua library or function. What OS are you on? Are you using standard 0.8.0 or a LuaJIT-compiled version? Can you provide a .love test case?
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: LUA file system.

Post by Roland_Yonaba »

Windows 7, x32 for me. Using a standard version of love, this specific one. Am I missing something ?
Here's the example (see *.love attached). With Love 0.7.2 I won't have any problem. But with Love 0.8.0, I just get this screen.
@Palmar: So maybe you should try it and see if it creates a text file. If so, then I might be totally wrong.
Attachments
io.love
(247 Bytes) Downloaded 160 times
t.jpg
t.jpg (13.06 KiB) Viewed 5605 times
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests