Skip to content

Stricter Modules

noooway edited this page Aug 5, 2017 · 8 revisions

In part 2-01 I've been talking about splitting the code into several files.

I've adopted a convention that each variable or function declaration should be either local or go into module table. The problem of such approach is that it is easy to forget declare a variable local. In that case, the variable is defined in the global environment.

There are several approaches to avoid this. Some of them can be found here: http://lua-users.org/wiki/ModulesTutorial

Let's again take a look at greetings.lua module from 2-01.

local greetings = {}  --(*1)

greetings.hello_message = "Hello from Greetings module."

function greetings.say_hello()
  print( greetings.hello_message )
end

local secret = "IDDQD"

function greetings.reveal_secret() 
  print( secret )
end

function make_mess()
   mess = "terrible"
end

return greetings --(*2)

This only one possible way to handle this problem. Other approaches are available. Besides, there is strict.lua module serving similar purposes.

local print = print              --(*1)

local stricter_greetings = {}

_ENV = nil                       --(*2)

local hello_message = "Hello from Greetings module."

function stricter_greetings.say_hello()
  print( hello_message )
end

local secret = "IDDQD"

function stricter_greetings.reveal_secret() 
  print( secret )
end

function stricter_greetings.make_mess()
   mess = "terrible"               --(*3)
   -- local mess = "terrible"      
end

return stricter_greetings

(*1): import necessary global functions and variables
(*2): _ENV = nil after this line the global environment is no longer accessible from the code. So, any attempt to add any variable to this environment will result in error. Any attempt to read from the global environment also will result in error.
(*3): mess is not declared local, so it will result in error.

    Home
    Acknowledgements
    Todo

Chapter 1: Prototype

  1. The Ball, The Brick, The Platform
  2. Game Objects as Lua Tables
  3. Bricks and Walls
  4. Detecting Collisions
  5. Resolving Collisions
  6. Levels

    Appendix A: Storing Levels as Strings
    Appendix B: Optimized Collision Detection (draft)

Chapter 2: General Code Structure

  1. Splitting Code into Several Files
  2. Loading Levels from Files
  3. Straightforward Gamestates
  4. Advanced Gamestates
  5. Basic Tiles
  6. Different Brick Types
  7. Basic Sound
  8. Game Over

    Appendix C: Stricter Modules (draft)
    Appendix D-1: Intro to Classes (draft)
    Appendix D-2: Chapter 2 Using Classes.

Chapter 3 (deprecated): Details

  1. Improved Ball Rebounds
  2. Ball Launch From Platform (Two Objects Moving Together)
  3. Mouse Controls
  4. Spawning Bonuses
  5. Bonus Effects
  6. Glue Bonus
  7. Add New Ball Bonus
  8. Life and Next Level Bonuses
  9. Random Bonuses
  10. Menu Buttons
  11. Wall Tiles
  12. Side Panel
  13. Score
  14. Fonts
  15. More Sounds
  16. Final Screen
  17. Packaging

    Appendix D: GUI Layouts
    Appendix E: Love-release and Love.js

Beyond Programming:

  1. Game Design
  2. Minimal Marketing (draft)
  3. Finding a Team (draft)

Archive

Clone this wiki locally