[SOLVED] Executing Lua code from external file in LÖVE?

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
Flamore
Prole
Posts: 11
Joined: Sat Mar 14, 2020 10:19 pm

[SOLVED] Executing Lua code from external file in LÖVE?

Post 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.
Last edited by Flamore on Thu Mar 19, 2020 5:48 pm, edited 1 time in total.
User avatar
Varkas
Citizen
Posts: 83
Joined: Mon Mar 09, 2020 2:26 pm

Re: Executing Lua code from external file in LÖVE?

Post 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.
In soviet russia, code debugs you.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Executing Lua code from external file in LÖVE?

Post 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.
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.
Flamore
Prole
Posts: 11
Joined: Sat Mar 14, 2020 10:19 pm

Re: Executing Lua code from external file in LÖVE?

Post by Flamore »

Alright, gonna try these methods. Also i have heard that dofile isn't secure, is it just like some bias or something
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Executing Lua code from external file in LÖVE?

Post 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)
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.
Flamore
Prole
Posts: 11
Joined: Sat Mar 14, 2020 10:19 pm

Re: Executing Lua code from external file in LÖVE?

Post 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'
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Executing Lua code from external file in LÖVE?

Post by pgimeno »

Try with dofile(file:getFilename())
Flamore
Prole
Posts: 11
Joined: Sat Mar 14, 2020 10:19 pm

Re: Executing Lua code from external file in LÖVE?

Post 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?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Executing Lua code from external file in LÖVE?

Post by pgimeno »

You can restart with love.event.quit("restart")
Post Reply

Who is online

Users browsing this forum: slime and 160 guests