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.
One-way requiring...
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: One-way requiring...
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: One-way requiring...
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
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.
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
class = require 'middleclass' -- instead of local
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 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
Re: One-way requiring...
Ah, I see. Thanks for the clarification. Definitely going to have to keep things neat if I'm going to make a larger project.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 10 guests