Execute lua script in a user's folder

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.
Post Reply
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

Execute lua script in a user's folder

Post by louie999 »

So, example:
A person downloads my game then he wants to alter the game's gameplay a little bit then he goes to a folder in where he installed the game which is called something like "Scripts" then he puts a .lua file there then when he runs the game, the .lua script in the "Scripts" folder will be executed.

Will doing that be possible, if so, then how? Thanks in advance :)

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Execute lua script in a user's folder

Post by micha »

Hi,

first check out the functions in love.filesystem. These allow you to get a list of all files in a certain folder and to run lua-files.

love.filesystem only allows you to look for files in certain places, which is the game folder and the game's save directory. So you have two options: Either tell the user to place his lua-file in the save directory (you can display it with getSaveDirectory) or you distribute the game un-zipped, which means not as a .love file. In the latter case, the user can just put files into the game folder.
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

Re: Execute lua script in a user's folder

Post by louie999 »

Aw, so there's no way to somehow "create" a directory in the user's folder(where the user puts the game in) ? :?

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Execute lua script in a user's folder

Post by micha »

louie999 wrote:Aw, so there's no way to somehow "create" a directory in the user's folder(where the user puts the game in) ? :?
If you distribute the game "as is", in form of the source code (instead of packing it into a .love-file), then you can do it.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Execute lua script in a user's folder

Post by slime »

louie999 wrote:Aw, so there's no way to somehow "create" a directory in the user's folder(where the user puts the game in) ? :?
Not with love.filesystem – and even without it, operating systems (including Windows) tend to restrict where programs can write files, so for example programs generally can't write to anywhere in the Program Files folder unless the user explicitly allows it themselves.
User avatar
Calandriel
Prole
Posts: 39
Joined: Wed Apr 22, 2015 9:00 am

Re: Execute lua script in a user's folder

Post by Calandriel »

Maybe you can use ffi, but it's not recommended. I'm using it (muzz said that Slime wrote it)to set the write dir to wherever I want, using this function

Code: Select all

    function changeDirectory()
      local ffi = require("ffi")
     
      ffi.cdef[[ int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath); ]]; 
      ffi.cdef[[ int PHYSFS_setWriteDir(const char *newDir); ]]

      local liblove = ffi.os == "Windows" and ffi.load("love") or ffi.C

      -- This grabs your source folder, but you can point it anywhere, 
      --Look at love.filesystem to see what you can call so that you can keep it multiplatform. 
      local docsdir = love.filesystem.getSourceBaseDirectory()

      liblove.PHYSFS_setWriteDir(docsdir)
      liblove.PHYSFS_mount(docsdir, nil, 0)

    end
. but I don't know if it works to read files. There are ffi PHYSFS functions that cand read files and save it in Strings, but you will need know a little bit of C, (Which is a problem to me, heh). You might can do this, but it won't be easy(But if you do it, share the code, I'm new to that ffi thing, too :) )
If violence is not solving all your problems, You're simply not using enough of it.
Twitter: https://twitter.com/Calandriell Some news about wtf I'm doing.
Tumblr:https://www.tumblr.com/blog/calandriell I use to draw some random stuff.
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Execute lua script in a user's folder

Post by Muzz »

Yeah as i said in your other thread, i think it's good if you want to make your game install completely portable, but if you are making a game, and not a tool or something else like that i would use Love's intended functionality.
Post Reply

Who is online

Users browsing this forum: K2-XT and 134 guests