Make table "love" local only to main.lua

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Taki
Prole
Posts: 1
Joined: Wed May 04, 2022 12:34 pm

Make table "love" local only to main.lua

Post by Taki »

Hi, this is my first question on the forum :awesome:.

I want to prevent lua files outside main.lua from accessing the love table. Example:

Code: Select all

-- main.lua
require "test"

Code: Select all

-- test.lua
love.graphics.setColor(1, 1, 1) 
-- return error:
--test.lua:1: attempt to index global 'love' (a nil value)
or

Code: Select all

-- test.lua
print(love) -- return nil
I tried to use lua sandbox https://github.com/kikito/lua-sandbox, but even the cited examples don't work.
If it is possible to do this, how can I do it?
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: Make table "love" local only to main.lua

Post by MrFariator »

The simplest method might be

Code: Select all

-- at the top of main.lua
local love = love
_G.love = nil
Because the love api is wrapped in a single table, assigning a reference into a local variable and then setting the global to nil should prevent its use in other files.

The linked sandbox library should be able to achieve this, too, you'll just have to correctly pass the environment to use.

Little curious about what the specific reason or use-case might be.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests