Kikito's 'middleclass' OOP Solution

Yes, that was a mistake.

Another interesting idea is for more complex components, you can override the __index metamethod to get behavior based upon indexes.

A really useful class that came out of this was a ValueObject which works like an ObjectValue, but for any Lua object. Check out the source.

You can use classes like this for really interesting results:

...
function ActionManager.new()
	local self = setmetatable(BasicPane.new(), ActionManager)
	
	self.ActiveAction = ValueObject.new()
	self.Actions = {}
	
	self.ActionAdded = Signal.new() -- :fire(Action)
	
	self.Maid:GiveTask(ContextActionService.LocalToolEquipped:connect(function(Tool)
		self:StopCurrentAction()
	end))

	self.Maid:GiveTask(self.ActiveAction.Changed:connect(function(Value, OldValue)
		if OldValue then
			OldValue:Deactivate()
		end
	end))
	
	return self
end

function ActionManager:StopCurrentAction()
	self.ActiveAction.Value = nil -- Fires the changed event! 
end
...

This is incredibly useful as state is maintained in a single object – and it’s easy to see how these events connect. Being able to fire/mutate state based upon an internal state is really useful and makes for cleaner code.

The cost is that it’s a bit harder to follow for people not used to events (the observer pattern).

1 Like