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.
I have a few questions about this
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.
Running Require() Multiple Times With Different Results
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Running Require() Multiple Times With Different Results
gaeem defelpmint is maiy pashin
Itch: Freakman
Itch: Freakman
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.
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.
Re: Running Require() Multiple Times With Different Results
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
Itch: Freakman
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
It's also useful for returning multiple values, as (ab)used in my 'ns' template: https://love2d.org/forums/viewtopic.php ... ck#p230163
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.
Re: Running Require() Multiple Times With Different Results
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)
Code: Select all
-- Function part from pgimeno
ent[name] = require(name)()
gaeem defelpmint is maiy pashin
Itch: Freakman
Itch: Freakman
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.
Re: Running Require() Multiple Times With Different Results
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
Itch: Freakman
Who is online
Users browsing this forum: Google [Bot] and 2 guests