-
Notifications
You must be signed in to change notification settings - Fork 19
Stricter Modules
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)
local print = print --(*1)
local stricter_greetings = {}
if setfenv then
setfenv( 1, {} ) -- for Lua 5.1 --(*2)
else
_ENV = nil -- for Lua 5.2 and newer --(*2)
end
local hello_message = "Hello from Stricter 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.
In Lua 5.1 setfenv
function is used instead of manipulations with _ENV
variable.
(*3): mess
is not declared local
, so in Lua 5.3 it will result in error. In older Lua it will be defined
in a separate table.
This is one possible approach to create modules in Lua.
Besides, there is strict.lua
module serving similar purposes.
Feedback is crucial to improve the tutorial!
Let me know if you have any questions, critique, suggestions or just any other ideas.
Chapter 1: Prototype
- The Ball, The Brick, The Platform
- Game Objects as Lua Tables
- Bricks and Walls
- Detecting Collisions
- Resolving Collisions
- Levels
Appendix A: Storing Levels as Strings
Appendix B: Optimized Collision Detection (draft)
Chapter 2: General Code Structure
- Splitting Code into Several Files
- Loading Levels from Files
- Straightforward Gamestates
- Advanced Gamestates
- Basic Tiles
- Different Brick Types
- Basic Sound
- 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
- Improved Ball Rebounds
- Ball Launch From Platform (Two Objects Moving Together)
- Mouse Controls
- Spawning Bonuses
- Bonus Effects
- Glue Bonus
- Add New Ball Bonus
- Life and Next Level Bonuses
- Random Bonuses
- Menu Buttons
- Wall Tiles
- Side Panel
- Score
- Fonts
- More Sounds
- Final Screen
- Packaging
Appendix D: GUI Layouts
Appendix E: Love-release and Love.js
Beyond Programming: