Writing stuff in separate files

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.
User avatar
Xrocetoxtos
Prole
Posts: 9
Joined: Wed Apr 19, 2023 7:31 pm

Writing stuff in separate files

Post by Xrocetoxtos »

Hi people, I'm new here (well, I have some Löve experience back in the past, but switched to Unity and revisiting Löve now)

I am working on a survival game of currently just blocks avoiding each other and I'm getting to the point of inserting some animations and graphics. To avoid my files being very large and stuff being hard to find and debug, I want graphics stuff, AI stuff, inventory stuff etc outside the main.lua if it's possible.

I know you can use the require to link to other files, but can anyone tell me how to set that up? I'm planning to have a graphics.lua file that will contain a couple of references like an image called "spritesheet" and a couple of functions to do something with that image. in main.lua, I want to access those functions and references in code, like in love.draw().

for instance:

Code: Select all

-- main.lua

graphics = require 'graphics'

-- [...]

function love.update(dt)
	graphics.doSomething(dt)
end

Code: Select all

-- graphics.lua

function doSomething(dt)
	-- AAA-style graphics doing mindboggling stuff
end
How should that be done?
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Writing stuff in separate files

Post by Andlac028 »

Basically, modify your graphics.lua to return table, like:

Code: Select all

local graphics = {}

function graphics.doSomething(a, b)
    -- your logic here
end

-- maybe more functions

return graphics
Then you can just do what you have in main.lua (note that if graphics.lua is not in same directory as main.lua (for example it is in scripts/graphics.lua, you need do call require with ‘scripts.graphics’ instead of ‘graphics’ only)
User avatar
BrotSagtMist
Party member
Posts: 636
Joined: Fri Aug 06, 2021 10:30 pm

Re: Writing stuff in separate files

Post by BrotSagtMist »

Well there are basically two ways of using require:
We can just use it to plain insert any file at that place so that the end result behaves as being one file.
Or we handle it as a block that returns a result that can be used, as explained above.

For private use its just a matter of if you want to ad "graphics" before every call or not.
Libs however are required to use the later format.
obey
User avatar
Xrocetoxtos
Prole
Posts: 9
Joined: Wed Apr 19, 2023 7:31 pm

Re: Writing stuff in separate files

Post by Xrocetoxtos »

Thank you both. What I meant was exactly what Andlac was suggesting. I'll use it that way, except the other way is even better for my taste, so he's one more question:
BrotSagtMist wrote: Tue Apr 25, 2023 12:12 pm We can just use it to plain insert any file at that place so that the end result behaves as being one file.
does this in practice mean that in my example the require needed to be inside the love.update? Something like this:

Code: Select all

function love.update(dt)
	graphics = require 'graphics'
	doSomething(dt)
end
Or should it be written differently? Or does it not work like that at all?
User avatar
BrotSagtMist
Party member
Posts: 636
Joined: Fri Aug 06, 2021 10:30 pm

Re: Writing stuff in separate files

Post by BrotSagtMist »

Yea the thing is that require itself is a wrapper around the actual code insert and as such does a little bit more than just adding it.
Specifically it resolves and searches all possible paths for libs, inspects what the file is about and keeps track about if that file has been used already.
What you wrote there would mean to check for a file in a bunch of places 60 times per second, yea thats a bit stupid.
It also wont work because as it keeps track of what it already opened before it will simply ignore this.

You could make this work by using the dofile() function instead, which lacks the trackkeeping and place resolving.
But again, reading a file 60 times per second isnt normal.

You could read the file once and then insert it using just do, but thats just require with extra steps.

Your example would neet to look like this:

Code: Select all

--something.lua
function doSomething(dt)
-- stuff
end

Code: Select all

--main.lua
require(something)
function love.update(dt)
	doSomething(dt)
end
obey
User avatar
Xrocetoxtos
Prole
Posts: 9
Joined: Wed Apr 19, 2023 7:31 pm

Re: Writing stuff in separate files

Post by Xrocetoxtos »

Ah right. I think I understand. i probably like the other way better, with graphics.doSomething(). Maybe once I'm more versed in lua and less in c#, it might feel more comfortable. Thanks anyway, I'll let my people bring you guys the karma :awesome:
User avatar
Xrocetoxtos
Prole
Posts: 9
Joined: Wed Apr 19, 2023 7:31 pm

Re: Writing stuff in separate files

Post by Xrocetoxtos »

Hmm I think I'm still misunderstanding something, because I have this thing in my love.load now in main.lua

Code: Select all

objects = require 'objects'

player=objects.Player:new([... params...)
then there is objects.lua

Code: Select all

local objects={}

Player={}
function objects:Player:new([...params...])
...
end
If I run this, Löve tells me it can't index field 'Player' because it's nil. Now I can solve this by using the other way of referencing the file, like you suggested, BrotSagtMist, and remove the references to 'objects', but how do I use the objects....- or objects:... way?

does this question make sense?
User avatar
BrotSagtMist
Party member
Posts: 636
Joined: Fri Aug 06, 2021 10:30 pm

Re: Writing stuff in separate files

Post by BrotSagtMist »

Ahh yea sweet dejavu.
We just had a thread here viewtopic.php?f=4&t=94465
The TLDR is: I am out.
obey
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Writing stuff in separate files

Post by Andlac028 »

Xrocetoxtos wrote: Tue Apr 25, 2023 2:17 pm Hmm I think I'm still misunderstanding something, because I have this thing in my love.load now in main.lua

Code: Select all

objects = require 'objects'

player=objects.Player:new([... params...)
then there is objects.lua

Code: Select all

local objects={}

Player={}
function objects:Player:new([...params...])
...
end
If I run this, Löve tells me it can't index field 'Player' because it's nil. Now I can solve this by using the other way of referencing the file, like you suggested, BrotSagtMist, and remove the references to 'objects', but how do I use the objects....- or objects:... way?

does this question make sense?
Yes, Player is undefined, because it is not in table, use something like this:

Code: Select all

local objects = {
    Player = {},
    more = {}
}

function object.Player.something()
end

return objects
Note that there is actually difference when using . and : and how you then can use self, but to better understand it, I recommend you manual for Lua OOP mentioned in the thread mentioned above (sorry, I am on phone and too lazy to find and copy link, just search for some Lua OOP tutorials)
Last edited by Andlac028 on Tue Apr 25, 2023 7:12 pm, edited 1 time in total.
User avatar
Xrocetoxtos
Prole
Posts: 9
Joined: Wed Apr 19, 2023 7:31 pm

Re: Writing stuff in separate files

Post by Xrocetoxtos »

I will check that out. Thank you for the help anyway.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests