Page 1 of 1
[SOLVED] Executing Lua code from external file in LÖVE?
Posted: Sat Mar 14, 2020 10:45 pm
by Flamore
So, I've made a small let's say "boilerplate" in LÖVE with variables, custom screen buffer and custom screen buffer functions. The question is, is it possible to like drag n drop a file into the executable and have the main application execute it like emulator a rom? Will it be still possible to access the functions and screen buffer? If so, how.
Re: Executing Lua code from external file in LÖVE?
Posted: Sun Mar 15, 2020 1:55 am
by Varkas
This page shows several methods:
https://www.lua.org/pil/8.html
"dofile" will execute the file right away
"loadfile" will load it and hand you a function that you can call
"loadstring" does the trick if you have the code as text and not as file
Edit: Accessing global variables should work.
Re: Executing Lua code from external file in LÖVE?
Posted: Sun Mar 15, 2020 7:51 am
by zorg
And on the löve side, it does have the love.filedropped callback if you rather drop a file onto the already running löve project's window.
Re: Executing Lua code from external file in LÖVE?
Posted: Sun Mar 15, 2020 9:49 am
by Flamore
Alright, gonna try these methods. Also i have heard that dofile isn't secure, is it just like some bias or something
Re: Executing Lua code from external file in LÖVE?
Posted: Sun Mar 15, 2020 10:02 am
by zorg
technically, none of those are... but that depends on what you think "secure" is.
by the way, if you are using löve, two of those functions have "better" löve equivalents:
love.filesystem.load instead of loadfile;
dofile is basically the above, with an extra () at the end;
(better in terms of they will work even if you zip up your project into a .love file)
Re: Executing Lua code from external file in LÖVE?
Posted: Sun Mar 15, 2020 10:12 am
by Flamore
I just wrote a simple function
Code: Select all
function love.filedropped(file)
local ext
ext = file:getExtension( )
if ext == "cart" then
dofile(file)
else
print("Filetype not supported")
end
end
But yeth dofile() does not support FileData just strings. Giving this error below.
Code: Select all
Error: main.lua:240: bad argument #1 to 'dofile' (string expected, got userdata)
stack traceback:
[string "boot.lua"]:637: in function <[string "boot.lua"]:633>
[C]: in function 'dofile'
main.lua:240: in function <main.lua:236>
[string "boot.lua"]:503: in function <[string "boot.lua"]:493>
[C]: in function 'xpcall'
Re: Executing Lua code from external file in LÖVE?
Posted: Sun Mar 15, 2020 11:02 am
by pgimeno
Try with dofile(file:getFilename())
Re: Executing Lua code from external file in LÖVE?
Posted: Sun Mar 15, 2020 12:16 pm
by Flamore
pgimeno wrote: ↑Sun Mar 15, 2020 11:02 am
Try with dofile(file:getFilename())
Thank you, it works like a charm. but is it possible to stop the execution like reset the sandbox?
Re: Executing Lua code from external file in LÖVE?
Posted: Sun Mar 15, 2020 1:15 pm
by pgimeno
You can restart with
love.event.quit("restart")