Creating a class from scratch

Posted by Ashleykitsune on Sun 26 Jul 2020 07:39 PM — 2 posts, 14,014 views.

#0
Hello, I was planning on creating a module for my muck I play where I would use simple buttons, based off the button code I found in the forum here:
http://gammon.com.au/forum/?id=9359
This is a plugin however, and I was hoping to create a class module for Button where I can use it to create a much of buttons, and enable them and disable them as I click through them.
For instance, a menu might start off with 3 buttons: spells, skills, and actions. You could click the spells button and the three buttons would become disabled, and replaced with 4 new buttons, Home (return to the first three), Torch, Cure, and Fire, for example.

It's been many years since I've used Mushclient and so I'm trying to get back into it, but after searching around for creating a class for Mushclient, I'm not entirely sure how to do this.
Can someone point me to a post somewhere that discusses this?
~Ash


So far I have a class started and it looks like this:

function Button:new (enabled, icon, tooltip, send, cooldown, sound)

  self.enabled = enabled,  -- displays only when enabled
  self.icon = icon,        -- icon image
  self.tooltip = tooltip,  -- tooltip help text
  self.send = send,        -- what to send
  self.cooldown = cooldown,-- cooldown time in seconds
  self.sound = sound,      -- sound to play when cast
  local Enable = function ()
     self.enabled = true
  end
  local Disable = function ()
     self.enabled = false
  end
end
Amended on Sun 26 Jul 2020 08:56 PM by Ashleykitsune
USA Global Moderator #1
Because Lua doesn't have an innate way to develop classes, people have come up with a million different ways to do it.

One I use is this pattern, which is specifically tailored for MUSHclient:


MyClass = {
   hotspot_map = {}  -- we need this if we want to have miniwindow hotspots bound to our class instances, because hotspot callbacks can't invoke a specific instance
   -- global class variables go here
}
MyClass_defaults = {
   -- default instance variables go here
}
MyClass_meta = { __index = MyClass }


-- call MyClass.new() to get a new instance of MyClass
function MyClass.new()
   new_instance = setmetatable(copytable.deep(MyClass_defaults), MyClass_meta)
   -- you could also assign default instance variables here instead of in MyClass_defaults as well as assign custom ones by passing arguments into new
   return new_instance
end


-- call some_instance:functionCalledOnInstance() to do this for a particular instance
function MyClass:functionCalledOnInstance()
   -- this function has a `self` variable that refers to the instance
end


-- call some_instance:bindHotspot(hotspot_id) to bind a miniwindow hotspot to a particular class instance for callbacks
function MyClass:bindHotspot(hotspot_id)
   MyClass.hotspot_map[hotspot_id] = self
end


-- "MyClass.hotspotCallbackFunction" can be assigned as a miniwindow hotspot callback
function MyClass.hotspotCallbackFunction(flags, hotspot_id)
   -- this function does not have a `self` variable, so we retrieve the instance from the class hotspot map
   local some_instance = MyClass.hotspot_map[hotspot_id]
   -- now you can do stuff with some_instance after a hotspot callback
end