lua 5

Chapter 5 — Tables (πŸ”₯ Lua's Superpower)

If you master only one advanced feature in Lua, make it Tables.

Almost everything in Lua—game inventories, players, enemies, settings, maps, menus, JSON data—is built with tables.


🎯 Mission

Imagine you own a backpack.

Without tables:

Sword
Shield
Potion
Apple
Bow
Arrow
Helmet
Gold

You'd need one variable for every item.

❌ Bad idea.

Instead:

inventory = {
    "Sword",
    "Shield",
    "Potion"
}

Now one variable (inventory) can hold many values.

Think of a table as a box with many compartments.


🟒 LEVEL 1 — Survival


Q1

What is a table mainly used for?

A) Storing many related values

B) Printing text

C) Repeating code

D) Making decisions

Answer: A

πŸ’‘ Tables let one variable hold lots of data.


Q2

Which creates a table?

A)

inventory = {}

B)

inventory = ()

C)

inventory = []

D)

inventory = <>

Answer: A

πŸ’‘ Lua uses curly braces {}.


Q3

inventory = {
    "Sword",
    "Shield",
    "Potion"
}

How many items are inside?

A) 1

B) 2

C) 3

D) 4

Answer: C


Q4

How do you access the first item?

A)

inventory[0]

B)

inventory[1]

C)

inventory.first

D)

inventory(1)

Answer: B

πŸ’‘ Lua starts counting from 1, not 0.

⚠️ This is one of the biggest beginner mistakes.


Q5

inventory = {
    "Sword",
    "Shield"
}

print(inventory[2])

Output?

A)

Sword

B)

Shield

C)

2

D)

nil

Answer: B


Q6

inventory = {}

print(inventory[1])

Output?

A)

0

B)

Empty

C)

nil

D)

1

Answer: C

πŸ’‘ No item exists there yet.


🟑 LEVEL 2 — Think Like a Programmer


Q7

inventory = {
    "Sword",
    "Shield",
    "Potion"
}

print(inventory[3])

Output?

A)

Sword

B)

Shield

C)

Potion

D)

nil

Answer: C


Q8

inventory = {
    "Sword",
    "Shield"
}

inventory[2] = "Bow"

print(inventory[2])

Output?

A)

Shield

B)

Bow

C)

Sword

D)

nil

Answer: B

πŸ’‘ You replaced the second item.


Q9

numbers = {
    10,
    20,
    30
}

print(numbers[1] + numbers[3])

Output?

A) 20

B) 30

C) 40

D) 50

Answer: C

πŸ’‘ 10 + 30 = 40.


Q10

pets = {
    "Dog",
    "Cat"
}

pets[3] = "Rabbit"

print(pets[3])

Output?

A)

Rabbit

B)

Cat

C)

Dog

D)

nil

Answer: A


Q11

scores = {
    50,
    80,
    90
}

scores[1] = scores[1] + 10

print(scores[1])

Output?

A) 50

B) 60

C) 80

D) 90

Answer: B


Q12

Which statement is true?

A) Tables can only store numbers.

B) Tables can only store strings.

C) Tables can store different data types together.

D) Tables store only one value.

Answer: C

πŸ’‘ Tables are flexible.


🟠 LEVEL 3 — Real-Life Programming


Q13

A player has 10 weapons.

Best choice?

A) 10 variables

B) One table

C) 10 functions

D) One loop

Answer: B


Q14

A class has 50 student names.

Best choice?

A) 50 variables

B) One table

C) 50 functions

D) 50 loops

Answer: B


Q15

Which is better?

A)

weapon1 = "Sword"
weapon2 = "Bow"
weapon3 = "Axe"

B)

weapons = {
    "Sword",
    "Bow",
    "Axe"
}

Answer: B

πŸ’‘ Cleaner, shorter, easier to manage.


Q16

You're making a music playlist.

Best tool?

A) Boolean

B) Table

C) Function

D) if

Answer: B


Q17

A game inventory changes constantly.

Best tool?

A) Variable

B) Table

C) while

D) Boolean

Answer: B


Q18

A shopping cart stores many products.

Best choice?

A) Table

B) Function

C) Loop

D) if

Answer: A


πŸ”΄ LEVEL 4 — Bug Hunter


Q19

inventory = []

What's wrong?

A) Lua doesn't use [] to create tables.

B) Missing if

C) Missing end

D) Nothing

Answer: A


Q20

inventory = {}

inventory[0] = "Sword"

print(inventory[1])

Output?

A)

Sword

B)

nil

C)

0

D) Error

Answer: B

πŸ’‘ Lua arrays normally start at 1, not 0.


Q21

inventory = {
    "Sword"
}

print(inventory[2])

Output?

A)

Sword

B)

2

C)

nil

D) Error

Answer: C


Q22

inventory = {}

inventory[1] = "Sword"

inventory[1] = "Bow"

print(inventory[1])

Output?

A)

Sword

B)

Bow

C)

Sword Bow

D)

nil

Answer: B

πŸ’‘ The old value gets replaced.


Q23

numbers = {
    5,
    10
}

print(numbers[1] * numbers[2])

Output?

A) 10

B) 15

C) 50

D) 5

Answer: C


Q24

Which is valid?

A)

player = {}

B)

player = []

C)

player = ()

D)

player = <>

Answer: A


🟣 LEVEL 5 — Boss Battle πŸ‘‘


Q25

What is the biggest advantage of tables?

A) Store many related values in one variable.

B) Replace loops.

C) Replace functions.

D) Replace if.

Answer: A


Q26

Lua tables usually start counting from...

A) 0

B) 1

C) -1

D) Depends on the computer

Answer: B

⚠️ This is the #1 thing to remember about Lua tables.


Q27

items = {
    "Apple",
    "Banana",
    "Orange"
}

print(items[2])

Output?

A)

Apple

B)

Banana

C)

Orange

D)

nil

Answer: B


Q28

Which real-life thing is most like a table?

A) A light switch

B) A backpack with many pockets

C) A calculator

D) A clock

Answer: B

πŸ’‘ One container holding many related things.


Q29

Which is easier to manage?

A)

coin1
coin2
coin3
coin4
coin5

B)

coins = {
    10,
    20,
    30,
    40,
    50
}

Answer: B


Q30 — Final Boss

You're making an RPG game.

A player has:

  • Name

  • Level

  • Health

  • Mana

  • Gold

  • Inventory

  • Quests

  • Skills

What's your first thought?

A) Make 8 separate variables.

B) Put everything inside a table.

C) Use only functions.

D) Use only loops.

Answer: B

πŸ’‘ In real Lua projects, a player is almost always represented as a table because it groups all related information together.


🧠 Pareto Cheat Sheet

SituationUse
Create a table{}
Get first itemtable[1]
Change an itemtable[2] = value
Empty table{}
Missing itemnil
Lua starts counting at1

πŸ† Boss Tip (The Most Important Thing in This Chapter)

So far you've learned:

  • Variables → Store one value.

  • Functions → Group many actions.

  • Tables → Group many values.

That's a powerful mental model you'll keep using throughout Lua. In later chapters, you'll combine tables with loops and functions to build inventories, players, enemies, menus, and much more.

No comments:

Post a Comment

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