Chapter 9 — Tables with Names (Key-Value Tables) ⭐⭐⭐⭐⭐
If Chapter 5 taught you tables...
This chapter teaches you REAL tables.
This is probably the most important Lua chapter after variables, if, loops and functions.
Almost every real Lua project uses this.
🎯 Mission
Imagine you're storing a player.
Instead of this:
player = {
"Pramod",
25,
100,
500
}
Question:
What is player[3]?
🤔 Health?
🤔 Coins?
🤔 Level?
Nobody knows.
Instead...
player = {
name = "Pramod",
level = 25,
health = 100,
coins = 500
}
Now it's obvious.
print(player.health)
Everyone immediately understands.
This is how real Lua code is written.
🟢 LEVEL 1 — Survival
Q1
Which player is easier to understand?
A)
player = {
"Pramod",
25,
100
}
B)
player = {
name = "Pramod",
level = 25,
health = 100
}
✅ Answer: B
💡 Named values are much easier to read.
Q2
Which is called a key?
player = {
health = 100
}
A) 100
B) health
C) player
D) =
✅ Answer: B
Q3
Which is the value?
player = {
health = 100
}
A) health
B) player
C) 100
D) {}
✅ Answer: C
Q4
How do you read the player's health?
A)
player.health
B)
player(health)
C)
health.player
D)
player.health()
✅ Answer: A
Q5
player = {
name = "Pramod"
}
print(player.name)
Output?
A)
Pramod
B)
name
C)
player
D)
nil
✅ Answer: A
Q6
player = {
level = 10
}
print(player.level)
Output?
A) 1
B) 10
C) level
D) nil
✅ Answer: B
🟡 LEVEL 2 — Think Like a Programmer
Q7
player = {
health = 100
}
player.health = 80
print(player.health)
Output?
A) 100
B) 80
C) nil
D) Error
✅ Answer: B
Q8
car = {
brand = "Tesla",
year = 2025
}
print(car.brand)
Output?
A)
Tesla
B)
2025
C)
brand
D) nil
✅ Answer: A
Q9
dog = {
age = 5
}
dog.age = dog.age + 1
print(dog.age)
Output?
A) 5
B) 6
C) 1
D) Error
✅ Answer: B
Q10
phone = {
battery = 75
}
print(phone.camera)
Output?
A) 75
B) 0
C) nil
D) camera
✅ Answer: C
💡 The key doesn't exist.
Q11
Which is easier?
A)
player[4]
B)
player.health
✅ Answer: B
Q12
user = {
username = "Alex",
coins = 500
}
print(user.coins)
Output?
A) Alex
B) 500
C) coins
D) nil
✅ Answer: B
🟠 LEVEL 3 — Real-Life Programming
Q13
You're making an RPG player.
Which is better?
A)
player = {
"Pramod",
20,
100,
300
}
B)
player = {
name="Pramod",
level=20,
health=100,
coins=300
}
✅ Answer: B
Q14
A car has:
Brand
Speed
Fuel
Best choice?
A)
car = {
brand="BMW",
speed=120,
fuel=60
}
B)
car = {
"BMW",
120,
60
}
✅ Answer: A
Q15
A website stores a user.
Best structure?
A)
user = {
name="John",
email="john@mail.com",
age=25
}
B)
user = {
"John",
"john@mail.com",
25
}
✅ Answer: A
Q16
Minecraft stores a player.
Would it likely use named keys?
A) Yes
B) No
✅ Answer: A
Q17
Roblox stores:
Health
WalkSpeed
JumpPower
Best tool?
A) Key-value table
B) Variables only
C) Loop
D) If
✅ Answer: A
Q18
A bank account stores:
Owner
Balance
Account Number
Best approach?
A) Named table
B) Three unrelated variables
C) Loop
D) Function
✅ Answer: A
🔴 LEVEL 4 — Bug Hunter
Q19
player = {
health = 100
}
print(player.Health)
Problem?
A) Lua is case-sensitive.
B) Missing loop.
C) Missing function.
D) Nothing.
✅ Answer: A
💡 health ≠ Health
Q20
player = {
health = 100
}
player.health = "Dead"
Is this allowed?
A) Yes
B) No
✅ Answer: A
💡 Lua allows different data types, although using consistent types is usually better.
Q21
player = {
coins = 500
}
print(player.gold)
Output?
A) 500
B) gold
C) nil
D) Error
✅ Answer: C
Q22
car = {}
car.speed = 100
print(car.speed)
Output?
A) 100
B) nil
C) Error
D) speed
✅ Answer: A
💡 You can add new keys anytime.
Q23
player = {
level = 5
}
player.level = player.level + 5
print(player.level)
Output?
A) 5
B) 10
C) nil
D) Error
✅ Answer: B
Q24
Which is valid?
A)
player.health
B)
player->health
C)
player::health
D)
player=>health
✅ Answer: A
🟣 LEVEL 5 — Boss Battle 👑
Q25
Key-value tables make code...
A) Harder
B) Easier to read
C) Slower
D) Random
✅ Answer: B
Q26
Which is a key?
player = {
coins = 200
}
A) 200
B) coins
C) player
D) {}
✅ Answer: B
Q27
How do you change health to 50?
A)
player.health = 50
B)
health.player = 50
C)
player(health)=50
D)
health = player
✅ Answer: A
Q28
Which is easier to remember?
A)
enemy[7]
B)
enemy.damage
✅ Answer: B
Q29
Which style do professional Lua programmers mostly use?
A)
player[1]
player[2]
player[3]
B)
player.name
player.health
player.level
✅ Answer: B
Q30 — Final Boss 🏆
You're creating a player for an online game.
The player has:
Name
Health
Mana
Coins
Level
Experience
Inventory
Position
Which design would an experienced Lua programmer choose?
A)
player = {
"Pramod",
100,
50,
500,
20,
1500,
{},
{10,20}
}
B)
player = {
name = "Pramod",
health = 100,
mana = 50,
coins = 500,
level = 20,
experience = 1500,
inventory = {},
position = {x=10, y=20}
}
✅ Answer: B
💡 This is how you'll see objects represented in real Lua code and game frameworks.
🧠 Pareto Cheat Sheet
| Need | Syntax |
|---|---|
| Create object-like table | {name="Bob", health=100} |
| Read value | player.health |
| Change value | player.health = 50 |
| Add new value | player.coins = 100 |
| Missing key | nil |
| Nested table | player.position.x |
🏆 Boss Tip (The Biggest Upgrade Yet)
You now know both kinds of tables:
📦 List (ordered values)
inventory = {
"Sword",
"Shield",
"Potion"
}
Access by position:
inventory[1]
Use when order matters.
👤 Object (named values)
player = {
name = "Pramod",
health = 100,
level = 20
}
Access by name:
player.health
Use when each value has a meaningful name.
Real-world Lua programs use both together.
Example:
player = {
name = "Pramod",
health = 100,
inventory = {
"Sword",
"Bow",
"Potion"
}
}
This combines everything you've learned:
Variables
Tables
Nested tables
Named keys
From here on, you're learning patterns used in actual Lua applications rather than just isolated language features.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.