How should I set this up to do it as OOP

I’m starting development on a new game, a rather large one and I’ve also begun learning how to use OOP in Roblox.

I am making a moduleScript that will control the effects of what happens when you interact with different gui objects. Since I want all my TextButtons to share similar functions I thought this would be a good idea. But I find I am declaring the same variables over and over for each function so it knows what things are.

How would I go about making this more OOP so that I am not repeating code.

local gui = {}

gui.mouseEnter = function(buttonFrame)
	local button = buttonFrame.Button
	local background = buttonFrame.Background
	local outline = buttonFrame.Outline
	
	print'entered'
end

gui.mouseLeave = function(buttonFrame)
	local button = buttonFrame.Button
	local background = buttonFrame.Background
	local outline = buttonFrame.Outline
	
	print'left'
end

gui.mouseClick = function(buttonFrame)
	local button = buttonFrame.Button
	local background = buttonFrame.Background
	local outline = buttonFrame.Outline
	
	print'ciick'
end

return gui

Have you learned how to write a class in Lua? If you have a bunch of similar buttons, you can create a class for it and then every time you want to create a button, you can execute one line of code to create an instance. However, creating classes involve metatables. Since it’s simple use of metatables and metamethods and the procedures are extremely similar for all classes, some developers memorize the lines before understanding metatables and metamethods and then go on to learn metatables later on in their development careers.

To create an optimal class that supports overriding in subclasses, you’d want to create and connect the events in the constructor, which could reference some overridable instance functions in the class. This goes with the OOP principle of flexibility. An example:

local BasicButton = {} -- class
BasicButton.__index = BasicButton -- metamethods common to all classes

function BasicButton.new(button) -- constructor function returns instance. Its parameter is the button object
    local self = setmetatable({}, BasicButton) -- the instance itself
    self.Button = button -- Button is now a property aka instance variable of the instance
    -- Connecting the events
    self.Button.MouseEnter:Connect(function()
        self:MouseEnter() -- fires instance function
    end)
    --- etc, etc, all other events I won't be typing for you
    return self -- remember the constructor returns the instance
end

local BasicButton:MouseEnter() -- the overridable instance function
    -- your code for this event here
    -- Use self.Button to reference the button object in instance functions
    -- The colon allows you to use the self keyword
end

return BasicButton

This is the basic class format. I’d be happy to answer any questions you have regarding this :slight_smile:

6 Likes

This was exactly what I was looking for, I had recently learnt how metatables work but I hadn’t really been good at applying them in my own work.

Thank you.