Help with require()
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- jag_e_nummer_ett
- Citizen
- Posts: 52
- Joined: Thu May 16, 2013 6:31 pm
- Location: Stockholm, Sweden
Help with require()
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?
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?
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Help with require()
The PiL is sadly outdated, the module name gets passed as first vararg.
- jag_e_nummer_ett
- Citizen
- Posts: 52
- Joined: Thu May 16, 2013 6:31 pm
- Location: Stockholm, Sweden
Re: Help with require()
So like this?bartbes wrote:The PiL is sadly outdated, the module name gets passed as first vararg.
Code: Select all
args = {...}
name = args[1]
or
name = ...
Re: Help with require()
I need some help with require() too.
How do you require something twice?
How do you require something twice?
- jag_e_nummer_ett
- Citizen
- Posts: 52
- Joined: Thu May 16, 2013 6:31 pm
- Location: Stockholm, Sweden
Re: Help with require()
At the site I posted in the main post content it says this "Remember that require avoids loading the same package multiple times."Simon6689 wrote:I need some help with require() too.
How do you require something twice?
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
Re: Help with require()
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:
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?
I have also tried using
Code: Select all
loadfile "name.lua"
Code: Select all
loadfile("C:\\Users\\Simon\\Documents\\GitHub\\Game\\Game\\objects\\"..name..".lua")
Is there a way to get the path that main.lua is in?
- slime
- Solid Snayke
- Posts: 3162
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Help with require()
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.
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.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Help with require()
You can go ahead with The Great Slime's suggestion.Simon6689 wrote:It's not just two times, I need something that would re-run the file several times.
I'll throw this function:
Code: Select all
local function rrequire(modname) return love.filesystem.load(modname..'.lua')() end
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'
Code: Select all
local function rrequire(modname)
package.loaded[modname] = nil
return require(modname)
end
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Help with require()
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:
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
Help us help you: attach a .love.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests