One-way requiring...

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
sam
Prole
Posts: 32
Joined: Sat Nov 10, 2012 2:18 am

One-way requiring...

Post by sam »

Not too sure how to word this one without it sounding weird.
In my game, I have a ton of individual entity files, as many of you do. These are all required by main.lua so they can be manipulated and put into different pre-programmed levels. A side-effect of this is that every file required by main.lua also requires the rest of main's required files as well.
This makes me nervous on some fronts, as I could easily modify an entity's value from another entity without realizing it. I was wondering if there was a way in Lua to do a sort of "one-way require", or make it so that required files don't also require everything else required in main.lua. It would mean a little extra work on my end, putting require statements in front of every entity file, but it would be worth it so that I don't create unnecessary bugs without knowing it...
Not too sure if this even exists. If it's possible, that would be awesome.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: One-way requiring...

Post by Nixola »

I guess you could just use locals?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: One-way requiring...

Post by Inny »

I'm of the philosophy that if you write the main.lua, then you're in charge of the global environment for the application, so in the simplest form, your main.lua can require things and make them global instead of local

Code: Select all

class = require 'middleclass' -- instead of local
But that only works for smaller projects, and makes it that the files in that project aren't usable anywhere because of the global environment assumption. You shouldn't do this for projects of any appreciable size (the safest definition here is for projects with three or more files, and I picked that number so that people can disagree with me, and they should).

So once you have two files, you'll want to switch to listing dependencies at the top of each file via the localized require form.

Code: Select all

local class = require 'middleclass'
You never want to require main. Design your code as a tree, where main requires up, and nothing requires down.

You never want to create circular dependencies, where A.lua requires B.lua, and B.lua requires C.lua, but C.lua requires A.lua. Those are really difficult to fix.

To fix circular dependencies, you can't require at the top of a file, you have to require at the exact point where you need it. This is something you should never do if you can help it, but can do in an emergency.

Code: Select all

function test_something(x)
  local inspect = require 'inspect'
  print(inspect(x))
end
sam
Prole
Posts: 32
Joined: Sat Nov 10, 2012 2:18 am

Re: One-way requiring...

Post by sam »

Ah, I see. Thanks for the clarification. Definitely going to have to keep things neat if I'm going to make a larger project.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests