Page 1 of 1

Directory & Code Organisation

Posted: Sun Aug 04, 2013 10:54 pm
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.

Re: Directory & Code Organisation

Posted: Mon Aug 05, 2013 4:27 am
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. :)

Re: Directory & Code Organisation

Posted: Mon Aug 05, 2013 4:30 pm
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).

Re: Directory & Code Organisation

Posted: Mon Aug 05, 2013 5:22 pm
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.

Re: Directory & Code Organisation

Posted: Mon Aug 05, 2013 6:58 pm
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.

Re: Directory & Code Organisation

Posted: Mon Aug 05, 2013 7:04 pm
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.

Re: Directory & Code Organisation

Posted: Mon Aug 05, 2013 7:20 pm
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.

Re: Directory & Code Organisation

Posted: Thu Aug 08, 2013 12:13 pm
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.

Re: Directory & Code Organisation

Posted: Thu Aug 08, 2013 4:04 pm
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.

Re: Directory & Code Organisation

Posted: Thu Aug 08, 2013 4:49 pm
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.