Requiring a Lua file multiple times always returns the same thing. It is cached by Lua. What you probably want is either a stateless Lua module or more of an OOP style Lua module. The stateless one would look like this:
-- car_stateless.lua
local M = {}
function M.new(speed, acceleration, weight, color)
return {
speed = speed,
acceleration = acceleration,
weight = weight,
color = color
}
end
function M.repaint(car, color)
car.color = color
end
function M.strip_seats(car)
car.weight = car.weight - 50
end
function M.upgrade_engine(car)
car.speed = car.speed + 10
car.acceleration = car.acceleration + 110
end
return M
And used like this:
local car = require "car"
local volvo = car.new(220, 800, 1500, "blue")
local ferrari = car.new(320, 1800, 1100, "red")
car.repaint(ferrari, "yellow")
car.upgrade_engine(volvo)
Or more of an OOP-style:
-- car_oop.lua
local M = {}
function M.new(speed, acceleration, weight, color)
local car = {
speed = speed,
acceleration = acceleration,
weight = weight,
color = color,
}
function car.repaint(color)
car.color = color
end
function car.strip_seats()
car.weight = car.weight - 50
end
function car.upgrade_engine()
car.speed = car.speed + 10
car.acceleration = car.acceleration + 110
end
return car
end
return M
Used like this:
local car = require "car"
local volvo = car.new(220, 800, 1500, "blue")
local ferrari = car.new(320, 1800, 1100, "red")
ferrari.repaint("yellow")
volvo.upgrade_engine()