Page 1 of 2

What to keep separate from main.lua

Posted: Thu Apr 02, 2015 7:35 pm
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.

Re: What to keep separate from main.lua

Posted: Thu Apr 02, 2015 11:56 pm
by arampl
An excellent question. I'm also would like to know.

Re: What to keep separate from main.lua

Posted: Fri Apr 03, 2015 12:45 am
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.

Re: What to keep separate from main.lua

Posted: Fri Apr 03, 2015 3:59 am
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.

Re: What to keep separate from main.lua

Posted: Fri Apr 03, 2015 7:48 am
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.

Re: What to keep separate from main.lua

Posted: Fri Apr 03, 2015 3:29 pm
by Bindie
Game architecture, nice!

Re: What to keep separate from main.lua

Posted: Fri Apr 03, 2015 8:47 pm
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).

Re: What to keep separate from main.lua

Posted: Fri Apr 03, 2015 9:35 pm
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.

Re: What to keep separate from main.lua

Posted: Sun Apr 05, 2015 5:52 pm
by Bindie
I think I'm Swedish, "bootstrap"?

Re: What to keep separate from main.lua

Posted: Thu Apr 16, 2015 1:27 am
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.