Menu
CoddyTech

Value Equality Metamethods

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

Godot's built-in vector values support coordinate equality, but arbitrary GDScript script instances compare by identity unless you call your own comparison method. GDScript does not let a script class redefine the equality operator. Lua tables also compare by identity by default, yet a shared __eq metamethod can compare their stored coordinates. In Lua 5.1, distinct tables must have the same equality metamethod for this hook to be selected. The examples deliberately contrast a custom GDScript point's identity test with its explicit coordinate comparison, then show a Lua point using coordinate equality directly. Keep identity and value semantics separate when porting.

GDScript 4

extends SceneTree
class Point:
    var x
    var y
    func _init(px, py):
        x = px
        y = py
    func same_coordinates(other):
        return x == other.x and y == other.y
func _init():
    var first = Point.new(2, 3)
    var second = Point.new(2, 3)
    print("same" if first == second else "different")
    print("same" if first.same_coordinates(second) else "different")
    quit()

Lua 5.1

local Point = {}
Point.__index = Point
function Point.new(x, y)
  return setmetatable({x = x, y = y}, Point)
end
function Point.__eq(a, b)
  return a.x == b.x and a.y == b.y
end
local first = Point.new(2, 3)
local second = Point.new(2, 3)
print(first == second and "same" or "different")
print(rawequal(first, second) and "same" or "different")

Porting trap

Do not assume a GDScript custom point behaves like Vector2, or that Lua table fields are compared automatically. Implement the desired value comparison explicitly. Table-valued keys still use raw identity in Lua; a value-equality hook does not merge separately allocated table keys.

challenge icon

Challenge

Easy

Implement point objects with Model.new(x,y) and Model.__eq. Input has x1,y1,x2,y2 as four integer lines. The app uses a == b and returns equal or different.

Edit app.lua, model.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.

Public API checks: Point equality must be symmetric, compare both x and y, and distinguish a change in either coordinate. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.

Try it yourself

-- Public API checks
do
local Model=require("model")
local a,b,c=Model.new(1,2),Model.new(1,2),Model.new(1,3)
assert(a==b and b==a and a~=c and c~=a,"point equality compares both coordinates symmetrically")
local d=Model.new(2,2)
assert(a~=d,"x also participates in equality")
end
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