Isolating dofile() from love.* functions?

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

Isolating dofile() from love.* functions?

Post by Flamore »

Let's say i've got a variable like "score" that equals zero. and if i dofile i want the dofile function to be only able to access variables from main.lua and not from the rest of the Love2d engine. I've saw setfenv but it isn't really that straight forward.
User avatar
AuahDark
Party member
Posts: 107
Joined: Mon Oct 23, 2017 2:34 pm
Location: Indonesia
Contact:

Re: Isolating dofile() from love.* functions?

Post by AuahDark »

First, dofile doesn't really work in real scenarios. For your real solutions, you can use love.filesystem.load(file), setfenv the resulting function, and call it.
Profile. Do you encounter crashes in LÖVE Android and wanna send me logcats? Please hit me up in LÖVE Discord and send the full logcat file!
Flamore
Prole
Posts: 11
Joined: Sat Mar 14, 2020 10:19 pm

Re: Isolating dofile() from love.* functions?

Post by Flamore »

AuahDark wrote: Wed Mar 18, 2020 11:58 pm First, dofile doesn't really work in real scenarios. For your real solutions, you can use love.filesystem.load(file), setfenv the resulting function, and call it.
Alright so i merged the example setfenv function with lua.filedropper

Code: Select all

-- EXECUTE CODE
function love.filedropped(file)
	local ext
	ext = file:getExtension( )
	if ext == "cart" then
		executelua(file:getFilename())
	else
		print("Filetype not supported")
	end
end
function executelua(fn)
  -- read and parse the file
  local func, err = love.filesystem.load(fn)
  if not func then
    -- syntax error
    return false, err
  end
  -- lock out the environment
  setfenv(func, {})
  -- execute
  return pcall(func)
end
then i drop the cart file with no output or input results.

Code: Select all

game_title = "RGB palette and input demo."
resizescreen(1)
function gamedraw()
	if up == true then setpixel(2,1,8,lcdt) else setpixel(2,1,2,lcdt) end
	if down == true then setpixel(2,3,8,lcdt) else setpixel(2,3,2,lcdt) end
	if left == true then setpixel(1,2,8,lcdt) else setpixel(1,2,2,lcdt) end
	if right == true then setpixel(3,2,8,lcdt) else setpixel(3,2,2,lcdt) end
	if fire == true then setpixel(4,4,8,lcdt) else setpixel(4,4,2,lcdt) end
	for i=0, 15 do
		setpixel(1,i-1,i,lcd)
	end
end
function gameupdate()
end
all the functions just set values to an array that is drawn like a virtual screen
User avatar
AuahDark
Party member
Posts: 107
Joined: Mon Oct 23, 2017 2:34 pm
Location: Indonesia
Contact:

Re: Isolating dofile() from love.* functions?

Post by AuahDark »

For your example code, neither dofile and love.filesystem.load fits the scenario. You can first read all the contents, pass the file contents to "loadstring" function which returns same result as love.filesystem.load, setfenv the resulting function, then call it.
Profile. Do you encounter crashes in LÖVE Android and wanna send me logcats? Please hit me up in LÖVE Discord and send the full logcat file!
Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests