Design Talks - Your Game Module

I’m working on some starter materials for myself and the community and I wanted to talk to new and experienced developers about their best practices.

Specially, I want (in this post) to talk about the game module.

Game Module

Before I proceed, let me clearly define what I mean by ‘game module’.

In this discussion, a game module is a single file (usually called game.lua).

This file contains/defines a module that has all of the responsibility for creating, destroying, and in general managing game objects, game data, and game-play logic.

For all but the simplest games, a game module will often include/require other modules to do its work.  i.e. It is not monolithic.

A game module is NOT responsible for:

  • splash screen
  • main menu
  • help screens
  • options

A game module may however implement/manage, HUDS:

  • score count(s)
  • coin coin
  • hitpoints
  • damage indicators
  • radar

My Game Module(s) Approach

In my games, the game module may have any of the following functions: 

  • init() - One time module initialization.
  • create() - Create the game world/content  + (optionally) start game logic.
  • destroy() - Destroy all groups and game content + (optionally) stop game logic.
  • start() -  Starts game logic.
  • stop() - Stop game logic. Usually as the result of of of these:
    • Level or Game Loss
    • Player Death
    • Level Completion
    • Game Completion
  • continue() - Resumes a game after player death without starting over at the beginning.
  • restart() - Restarts the game afresh from the same coditions as the last create() and/or start().
  • pause( pause ) - Pauses or resumes game interaction and logic.

Legend:  required ** optional**

My Questions

  1. Are you a new or experienced game developer?
  2. Do you use the concept of a game module?
  3. Do you use some or all of the functions I use?
  4. If you do not use a game module, how do you organize your game logic?
  5. If you do use a game module and it is different from mine, can you describe yours?
  6. Was this an interesting discussion and would you like more discussions like this here?

So far, I’ve gotten no responses which is… disappointing.  However, maybe I posted this on a bad day for view.

I’m really curious to hear what other folks think and do.  

Please respond if you have any interest in this topic, something to contribute, and the time to do so.  Just five minutes should do it.

Thanks!

My games do not really follow a start, stop, restart format so mine are structured somewhat different.

I have a single scene (you could call this the game module) and show all other scenes via showOverlay() so I have no scene management and refreshing to really worry about.  In my main scene I only have UI/UX, onFrame(), onKey() and onTouch().

Now things get more complicated…  I then instantiate 2D arrays of modules to build my world.  Each module is completely self-contained and only exposes an interface so that the UI can use it.  Otherwise they are responsible for their full lifetime.

I then have lots of helper modules for things like networking, IAPs, translations, z buffer, texture/asset managers, static maps, sounds, particle effects, shaders and various vehicle managers.

Not sure if this was what you were asking for or not?

@SGS - Thanks for the answer.  That was perfectly fine.  

Your game architecture seems more like a spider (for lack of a better analogy) with the main scene being the body and the legs being all the modules/helpers.

Very interesting.

My game is base on a few levels of init.

init the game

   init a party

      init a level

 when my game is finish if the user restart the level it’s reload only the level, if he go to the next level it’s init again party and level… Like this there is no loading

The rest of the module don’t work vertically (play,pause, replay…) they work independently 

I have functions who manage the time. (remove visual effect if under a number of fps etc)

I have a module who manage timer, transition, enterframe (to pause them, x2,x0.5, synchronise when there is hundred of them, resume, finish…)

I have a module who manage sound (to don’t have too much sound play at the same time and too loud)

Manage visual effect (filter when you lose life, use spell, particules for blood and dust)

A module with ML who adapt the level to the player in real time.

Something to manage object in 3D isometric.

Function who manage texture memory (release when there is a warning), load on demand and on the availability of the device

So to elaborate a bit more…  I try and maintain an OOP approach.

For example, I have building.lua which is approx 4k lines of code and this completely handles an instance of a tile/building. 

This will expose properties and functions so that the main scene can manipulate it, functions like selectBuilding(), moveBuildingTo(), removeBuilding(), drawBackground(), etc. 

I can then define my world as buildings[x][y] = <instance of building.lua>

This makes the actual UI coding straightforward.  So if I want to add a road at 13,5 I simply call buildings[x][y]:addRoad().  In that function is all the logic for spawning a road and linking it up with neighbouring roads.

Woah, time to refactor :wink:

Haha, in the Corona version of my game I had modules 10,000 lines long which became a nightmare to debug - now I’m working in Unity I become queasy any time a class goes over 250 lines!

But surely most of that is just pointless squiggly brackets?  :lol:

RG,

  1. am experienced game developer - longtime corona.

  2. yes … always like to use a game module similar to how yours is defined. Mine is just not as elaborate as yours.

I have broke from using a game module a time or two, but I always prefer to use some version of a game module.

  1. I always call versions of create() & init() from my composer scene:create and scene:show … but I will normally have several functions handling all the create stuff, like  ‘createBoard’, ‘createPlayer’, ‘createUI’, etc…  It then depends on what I need done by init() as to wether it is called from scene:show or scene:create.  I don’t call it init(), call it different things depending the game. 

  2. n/a  I use a game module.

  3. I do like to break down the action of the gameScene into regular common actions, common to most games…  like you do, I am just not disciplined enough to do it consistently.  But I am working on making myself a template with some of the same function breakouts as you list them, and hope to stat using them consistently.  I have ‘classes or modules’ that I use for menus, messages,  and effects - those regular and repeated things used in most apps/games. These help keep my gameScene from getting to large.

My general app layout is usually close to this

main.lua  ( handles all the data setup and services(ads, backend, multiplayer) )

gameServer.lua ( module to handle multiplayer stuff )

constants.lua ( global module for constants and standard things like screen dims that are used repeatedly throughout that app )

mainScene.lua ( main menu - each menu item will call a scene or overlay )

setupScene.lua(  customize choices of game play for player - avatar selection, game speed,  etc…)

gameScene.lua ( all game mechanics and action )

then I have a few standard overlay templates I use for help-info, options, credits, game promos etc…

  1. I like this discussion. It is good to see how others tackle these common situations. There seems endless ways to do most things.

Thanks for posting the topic!

Bob

I tend to miss out so much on forums lately. Glad you posted this also to the Slack channel.

I’m developing games professionally for 7 years(6 of it is with Corona) now so I guess you can call me experienced.

  • For casual games, I use a single gameLevel.lua file(a Composer scene actually) and fit all the related functions in this single file. Since it doesn’t get cluttered so much when developing casual games, I still prefer this approach.

  • For point & click adventure games I did in the past, I was using that gameLevel.lua file to call another file where there are many functions like loadLevel(), createLayers() etc. Looking back, it doesn’t seem necessary and probably could have done it like the casual games I make today but that’s what I did for those games. By the way, that file I called for functions was somewhere near 6k lines of code so :slight_smile:

I can’t say I got a naming standart like yours since I’m the sole developer for all my projects but I have something similar to yours. I prefer using a createX(), destroyX(), moveX(), endGame() kind of naming scheme for the games I make. When I need to fix or add some stuff, I search for those keywords(create, destroy, move, end) in the code.

This is not some concept I got from somewhere but the way I come to terms after developing many games. This is working as expected for the casual games at the moment but I don’t know if things will change if I dive into a relatively bigger game.

I would really love to see more discussions like this here which means I have to keep following the forums more often. :slight_smile:

@bgmadclown

 You can have the forums email you all updates immediately like I do. :)    Then you’ll never miss out.

Other than the topics I follow, that would be too much for me :stuck_out_tongue:

  1. Are you a new or experienced game developer?  I guess I would say experienced… but not as much of late as I would like
  2. Do you use the concept of a game module? Yes
  3. Do you use some or all of the functions I use? I tend to have a startGame() and perhaps a startLevel() function. Depending on the game, there may be a pause() function depending on the game.
  4. If you do not use a game module, how do you organize your game logic?  As I’ve matured with Corona I’m getting much more complex in code organization. For instance in my side-shooter that I had to back-burner, I had a player.lua module that handled the player’s ship, it’s fire and damage code, etc. The low level enemies had their own .lua file, the bosses had their own .lua file. I’m trying to think more object oriented. The game.lua calls the UI module manages scrolling the background, managing collision, processing game wide events like bombs exploding, etc.
  5. If you do use a game module and it is different from mine, can you describe yours? See above.
  6. Was this an interesting discussion and would you like more discussions like this here? Always!

@roamimggamer  Great topic Ed!

My approach is One Module to Rule them all!

module_forum_small.jpg

In a previous post I brought up some questions about the way I was structuring my modules.  I was getting paranoid that I was going to program myself into a corner but you guys talked me off the ledge :wink:

Basically, to get to any module, you have to go through the master module.  I originally set it up this way to avoid circular references but it turned out to be a structure that allows me to rapidly deploy a whole bunch of unruly modules that don’t always want to get along.

1. So generally, I call SSK and all of the modules in main.lua.  I start with the master module and the other modules populate the master as they are referenced.

2.   From main.lua I call in my scenes using composer, but I only use composer lightly and do all of the heavy lifting through an init module that allows me to interact with the master module and call in the sub modules.  As the game progresses most of the interaction is directly with the master module.

3. So I’ll go to a scene and call in:  the background, the characters, ui and touch, the minimap, the shader, etc.  The master module tracks everything so I destroy objects “manually” instead of using the composer.

4. I do all of my collision detection and movement through the physics engine using a module I call the Trig Engine.

All of my modules, including the Shader and the Trig Engine are under 1,000 lines - it took some work to get them there!

About me:

     I’d call myself a seasoned developer.  I used to do web development with the best years being dedicated to Flash and ActionScript.  As an Artist/ Designer / Programmer, Flash provided me with an active playground where I could rapidly test ideas and make “sketches” with code and art before committing to a structure.  Once Flash declined I went fallow for several years and focused on my design business until I discovered Corona which in an environment that has everything I missed from Flash and A LOT more!  As @SGS says, I win the prize for fewest apps released  :) but now that I closed my design business I’m pushing to get something exciting to market (I’m rewriting everything with IAP in mind instead of going premium).

I always appreciate these types of discussions.  So many ways to peel a potato!

This topic is very interesting :slight_smile: thank you @roaminggamer

From an app to a new one, how much of your code you have in common?(for a same kind of game 85% and for a totally different game 20%)

What’s your average number of code line for the whole app? (mine is ~120k)

What’s your average number of code line for the minimal app? (mine is ~20k)

Which part of coding take you the longest time (game engine, physics, optimization, testing…)?(me it’s optimization ~30% of coding time)

Would you be agree to share your code to a close group of dev to share your methods and tips?(Ask for my code source of gate of heroes or stronghold and I will share you and you can take what ever you want but (not images))

If your game have physics, do you use Corona physics or your own algorithms?(I use mine, firstly because I didn’t know corona physics exist and I prefer to do it by myself)

@remiduchalard  Optimization used to take about 70% of my time but as my library of modules increases that time reduces.  Maybe optimization is 40% of my time now.  * I count play testing revisions as optimization *

@remi, what’s your definition of minimal? 20k seems too much considering my personal record of ~6k for a point & click adventure game. 120k… WOW!

Yeah 20k seems crazy for a small app!  I have less than 50k for my games (and they are kinda big games) and at least 10% of that is comments.

Oh and code sharing is not high on my list… discussing methodologies yeah sure.

For me, testing is the most time-consuming… as my games are time-based and level-based what works on level 50 does not work on level 100 so a full test of my game (prior to upgrade) takes well over a week.  I now have a 20 page test script.

I’ve just finished a cool little word game that is around 2k total - made for Corona Jam but turned into a proper app now

https://itch.io/jam/glitch-jam-1/rate/254805

Very interesting, I don’t know how your minimal apps is done with only a few thousand lines of code.

For example to manage properly the walking of my unit I have a few thousand lines of code. There is an explanation ?

I have attached the 3 files who manage the walking of my units. If someone is a bit crazy and want to have a look on it, I will be really please to have his opinion :slight_smile:

For example, only to display a unit bouncing when it get deadly hit, it’s take one hundred lines. (animdepter line 303 to 400), I see how to reduce but only little number of lines.

the result can be see here : https://youtu.be/2fFyBh5_FQU?t=47 (play the trailer at 0.25 of the normal speed to see the result)