Help with require()

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
jag_e_nummer_ett
Citizen
Posts: 52
Joined: Thu May 16, 2013 6:31 pm
Location: Stockholm, Sweden

Help with require()

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Help with require()

Post by bartbes »

The PiL is sadly outdated, the module name gets passed as first vararg.
User avatar
jag_e_nummer_ett
Citizen
Posts: 52
Joined: Thu May 16, 2013 6:31 pm
Location: Stockholm, Sweden

Re: Help with require()

Post 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...
User avatar
Simon6689
Prole
Posts: 7
Joined: Tue Jan 07, 2014 12:41 am

Re: Help with require()

Post by Simon6689 »

I need some help with require() too.
How do you require something twice?
User avatar
jag_e_nummer_ett
Citizen
Posts: 52
Joined: Thu May 16, 2013 6:31 pm
Location: Stockholm, Sweden

Re: Help with require()

Post 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
User avatar
Simon6689
Prole
Posts: 7
Joined: Tue Jan 07, 2014 12:41 am

Re: Help with require()

Post by Simon6689 »

It's not just two times, I need something that would re-run the file several times.
I have also tried using

Code: Select all

loadfile "name.lua"
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?
User avatar
slime
Solid Snayke
Posts: 3162
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Help with require()

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Help with require()

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help with require()

Post 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
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests