## Running Require() Multiple Times With Different Results

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Baggef
Prole
Posts: 20
Joined: Fri Aug 23, 2019 7:30 pm
Location: USA
Contact:

### Running Require() Multiple Times With Different Results

After doing some reading I found that require() only runs once, and stores that value in a table (some say package.loaded and others say _LOADED.) I am using a separate lua file as a sort of class file to be loaded and added to different tables. The point is that the tables all should get their own copies of the table returned by the lua file.

The problem seems to be that they all get the same copy, as the first require() is reading from the file and storing the value in package.loaded. When the next one needs a copy, require() just points to the first copy and says, "Well I've already done my work, you guys need to learn how to share." But of course this does not work out very well.

I have tried setting package.loaded['name of file.lua'] to nil, but that just deletes the copy after it's made.

Is there a better method? I have read about dofile() and it does work for what I want, although I need to supply it a full path which I don't like doing. If there is a mix between dofile() and require() that suits my needs, am I wasting memory by making copies of the script? I'm mainly asking because require() doesn't read twice in the first place.

TLDR I need a way to run an external lua script multiple times and get the new return value every time.
gaeem defelpmint is maiy pashin
Itch: Freakman

4vZEROv
Citizen
Posts: 94
Joined: Wed Jan 02, 2019 8:44 pm

### Re: Running Require() Multiple Times With Different Results

pgimeno
Party member
Posts: 2340
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Running Require() Multiple Times With Different Results

Or return a function that creates the table, rather than a table.

As far as I know, _LOADED is not present in LuaJIT 2.0, as it's a Lua 5.0 thing. Note that the PIL book freely available online applies to Lua 5.0. There are very few differences with Lua 5.1 which LuaJIT is compatible with, but you've run into one.

Baggef
Prole
Posts: 20
Joined: Fri Aug 23, 2019 7:30 pm
Location: USA
Contact:

### Re: Running Require() Multiple Times With Different Results

pgimeno wrote:
Tue Nov 05, 2019 5:34 pm
Or return a function that creates the table, rather than a table.
Wow, thank you. I didn't think it would be that simple. I was really about to write a version of require() without the "no double reading" rule.
gaeem defelpmint is maiy pashin
Itch: Freakman

pgimeno
Party member
Posts: 2340
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Running Require() Multiple Times With Different Results

You're welcome. I wish I could credit the first person who noted that simple solution to me, but I can't recall who that was

It's also useful for returning multiple values, as (ab)used in my 'ns' template: https://love2d.org/forums/viewtopic.php ... ck#p230163

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Running Require() Multiple Times With Different Results

Are you sure you're not trying to use "require()" in place of "class.new()"? Because beyond that, I can't think of any reason to do this, and even that's pretty backwards.

Baggef
Prole
Posts: 20
Joined: Fri Aug 23, 2019 7:30 pm
Location: USA
Contact:

### Re: Running Require() Multiple Times With Different Results

raidho36 wrote:
Tue Nov 05, 2019 9:38 pm
Are you sure you're not trying to use "require()" in place of "class.new()"? Because beyond that, I can't think of any reason to do this, and even that's pretty backwards.
I'm trying to organize things so that I can reuse files in multiple projects. Such as an animator that exists in an animator.lua. I can just call a simple function to add it to any object

Code: Select all

player.add('animator')
player['animator'].newAnim("run", love.graphics.newImage("assets/oldHero.png"), 32, 32, 0.3)

It adds it by using the require() function

Code: Select all

-- Function part from pgimeno
ent[name] = require(name)()

But yeah this is the way I thought it would work well. I'm pretty new to this so it's probably a dumb way of doing things
gaeem defelpmint is maiy pashin
Itch: Freakman

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Running Require() Multiple Times With Different Results

Sounds like you want ECS or mixin OOP. Require works the way it does because it's specifically created to load Lua modules, obviously a module definition code doesn't need to run any more than exactly once but it probably will get fetched much more than exactly once. Somewhere along these lines there is also the reason why require doesn't accept file paths, but rather module/submodule names. And why it can load Lua DLLs, and C++ host-defined modules. It has quite a bit of module-specific functionality.

Baggef
Prole
Posts: 20
Joined: Fri Aug 23, 2019 7:30 pm
Location: USA
Contact:

### Re: Running Require() Multiple Times With Different Results

raidho36 wrote:
Wed Nov 06, 2019 10:38 am
Sounds like you want ECS or mixin OOP.
Hmm. I know of OOP libraries like middleclass, but I didn't realize lua could use ECS. I'm not too familiar with the concept but I've found a few libraries for ECS like tiny-ecs and Entitas ECS. I could just barely understand ECS while using C# but it may be nice to try something new.
gaeem defelpmint is maiy pashin
Itch: Freakman