What to keep separate from main.lua

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

What to keep separate from main.lua

Post by Bindie »

Hey! The game I'm making is currently kinda big. Next time I'm to make a game, I feel excited to use separate .lua files for example NPC's, Player, Items and so on.

If I were to do it today it would be kinda experimental, would like some advice on the matter. What to separate into .lua sections, requiring them in main.lua? Of course to make thing easier to find.

Happy easter.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: What to keep separate from main.lua

Post by arampl »

An excellent question. I'm also would like to know.
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: What to keep separate from main.lua

Post by Jeeper »

Personal preference, but what I do is to have as many separate file as possible and do basically nothing aside from calling parent functions in main.lua. I find it a lot easier to keep a large project manageable to keep everything separated. A single file with thousands of lines of code is a lot harder to navigate than several files with only a few hundred lines.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: What to keep separate from main.lua

Post by bobbyjones »

Generally people separate files by purpose. A file should do one thing and do one thing only. Example if i have i have a gamestates.lua file and it handles all of my gamestates then well it can and will get very big as i will have a lot of gamestates. So i would separate my files into smaller more specific files. In this case i would make a gamestates folder and throw all of my gamestates in there as separate files. menu.lua, level1.lua, pause.lua. credits.lua and so forth. so now each of those do one thing and one thing only.
User avatar
SuperZazu
Citizen
Posts: 56
Joined: Sun Jun 10, 2012 2:06 pm
Location: France
Contact:

Re: What to keep separate from main.lua

Post by SuperZazu »

bobbyjones wrote:Generally people separate files by purpose. A file should do one thing and do one thing only. Example if i have i have a gamestates.lua file and it handles all of my gamestates then well it can and will get very big as i will have a lot of gamestates. So i would separate my files into smaller more specific files. In this case i would make a gamestates folder and throw all of my gamestates in there as separate files. menu.lua, level1.lua, pause.lua. credits.lua and so forth. so now each of those do one thing and one thing only.
I agree with this :)
At the end, the idea is that your main.lua is just a bunch of functions that updates/draw the current "game state" (where all your code really is). That allows you to split your game into files (like bobbyjones said "menu.lua", "gameover.lua", and so on), and it is much easier to maintain your code.
If you don't really understand what I'm trying to say, here's an example of what I have in mind (look in the folder "example"). I can also recommend this link and this link, these will be a great help for you if you start wondering about game architecture.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: What to keep separate from main.lua

Post by Bindie »

Game architecture, nice!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: What to keep separate from main.lua

Post by kikito »

I also tend to leave very little stuff on main. Here's my latest example:

https://github.com/kikito/ekrixion/blob/master/main.lua

Notice that in other projects I might need more functions (for mousepressed etc).
When I write def I mean function.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: What to keep separate from main.lua

Post by bartbes »

I tend to do the same, make the main.lua simply "bootstrap" the game by selecting a state and handing things over to the state manager.
Then in turn, I usually have a file per class, and a file per state.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: What to keep separate from main.lua

Post by Bindie »

I think I'm Swedish, "bootstrap"?
User avatar
Rukiri
Citizen
Posts: 68
Joined: Mon Jun 27, 2011 5:52 am

Re: What to keep separate from main.lua

Post by Rukiri »

Main should just be your startup, you should also have files for every map and cut scene, you also want to keep functionality scripts separate just code once and import when needed.
Post Reply

Who is online

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