lua 11

Chapter 11 — Modules (📦 Split Big Programs into Small Files)

This chapter teaches one of the biggest habits of professional programmers.

Imagine you're building Minecraft.

Would you put 50,000 lines of code into one file?

❌ Never.

Instead:

Player.lua
Inventory.lua
Enemy.lua
Shop.lua
Save.lua
Settings.lua

Each file has one job.

That's what Modules are.


🎯 Mission

Imagine building an online shopping app.

Would you rather have:

Everything.lua

(30,000 lines 😱)

OR

Login.lua
Cart.lua
Products.lua
Payment.lua
Orders.lua

Much cleaner.


🟢 LEVEL 1 — Survival


Q1

What is the main purpose of modules?

A) Store numbers

B) Split code into smaller files

C) Create loops

D) Print text

✅ Answer: B


Q2

Why do programmers use modules?

A) To organize code

B) To make code longer

C) To replace variables

D) To replace loops

✅ Answer: A


Q3

Imagine a game.

Where should inventory code go?

A) Inventory module

B) Settings module

C) Login module

D) Music module

✅ Answer: A


Q4

A module is basically...

A) A separate Lua file

B) A loop

C) A table

D) A string

✅ Answer: A


Q5

Which project is easier to manage?

A)

Everything.lua

B)

Player.lua
Enemy.lua
Inventory.lua

✅ Answer: B


Q6

The biggest benefit of modules is...

A) Better organization

B) Faster internet

C) More RAM

D) Bigger monitor

✅ Answer: A


🟡 LEVEL 2 — Think Like a Programmer


Q7

You're making a racing game.

Where should car movement code go?

A) Car.lua

B) Music.lua

C) Shop.lua

D) Save.lua

✅ Answer: A


Q8

You're making a chat app.

Where should login code go?

A) Login.lua

B) Message.lua

C) Theme.lua

D) Emoji.lua

✅ Answer: A


Q9

Which project looks more professional?

A)

game.lua

(10,000 lines)

B)

Player.lua
Enemy.lua
Map.lua
Items.lua

✅ Answer: B


Q10

Modules make code...

A) Easier to find

B) Harder to read

C) Slower

D) More confusing

✅ Answer: A


Q11

If a bug is in inventory,

where do you look first?

A) Inventory.lua

B) Player.lua

C) Music.lua

D) Random file

✅ Answer: A


Q12

A module should usually have...

A) One main responsibility

B) Ten unrelated jobs

C) No purpose

D) Every function in the project

✅ Answer: A

💡 This is called the Single Responsibility Principle.


🟠 LEVEL 3 — Real-Life Programming


Q13

You're making YouTube.

Where should video-playing code go?

A) VideoPlayer.lua

B) Login.lua

C) Comments.lua

D) Settings.lua

✅ Answer: A


Q14

You're making Instagram.

Where should photo upload code go?

A) Upload.lua

B) Chat.lua

C) Theme.lua

D) Friends.lua

✅ Answer: A


Q15

You're making WhatsApp.

Where should message code go?

A) Messages.lua

B) Camera.lua

C) Login.lua

D) Settings.lua

✅ Answer: A


Q16

You're making a game.

Enemy AI belongs in...

A) Enemy.lua

B) Player.lua

C) Shop.lua

D) Audio.lua

✅ Answer: A


Q17

A shopping cart should probably live in...

A) Cart.lua

B) Music.lua

C) Weather.lua

D) Login.lua

✅ Answer: A


Q18

Which project is easiest to expand?

A) Many small modules

B) One huge file

C) One huge function

D) Thousands of variables

✅ Answer: A


🔴 LEVEL 4 — Bug Hunter


Q19

Your file contains:

  • Login

  • Shop

  • Music

  • Inventory

  • Save

  • Enemy AI

Problem?

A) Too many unrelated jobs

B) Missing loop

C) Missing variable

D) Nothing

✅ Answer: A


Q20

You can't find inventory code.

Why?

A) Everything is in one file

B) Missing print

C) Missing for

D) Missing table

✅ Answer: A


Q21

Someone names files:

a.lua
b.lua
c.lua

What's wrong?

A) Poor names

B) Too many loops

C) Too many strings

D) Nothing

✅ Answer: A

💡 Good names save hours of searching.


Q22

Which is better?

A)

Enemy.lua

B)

thing.lua

C)

abc.lua

D)

random.lua

✅ Answer: A


Q23

A file grows to 8,000 lines.

Best idea?

A) Split it into modules

B) Keep adding more code

C) Delete comments

D) Rename variables

✅ Answer: A


Q24

Which naming style is best?

A)

Player.lua

B)

stuff.lua

C)

test2.lua

D)

file.lua

✅ Answer: A


🟣 LEVEL 5 — Boss Battle 👑


Q25

A module is...

A) A well-organized Lua file with a specific purpose

B) A loop

C) A table

D) A number

✅ Answer: A


Q26

Why split code into modules?

A) Easier reading

B) Easier debugging

C) Easier teamwork

D) All of the above

✅ Answer: D


Q27

You're making a 2D game.

Which structure looks best?

A)

Everything.lua

B)

Player.lua
Enemy.lua
Map.lua
Inventory.lua
UI.lua
Audio.lua

✅ Answer: B


Q28

Which file should contain save-game logic?

A) Save.lua

B) Shop.lua

C) Audio.lua

D) Menu.lua

✅ Answer: A


Q29

Professional programmers usually prefer...

A) Many small files

B) One gigantic file

C) One gigantic function

D) Thousands of global variables

✅ Answer: A


Q30 — Final Boss 🏆

You're building the next GTA game.

Which mindset is the best?

A)

Put every line of code into game.lua

B)

Split the project into modules based on what each part does

C)

One function with 20,000 lines

D)

One variable for everything

✅ Answer: B

💡 Big software is built by breaking big problems into small pieces. Modules are one of the main tools that make that possible.


🧠 Pareto Cheat Sheet

SituationBest Practice
Inventory codeInventory.lua
Player codePlayer.lua
Enemy codeEnemy.lua
Save systemSave.lua
AudioAudio.lua
UIUI.lua

🚨 The Only 20% You Actually Need to Remember

You do not need to memorize require() or advanced module syntax yet.

For now, remember just these three ideas:

  1. 📦 One file = One responsibility

  2. 🏷️ Name files by what they do (Player.lua, Shop.lua, Inventory.lua)

  3. ✂️ If one file becomes huge, split it into modules


🎁 Preview of the Next Chapter

So far you've learned:

  • Variables → Store data

  • If → Make decisions

  • Loops → Repeat work

  • Functions → Reuse code

  • Tables → Store collections

  • Strings → Handle text

  • Modules → Organize projects

The next chapter is where these pieces come together into something much more powerful:

Chapter 12 — Object-Oriented Programming (OOP): Building Real Game Objects

After Chapter 12, you'll understand how games represent things like:

  • 👤 Player

  • 👹 Enemy

  • 🚗 Car

  • 🗡️ Weapon

  • 📦 Inventory

This is the chapter where Lua starts to look like the code used in real games.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.