Directory & Code Organisation

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.
Post Reply
ixjf
Prole
Posts: 19
Joined: Sat Apr 27, 2013 10:27 am

Directory & Code Organisation

Post by ixjf »

Hi there,

I have been programming for about 3 years (Lua, C++ and a few others), and I feel this is a big problem to me. It has always been, to be honest, but when I was newbie this was not a problem, I mean, newbies don't care about that? (I was a 10-11 year old guy at that time, barely understood anything) Meanwhile I am working on two projects, and it's being a really very big problem for me to organise my code (be it code organisation within files, or folder organisation), I am barely ever able to start a project, because I cannot come up with a proper design and I delete the whole code, to do a fresh start - I have been this way on a project for half the summer now. I am a perfectionist, nothing can be wrong, or it makes me feel kinda.. I don't know how to explain what I feel - It simply has to be perfect, and this affects me much, and stops me from finishing any project, because I am never satisfied. I wonder, do any of you have this problem too? Have any of you ever had it, how did you overcome this problem? How do you organise your files and code within the files? Could all of this be just due to my lack of imagination to design code?

Believe me, I have searched a lot about this, but I couldn't find any information that would fit my case.
I used to be an adventurer like you, but then I took an arrow in the knee.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Directory & Code Organisation

Post by Davidobot »

I think this thread will help you out: http://love2d.org/forums/viewtopic.php?f=3&t=38100
I contains a lot of different programming code layouts and such. So be sure to read it all. :)
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
ixjf
Prole
Posts: 19
Joined: Sat Apr 27, 2013 10:27 am

Re: Directory & Code Organisation

Post by ixjf »

I think.. I think my problem is not really about the coding style I use (I actually searched Google for days regarding coding styles, and I know how to properly design my code), but rather how I organise the code within files (both the contents place, variable naming, and how I separate the code between the files).
I used to be an adventurer like you, but then I took an arrow in the knee.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Directory & Code Organisation

Post by Davidobot »

Well, then you can use game states to organize your code, check out my library in my signature. Or do a class based system thing.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
Mikaboshi
Prole
Posts: 12
Joined: Mon Jul 22, 2013 8:55 pm

Re: Directory & Code Organisation

Post by Mikaboshi »

I use files as abstraction tools. This is just how I tend to layout my code.

main.lua contains a high-level architecture of the game. You should be more or less able to read it and understand what it's doing from a high-level perspective. From this, you separate into directories. Each file in the directories should return a table, class, or mixin.

This is an example structure. It's of an incomplete game, but should give you an idea of how I generally work.
  • utils/ — Used for code that is completely abstracted from your game. Some examples:
    • middleclass.lua — For easy object-orientation.
    • strict.lua — To make sure you're not using any global variables accidentally.
    • lovedebug.lua — To provide a debug console for your game.
  • engine.lua — A table containing all of the facilities of the engine. (Optional)
  • engine/ — Components of your game that could be used for other games. Some examples:
    • camera.lua — Provides a camera class to pan/zoom/etc.
    • level.lua — Provides a level loader and/or level format.
    • entity.lua — Provides an entity class for the basis of your game entities.
    • physics.lua — Provides collision support and other physics.
  • gui.lua — A table containing all of the facilities of GUI system. (Optional)
  • gui/ — Folder for each GUI item
    • button.lua — Provides a button class with a callback
    • textbox.lua — Provides a textbox class
  • game.lua — A table containing all of the facilities of the game. (Optional)
  • game/ — Components of your game that are completely unique to your game. Some examples:
    • player.lua — Returns a player class that contains things like health, a table of items, etc.
    • serialization.lua — Provides facilities to save and load game.
    • item.lua — Returns an item class for constructing items with.
    • item/ — A folder for keeping item subclasses in
      • sword.lua — Returns a sword item class
      • gun.lua — Returns a gun item class
Once my files start getting >200 lines, I generally start restructuring my code into more files (and thus tables, classes, and mixins). Other people may like more lines, other less. It's just personally my style. I'm sure there are other great ways to layout your code. This is just most natural to me.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Directory & Code Organisation

Post by bartbes »

Mikaboshi wrote: This is an example structure. It's of an incomplete game, but should give you an idea of how I generally work.[stuff]
I'd recommend, instead of having files with the same names as folders, having an init.lua within the folder, that also gets loaded when you specify the folder name.
Mikaboshi
Prole
Posts: 12
Joined: Mon Jul 22, 2013 8:55 pm

Re: Directory & Code Organisation

Post by Mikaboshi »

bartbes wrote:
Mikaboshi wrote: This is an example structure. It's of an incomplete game, but should give you an idea of how I generally work.[stuff]
I'd recommend, instead of having files with the same names as folders, having an init.lua within the folder, that also gets loaded when you specify the folder name.
Oh thanks, I actually didn't know about that functionality. Also worth noting that you should probably have a data/ or assets/ folder with subfolders for sprites, tilesheets, maps, etc.
ixjf
Prole
Posts: 19
Joined: Sat Apr 27, 2013 10:27 am

Re: Directory & Code Organisation

Post by ixjf »

I'm sorry for this big delay, I couldn't come before.

Thanks for your time, I really appreciate it - though, I suppose having a complete game structure made for me won't solve my issue and when I come across another project, I'll end up with the same problem, I would like any of you, or anybody else, to answer these questions too, which I made in the original post:
I wonder, do any of you have this problem too? Have any of you ever had it, how did you overcome this problem? Could all of this be just due to my lack of imagination to design code?
By the way, how do you strike through text (line-through) and center it here? It looks like the s and center tags aren't working.
I used to be an adventurer like you, but then I took an arrow in the knee.
Mikaboshi
Prole
Posts: 12
Joined: Mon Jul 22, 2013 8:55 pm

Re: Directory & Code Organisation

Post by Mikaboshi »

ixjf wrote:I'm sorry for this big delay, I couldn't come before.

Thanks for your time, I really appreciate it - though, I suppose having a complete game structure made for me won't solve my issue and when I come across another project, I'll end up with the same problem, I would like any of you, or anybody else, to answer these questions too, which I made in the original post:
I do a lot of code reorganization, actually. I spend a very significant amount of time making a good code architecture. However, to keep myself from going overboard, I set milestones with due dates. Each one of my milestones has a set of issues which represent features or bugs to fix. After I complete all of the issues for a milestone, I spend the rest of the time reorganizing and cleaning up my code. Although often times I do it intermittently too.
ixjf
Prole
Posts: 19
Joined: Sat Apr 27, 2013 10:27 am

Re: Directory & Code Organisation

Post by ixjf »

I always do it too, but I never get satisfied with a structure - it's always bad for me. Perhaps I just need to organise my work better.
I used to be an adventurer like you, but then I took an arrow in the knee.
Post Reply

Who is online

Users browsing this forum: No registered users and 73 guests