Couple of days ago someone recommended using the command pattern and just now I finished reading a guide and tried implementing it into Roblox and I wanted to know if it was correct.
This command class is probably unnecessary, but made it anyways to follow along with the guide.
local Command = {}
Command.__index = Command
function Command.new()
local self = setmetatable({},Command)
return self
end
function Command:Execute()
-- shouldn't do anything
end
return Command
local Light = {}
Light.__index = Light
function Light.new()
local self = setmetatable({},Light)
return self
end
function Light:On()
print("Light is on")
end
function Light:Off()
print("Light is off")
end
return Light.new()
local Command = require(script.Parent:WaitForChild("Command"))
local LightOnCommand = setmetatable({},Command)
LightOnCommand.__index = LightOnCommand
function LightOnCommand.new(light)
local self = setmetatable(Command.new(),LightOnCommand)
self.light = light
return self
end
function LightOnCommand:Execute()
-- print("LightOnCommand:Execute()")
self.light:On()
end
return LightOnCommand
local UIS = game:GetService("UserInputService")
local Light = require(script.Parent:WaitForChild("Light"))
local LightOnCommand = require(script.Parent:WaitForChild("LightOnCommand"))
local LightOffCommand = require(script.Parent:WaitForChild("LightOffCommand"))
local keyE = LightOffCommand.new(Light)
local keyR = LightOnCommand.new(Light)
UIS.InputBegan:Connect(function(inputObject, gameProcessed)
if gameProcessed then return end
if UIS:IsKeyDown(Enum.KeyCode.E) then keyE:Execute() end
if UIS:IsKeyDown(Enum.KeyCode.R) then keyR:Execute() end
end)