Back

Activity 1.3: Naming Your Numbers

divider

Activity 1.3

Naming Your Numbers

Key Concepts

A Name for a Value

Change It Once, Change It Everywhere

Doing Arithmetic With a Name

Where We Left Off

The traffic signal from the previous activity, centered in the window

Where We Left Off

The same traffic signal, now in the upper left of the window

Same shapes, moved. Today that is a two-number change, not an eight-number change.

The Problem With Yesterday's Program

love.graphics.circle("fill", 400, 190, 35)
love.graphics.circle("fill", 400, 300, 35)
love.graphics.circle("fill", 400, 410, 35)

The Problem With Yesterday's Program

love.graphics.circle("fill", 400, 190, 35)
love.graphics.circle("fill", 400, 300, 35)
love.graphics.circle("fill", 400, 410, 35)

To move the signal sideways you edit every one of those 400s, plus the rectangle.

Miss one and the picture comes apart.

Give the Number a Name

local lightX = 400
love.graphics.circle("fill", lightX, 190, 35)
love.graphics.circle("fill", lightX, 300, 35)
love.graphics.circle("fill", lightX, 410, 35)

One place to change. That is the whole idea.

What That Line Does

local lightX = 400
local
Make a new name.
lightX
The name. You choose it.
=
Put the value on the right into the name on the left.
400
The value.

Reading It Right to Left

= is not the equals sign from math class.

It does not say two things are the same. It says put this value in this name, and the arrow only points one way.

Which Is Why This Is Legal

local score = 0
score = score + 10

Which Is Why This Is Legal

local score = 0
score = score + 10

In math, score = score + 10 is nonsense. Here it means: work out the right side, then put the answer back in the name.

score is now 10. The second line does not need local — the name already exists.

Names Can Do Arithmetic

love.graphics.circle("fill", lightX, lightY + gap, radius)

The engine never sees lightY + gap. It gets the answer — one number, worked out first.

Above the Box, Not Inside It

local lightX = 400
local lightY = 190
local radius = 35
local gap = 110

These four lines go at the top of the file, above love.draw.

They run once, when the program starts. Everything in the box runs 60 times a second.

Naming Things Is Not Decoration

lightX tells the next reader what 400 meant. x1 does not, and the next reader is you in November.

On the Create Task you explain your own code from memory. Names are most of how that goes.

Today's Objectives

  • Create a variable and give it a value
  • Use a variable where a number used to be
  • Change a picture by changing one line
  • Do arithmetic with a variable

Key Terms

Variable
A name that holds a value.
local
The word that makes a new variable in Lua.
Assignment
Putting a value into a variable, written with =.
Reassignment
Putting a different value into a variable that already exists.
Hardcoded
A raw number typed straight into the program.

'F' → Fullscreen

divider

Build

Task 1: One Circle, Three Names

Copy template-game and rename the copy 1-03-circle. Replace what is inside love.draw so the file looks like this:

main.lua
local circleX = 400
local circleY = 300
local radius = 40
function love.draw()
love.graphics.circle("fill", circleX, circleY, radius)
end

Run it. A white circle in the middle. Now change one line and run again:

main.lua
local circleX = 150

Then try changing radius, and then circleY. Run after each one.

The variables go above love.draw, not inside it. Keep love.keypressed at the bottom of the file, unchanged, so Escape still closes the window.

Task 2: Name the Signal's Numbers

Copy your 1-02-signal folder and rename the copy 1-03-signal. Add four variables above love.draw:

main.lua
local lightX = 400
local lightY = 190
local radius = 35
local gap = 110

Now replace the numbers inside the box with those names. The rectangle needs arithmetic, because it is placed by its corner and the lights are placed by their centers:

main.lua
local lightX = 400
local lightY = 190
local radius = 35
local gap = 110
function love.draw()
love.graphics.setColor(0.6, 0.6, 0.6)
love.graphics.rectangle("fill", lightX - 50, lightY - 60, 100, 340)
love.graphics.setColor(0.9, 0.2, 0.2)
love.graphics.circle("fill", lightX, lightY, radius)
love.graphics.setColor(0.9, 0.8, 0.2)
love.graphics.circle("fill", lightX, lightY + gap, radius)
love.graphics.setColor(0.2, 0.8, 0.3)
love.graphics.circle("fill", lightX, lightY + gap + gap, radius)
end

Run it. The picture should look exactly like it did last session. Nothing changed on screen, and that is how you know you did it right.

Task 3: Move the Whole Thing

Change two lines, and only those two:

main.lua
local lightX = 200
local lightY = 120
The traffic signal drawn in the upper left of the window instead of the middle

Four shapes moved, and you touched two numbers. Now set gap to 80 and radius to 25 and see what happens.

Put lightX and lightY back to 400 and 190 before you submit, or leave the signal wherever you like it — but write down which you chose.

Challenge (Optional): Break It on Purpose

Change one variable name in the red light's line so it no longer matches the one at the top of the file — a lower case x instead of a capital one:

main.lua
love.graphics.setColor(0.9, 0.2, 0.2)
love.graphics.circle("fill", lightx, lightY, radius)

The window turns blue and shows this. Press Escape to dismiss it:

Error
Error: main.lua:11: bad argument #2 to 'circle' (number expected, got nil)

Lua did not say "there is no such variable." It said the circle got nil where it wanted a number. Write down what you think nil means, then fix the spelling.

A name you never made is not an error in Lua. It is a name holding nothing, and you find out later, somewhere else.

Challenge (Optional): One Number, Whole Scene

Copy your 1-02-scene from last session to 1-03-scene and rebuild it so that one variable moves the entire drawing and one more variable changes its size. Every shape has to be positioned from those two.

divider

Same Idea, Exam Notation

The exam does not use Lua. It uses its own written notation, and you get a booklet of it on exam day. Assignment is the first thing on that sheet.

Lua
local lightX = 400
local gap = 110
lightX = lightX + 50
print(lightX)
Exam Notation
lightX ← 400
gap ← 110
lightX ← lightX + 50
DISPLAY(lightX)

An arrow instead of an equals sign

LuaExam notation
Make a variablelocal lightX = 400lightX ← 400
Change itlightX = lightX + 50lightX ← lightX + 50
Show itprint(lightX)DISPLAY(lightX)

There is no local. The arrow makes the variable the first time you use it, and changes it every time after.

On the exam, = means "is equal to". It is a question, not an instruction — the thing Lua writes as ==. Assignment is always the arrow, and comparison is always =. They never overlap, so a line with an arrow in it is always storing something.

You will meet = as a comparison properly at session 8. For now, notice that the exam kept them apart and Lua did not.

divider

Checkpoint

  • 1-03-circle draws one circle from three variables
  • 1-03-signal positions every shape from a variable — the only raw numbers left in love.draw are colors, the rectangle's width and height, and the two offsets
  • Changing lightX alone moves all four shapes
  • Every variable is declared above the box, not inside it
divider

Reflection

Answer the following questions before submitting your work.

  1. In task 2 the picture looked identical before and after your changes. Explain what you actually gained, given that a person looking at the screen could not tell you had done anything.
  2. score = score + 10 would be false in math class. Explain what it means in a program, in your own words, and say what score holds afterward if it held 40 before.
  3. You misspelled a variable name on purpose and the error message talked about nil instead of about spelling. Say why you think Lua reported it that way, and where the mistake actually was.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete