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

Re: Writing stuff in separate files

Post by Xrocetoxtos »

BrotSagtMist wrote: Tue Apr 25, 2023 4:35 pm Ahh yea sweet dejavu.
We just had a thread here viewtopic.php?f=4&t=94465
The TLDR is: I am out.
Alright. Thanks still.
User avatar
dusoft
Party member
Posts: 655
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Writing stuff in separate files

Post by dusoft »

I liked this lib to manage assets - it has an autodiscovery of assets:
https://github.com/bjornbytes/cargo
User avatar
Xrocetoxtos
Prole
Posts: 9
Joined: Wed Apr 19, 2023 7:31 pm

Re: Writing stuff in separate files

Post by Xrocetoxtos »

Ah thank you. That looks useful too.
Ross
Party member
Posts: 100
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Writing stuff in separate files

Post by Ross »

The thing to understand when switching to Lua from C# (or many other languages) is that it only does what you explicitly tell it to do. There's only one way to define variables and it always works exactly the same way, there's no hidden features. Personally, I think this is wonderful, C# drove me nuts. :D

The only data structure in lua is a table. Everything with properties on it is a table and behaves exactly the same way. If you define a table with properties on it, or add properties to it later, then it will have those properties. Otherwise, it will not. Things don't magically get added to tables when you try to define variables at a certain position in your code. Defining variables and adding elements to tables are two separate things. There is no hidden "self" variable (unless you use the colon (:) syntax to explicitly create and hide one).

You should basically always define variables (including functions) with the "local" keyword. Otherwise, you are declaring them as global variables, accessible throughout your entire program.

That also applies to functions. Using the "function" keyword before the variable name is just extra syntax sugar.

Code: Select all

local function foo()
    -- do stuff
end
-- Is the same as:
local foo = function()
    -- do stuff
end
When you call "require", all it does is run the code in the file as if it was inside a function, and returns the result (or true if the code returns nothing). If you define local variables in there, they won't be accessible anywhere else unless you make them so somehow. If you define global variables in a file then those will of course be defined globally when you require the file.

It's standard practice to return a table with stuff in it from other files and not mess with the global scope to avoid spaghetti code, but you can do whatever you want. You can return whatever you want from the other file if that's useful to you.

The colon syntax is just a convenient little bit of syntax sugar, it's very simple:

Code: Select all

thing:do_your_stuff("string argument")
-- Is exactly the same as:
thing.do_your_stuff(thing, "string argument")
That's it. All it does is pass in the table containing the function as the first argument.
You can also use it when adding/defining functions in a table, and it just creates an invisible first function argument named "self" (but it does not add any extra requirements, guarantees, or other significance to that argument). Personally I think it's a terrible idea to use it that way. :)

Your original code:

Code: Select all

-- graphics.lua
function doSomething(dt)
	-- AAA-style graphics doing mindboggling stuff
end
defines a global function called "doSomething". So as soon as you require that file, that function is defined and any other code in any other file can use it just by calling "doSomething()". So your main.lua would just be this:

Code: Select all

require 'graphics'

function love.update(dt)
	doSomething(dt)
end
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, Ross. There's some differences between lua and c# that I need to get used to, like local not being the default and all the self stuff in functions. Your post here will be very useful.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests