Page 1 of 1
Help with require()
Posted: Sat Jan 18, 2014 3:16 pm
by jag_e_nummer_ett
I was looking around in Lua.org's "Programming in Lua" and they showed that if you require a file you get a variable named _REQUIREDNAME
http://www.lua.org/pil/15.3.html
I tested with LÖVE 0.9 and it didn't work, no _REQUIREDNAME.
I bet LÖVE got its own custom require function but could they include this in an update?
Re: Help with require()
Posted: Sat Jan 18, 2014 3:31 pm
by bartbes
The PiL is sadly outdated, the module name gets passed as first vararg.
Re: Help with require()
Posted: Sat Jan 18, 2014 3:34 pm
by jag_e_nummer_ett
bartbes wrote:The PiL is sadly outdated, the module name gets passed as first vararg.
So like this?
Code: Select all
args = {...}
name = args[1]
or
name = ...
Yea that worked. The more you know...
Re: Help with require()
Posted: Mon Jan 20, 2014 5:28 am
by Simon6689
I need some help with require() too.
How do you require something twice?
Re: Help with require()
Posted: Mon Jan 20, 2014 5:42 am
by jag_e_nummer_ett
Simon6689 wrote:I need some help with require() too.
How do you require something twice?
At the site I posted in the main post content it says this "Remember that require avoids loading the same package multiple times."
So what you can do, if your file returns an object at the end you can save it twice. Example:
Code: Select all
-- file: myfile.lua
class = {}
class.num = math.random(255)
function class.test()
print(class.num)
end
return class
Code: Select all
-- file: main.lua
one = require "myfile"
two = require "myfile"
function love.load()
one.test()
two.test()
end
I do not know if that works, cant test it atm, but if that doesnt then have some function in it like class.new() or something
Re: Help with require()
Posted: Mon Jan 20, 2014 6:05 am
by Simon6689
It's not just two times, I need something that would re-run the file several times.
I have also tried using
but that only works if I paste in the absolute path of main.lua's directory like:
Code: Select all
loadfile("C:\\Users\\Simon\\Documents\\GitHub\\Game\\Game\\objects\\"..name..".lua")
which wont't work if I pack it into a .love, move it out of my documents or another person uses it.
Is there a way to get the path that main.lua is in?
Re: Help with require()
Posted: Mon Jan 20, 2014 6:33 am
by slime
You should use [wiki]love.filesystem.load[/wiki] if you want to load and run a Lua file multiple times.
However, there aren't many reasons to do that (aside from hot reloading of Lua tables or something) - if you actually just want to execute code in your Lua file multiple times, you should turn the code into a function and call the function multiple times instead.
Re: Help with require()
Posted: Mon Jan 20, 2014 7:51 am
by Roland_Yonaba
Simon6689 wrote:It's not just two times, I need something that would re-run the file several times.
You can go ahead with The Great Slime's suggestion.
I'll throw this function:
Code: Select all
local function rrequire(modname) return love.filesystem.load(modname..'.lua')() end
Or indeed, you can alter
package.loaded.
See, when you include some file via
require, it caches the file content into a table named
package.loaded.
In case you call the same file again,
require would just lookup into that table and return the cached entry if found.
So, in terms of code, it would be as simple as:
Code: Select all
require 'somefile'
-- some code
package.loaded.somefile = nil -- or package.loaded['somefile'] = nil
require 'somefile'
Or you can wrap it into a function:
Code: Select all
local function rrequire(modname)
package.loaded[modname] = nil
return require(modname)
end
Hope this helps.
Re: Help with require()
Posted: Mon Jan 20, 2014 8:22 am
by Robin
Follow slime's suggestion. It's not hacky at all, unlike any other way of doing it.
I'll show it using jag_e_nummer_ett's example code:
Code: Select all
-- file: myfile.lua
local class = {} --also: should be local not global
function class.init()
class.num = math.random(255)
end
function class.test()
print(class.num)
end
return class
Code: Select all
-- file: main.lua
local one = require "myfile" --also local
function love.load()
one.init()
one.test()
one.init()
one.test()
end