Saving files in game directory

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
RednibCoding
Prole
Posts: 8
Joined: Fri Nov 10, 2017 7:31 pm

Saving files in game directory

Post by RednibCoding »

Hello guys,

im new to love (but not new to programming and game development) and i was wondering how i can save files into the game directory. Lets say i want to make a game/scene/map editor for my game for faster development and want to save the created scenes/maps in the scenes/map directory of the game from the editor. Also i want the game to load them when needed with the same load routine the editor uses from the scene/map directory.
Basically, how you do it in ANY OTHER game engine/framework what ever. :)
But love only alows you to save files to %appdata%. Thats pretty dumb because when you publish your game, you want all the game resources including map and scene files to be in your games directory. So the scene and map files has to go into the game's scene/map directory. Ontop of that, love blockes the usage of the standard lua io.

I've postet my tilemap and scene lib on github so you can see what i mean.
https://github.com/RednibCoding?tab=repositories

Hope someone can help me as im a bit confused by that.

Thanks
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Saving files in game directory

Post by zorg »

RednibCoding wrote: Fri Nov 10, 2017 10:50 pmim new to love (but not new to programming and game development) and i was wondering how i can save files into the game directory.
That's a tricky question for a few reasons; the basic answer would be that you can't do that with love.filesystem functions.
RednibCoding wrote: Fri Nov 10, 2017 10:50 pmLets say i want to make a game/scene/map editor for my game for faster development and want to save the created scenes/maps in the scenes/map directory of the game from the editor.
You'd save them in, let's say, <savedir>/scenes/map instead; to löve, both the save and game directories are mapped onto the same virtual root, and the savedir contents get precedence if files of the same name exist in both places, since that makes updates and such easier.
RednibCoding wrote: Fri Nov 10, 2017 10:50 pmAlso i want the game to load them when needed with the same load routine the editor uses from the scene/map directory.
As i said above, because of the virtual root, it shouldn't be an issue.
RednibCoding wrote: Fri Nov 10, 2017 10:50 pmBasically, how you do it in ANY OTHER game engine/framework what ever. :)
The blame mostly goes to the fact that OS-es nowadays tend to make almost all folders inaccessible to programs for various reasons.
RednibCoding wrote: Fri Nov 10, 2017 10:50 pmBut love only alows you to save files to %appdata%.
Somewhat true, but technically you could get around that, with luaJIT's FFI and the internal PhysFS' not exposed functions to set the write directory (of which you can only have one at a time; that's how PhysFS works).
RednibCoding wrote: Fri Nov 10, 2017 10:50 pmThats pretty dumb because when you publish your game, you want all the game resources including map and scene files to be in your games directory.
When you publish a game, you might do that in the form of a zip archive renamed to the '.love' extension (instead of sharing the folder with a .bat file or something) or even fused with love.exe (for which you'll also need to supply the .dll files next to it); in that case, the "source base" directory will be the archive itself, not the folder it's actually in; you cannot really modify anything in the archive in that case.
RednibCoding wrote: Fri Nov 10, 2017 10:50 pmSo the scene and map files has to go into the game's scene/map directory.
As i said before, virtual root, folders in the two directories can overlap each other internally, so it will try to load things from both places.
RednibCoding wrote: Fri Nov 10, 2017 10:50 pmOntop of that, love blockes the usage of the standard lua io.
No it doesn't; the only thing stopping you from using lua's io is the fact that it's hard to code with it in a cross-platform manner; you are free to use it though. (you do need to do some acrobatics with how you load in love-related Data though)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Saving files in game directory

Post by ivan »

Zorg's answer is pretty much spot on.
But love only alows you to save files to %appdata%. Thats pretty dumb because when you publish your game, you want all the game resources including map and scene files to be in your games directory.
Sure, you can still load resources from the game directory.
Suppose the game directory is located on a CD/DVD or a read-only flash drive?
Or what if you are on Windows and have multiple accounts set up - you don't want all of the users saving to the same place, right?
There are so many good reasons to use the %appdata% folder that I can't even list them all.
Basically, how you do it in ANY OTHER game engine/framework what ever. :)
By loading/saving user files exclusively inside the %appdata% folder.
If loading from the %appdata% folder fails, you can always fallback to loading from the game's directory.
Alternatively, you can just copy the "default" maps or files to the %appdata% folder during install.
RednibCoding
Prole
Posts: 8
Joined: Fri Nov 10, 2017 7:31 pm

Re: Saving files in game directory

Post by RednibCoding »

There are so many good reasons to use the %appdata% folder that I can't even list them all.
Yea that might be the case for saves and userdata that are made during runtime of the game but not for game resources like images, sounds, scene files, map files and so on. They are static and should'nt be changed in any way. Because they are part of the core game. And copying core files to %appdata% just so they can be loaded during runtime sounds like a bad workaround to me.
RednibCoding wrote: ↑So the scene and map files has to go into the game's scene/map directory.
As i said before, virtual root, folders in the two directories can overlap each other internally, so it will try to load things from both places.
So if i understand that correctly, i can save the map files from my scene editor to /scenes/map directory and it will look if a file structure exist in both places, if such a structure exist in the game directory but not in %appdata% it will save it to games directory. So it will do it with loading...

Just to clarify, the editor is just for developing the game, so its handy to save the map files to the games directory during development and fire up the game to see and test the map ingame. After the game developing phase is done, and game is finished, maps should'nt be changed anymore and maps and other recources are core files of the game, so the game should load a map file as it would, lets say, load an image from the game's directory.
Why can i load an image file without any problems from the games directory let's image1.png with newImage() but not a let's say world1.map file defined by me (where i have written the loading routine let's say with loadMap())

That's what confuses me.
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Saving files in game directory

Post by MrFariator »

I also faced this "problem" when developing my game's map editor, and I realized that %appdata% is your friend, not your foe.

What I wound up doing was including an additional step in my build script(s): look for the "official" map levels in %appdata%, copy those files into the game's own directory and then finally create the fused love2d game app. This methodology allows someone with the app (and final set of levels) apply edits to them, without harming the original level file in the process, unless they go out of their way to copy-and-paste from %appdata% to the game folder (and someone will, I'm sure). Also makes it easier to compare changes and see what sticks/works better between the two versions.

Works regardless if you want to disable the editor from end users or not.
RednibCoding
Prole
Posts: 8
Joined: Fri Nov 10, 2017 7:31 pm

Re: Saving files in game directory

Post by RednibCoding »

That sounds like a good step.
But how do i load the map files from the game's directory? I do not mean the code implementation but the path. How do i tell love that i want to load the map file from the game's map folder.

I come from frameworks/languages where i could just load anything from anywhere and never ran into any troubles (as long as you know what you are doing). And you definitly should be able to load core game resources for your game other than images and sounds from the game folder. I mean, map or scene files definitly belong to the game core like for example player images do, and not to the user data that has to go in %appdata%.
This %appdata% thing feels more like an obstacle than a feature.
As for example when you want to deploy a game to Android it needs to have the read/write access to be set anyway. Thats done by so many Android game's/apps. So whats the point love implementing such (to me sensless) restrictions. I mean i like love because it is so "barebones" so you can code everything from scatch in your own way and needs like scene management, entity system, input handling.. Basically everything... nothing like that is predifined, love just gives you the right set of tools to do it easily, so the engine does not force the user to use stuff the way the engine wants to.
And then this restriction with %appdata% that totally does'nt make sense to me because this does'nt follow that red line love2d follows.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Saving files in game directory

Post by grump »

RednibCoding wrote: Sat Nov 11, 2017 12:55 pmI mean i like love because it is so "barebones" so you can code everything from scatch in your own way
You can also code file access from scratch the way you want it, either through the io API, by using OS functions through ffi or by using a third party lib like LuaFileSystem.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Saving files in game directory

Post by zorg »

Apologies beforehand for the gigantic post...
RednibCoding wrote: Sat Nov 11, 2017 9:01 amJust to clarify, the editor is just for developing the game, so its handy to save the map files to the games directory during development and fire up the game to see and test the map ingame. After the game developing phase is done, and game is finished, maps should'nt be changed anymore and maps and other recources are core files of the game, so the game should load a map file as it would, lets say, load an image from the game's directory.
I know that's your angle; you're not the first that had this kind of problem, actually.

First, if you have separate projects for the game and the editor, but both have the same 'identity' as löve calls it, either by setting it manually or by editing a conf.lua, both projects (if both or neither are fused) will use the same save directory.
RednibCoding wrote: Sat Nov 11, 2017 9:01 amWhy can i load an image file without any problems from the games directory let's image1.png with newImage() but not a let's say world1.map file defined by me (where i have written the loading routine let's say with loadMap())
That's what confuses me.
Both can load maps or any other files from that save directory under any circumstances, not just images, and the same thing is true for the source directory, if the files are already there, you just can't write there. The source base directory is where your main.lua file is, whether or not you have it in a folder, in a zip archive with the .love extension (or with just .zip), or if it's fused with löve.exe.
MrFariator wrote: Sat Nov 11, 2017 11:17 amWhat I wound up doing was including an additional step in my build script(s): look for the "official" map levels in %appdata%, copy those files into the game's own directory and then finally create the fused love2d game app. This methodology allows someone with the app (and final set of levels) apply edits to them, without harming the original level file in the process, unless they go out of their way to copy-and-paste from %appdata% to the game folder (and someone will, I'm sure). Also makes it easier to compare changes and see what sticks/works better between the two versions.
Works regardless if you want to disable the editor from end users or not.
I mean, what i'm not understanding is the premise that, since you're the dev, you should not give a wooden nickle about whether your editors dump files into the save folder in appdata or not, since you can always move those manually at once after everything works as you want, and you want to release the game with those files in the game's source folder hierarchy;

heck, you can even have the game itself copy out those files from the source to the save directory when the user starts up the program, if you want them to be able to edit the maps and stuff, without affecting the original files in the source! (because of what i said in my earlier post, the savedir files get priority when finding two files with the same name and path in both directories: file exists in savedir? load potentially modified map, if itdoesn't or if it was deleted, then load default from source.)
RednibCoding wrote: Sat Nov 11, 2017 12:55 pmThat sounds like a good step.
But how do i load the map files from the game's directory? I do not mean the code implementation but the path. How do i tell love that i want to load the map file from the game's map folder.
As i said, both directories map to the same root; now, there is a function to get the real path, so that you can see where a file truly is, but the precedence rule still applies; if a file is in both places, the savedir one will get loaded.

A bit more advanved, but if you FFI in PhysFS, which löve uses, you can re-map the save folder and the sourcebase directory to two sub-directories, for the following virtual tree:

Code: Select all

/
|-usr
|-src
folder names are what you give them, doesn't matter; point is, in those folders, the files will now truly be separate.
RednibCoding wrote: Sat Nov 11, 2017 12:55 pmThat sounds like a good step.I come from frameworks/languages where i could just load anything from anywhere and never ran into any troubles (as long as you know what you are doing).
And you can! There are always way to do anything; as i stated previously, löve doesn't block you from using lua io and such, and you can mess with PhysFS as well, or get luafilesystem (though that'll have portability issues too, i think) if you want to.
RednibCoding wrote: Sat Nov 11, 2017 12:55 pmAnd you definitly should be able to load core game resources for your game other than images and sounds from the game folder. I mean, map or scene files definitly belong to the game core like for example player images do, and not to the user data that has to go in %appdata%.
I think this is still a misconception on your end, since all we're saying is that you can't edit files in the source directory programmatically, not that you can't have every asset in there. :c
RednibCoding wrote: Sat Nov 11, 2017 12:55 pmThis %appdata% thing feels more like an obstacle than a feature.
As for example when you want to deploy a game to Android it needs to have the read/write access to be set anyway. Thats done by so many Android game's/apps. So whats the point love implementing such (to me sensless) restrictions. I mean i like love because it is so "barebones" so you can code everything from scatch in your own way and needs like scene management, entity system, input handling.. Basically everything... nothing like that is predifined, love just gives you the right set of tools to do it easily, so the engine does not force the user to use stuff the way the engine wants to.
And then this restriction with %appdata% that totally does'nt make sense to me because this does'nt follow that red line love2d follows.
Look, for what it's worth, i'm a bit salty about it as well, and to tell you the truth, i am working on a solution myself that i'll share whenever i get around to finish coding it, and testing it on various OS-es. I'm not exactly the model computer user the current year expects; i have my own PC that only has me as an user, my account is the admin account because fuck restrictions, i takeown-d my whole C drive so the OS doesn't prevent me from deleting bloated stuff they think are necessary features, and i'm experienced enough to not get viruses and shit without a firewall and an antivirus. I'm not boasting, all those things are horrible from a general and comp.-security perspective, probably; just saying that to me, logically, all apps should have their own folder as writeable by them, at the very least, and to not force me to "install" things in C:Program Files (x86 or not)... but i digress. :crazy:

Also, what grump said.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
RednibCoding
Prole
Posts: 8
Joined: Fri Nov 10, 2017 7:31 pm

Re: Saving files in game directory

Post by RednibCoding »

Oh dear now i got it.

I thought filesystem.load/open will always and only look in %appdata% for the required file. So i was wondering how the heck do i load game core files like maps that are stored in the game directory (where the main.lua is located)

So to make things clear: when i want to load a file "world1.map" from the game's directory (where main.lua is) then it will look at first at %appdata% and if there is no such file, it will look in the game's directory and load it from there, right?
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Saving files in game directory

Post by zorg »

That's right!
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 90 guests