Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ Lua
➜ Creating a class from scratch
Creating a class from scratch
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Ashleykitsune
(33 posts) Bio
|
Date
| Sun 26 Jul 2020 07:39 PM (UTC) Amended on Sun 26 Jul 2020 08:56 PM (UTC) by Ashleykitsune
|
Message
| 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
| Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #1 on Mon 27 Jul 2020 01:31 PM (UTC) Amended on Mon 27 Jul 2020 02:14 PM (UTC) by Fiendish
|
Message
| 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
|
https://github.com/fiendish/aardwolfclientpackage | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
9,436 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top