Menu
CoddyTech

Equality and Identity

Lesson 10 of 48 in Coddy's Lua for GDScript Developers course.

GDScript arrays compare their contents with ==, so separately allocated coordinate arrays can be equal. Plain Lua tables compare identity: two table constructors create different tables even when every field matches. Copying a table reference into another variable does preserve identity. For coordinate data, compare the relevant fields explicitly rather than assuming table equality performs a structural comparison. The same distinction applies when replacing a GDScript value such as a Vector2i with a Lua table: the representation has changed its equality behavior. Lua also keeps number and string equality separate; the number 4 does not equal the string "4". Convert input deliberately before comparison.

GDScript 4

extends SceneTree

func _init():
	var first = [2, 5]
	var second = [2, 5]
	print("same" if first == second else "different")
	quit()

Lua 5.1

local first = {x = 2, y = 5}
local second = {x = 2, y = 5}
print(first == second and "same" or "different")
print(first.x == second.x and first.y == second.y and "same" or "different")

Porting trap

A Lua coordinate record does not inherit GDScript array or vector value equality. Compare a.x == b.x and a.y == b.y when those fields define sameness.

challenge icon

Challenge

Easy

Input is four integer lines x1, y1, x2, y2. Build two coordinate tables and return same when both corresponding fields match, otherwise different.

Edit app.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.

Try it yourself

local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Lua for GDScript Developers

Practice on your own: Online Lua compiler