D15c4rd_hboogy101

quick (sub)class creation in lua

Oct 17th, 2025
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 2.07 KB | Source Code | 0 0
  1. #Requires AutoHotkey v2.0
  2.  
  3. ::``class::{
  4.     Send("{Backspace}")
  5.     getInput(false, &className, &ph)
  6.  
  7.     writeConstructor(className, false)
  8.     doubleEnter()
  9.     writeFunctionsAndReturn(className)
  10. }
  11.  
  12. ::``subclass::{
  13.     Send("{Backspace}")
  14.     getInput(true, &thisClass, &superClass)
  15.    
  16.     writeConstructor(thisClass, superClass)
  17.     doubleEnter()
  18.     writeFunctionsAndReturn(thisClass)
  19. }
  20.  
  21. getInput(subClass, &firstInput, &secondInput) {
  22.     local firstHook := createHook()
  23.     firstHook.Start()
  24.     firstHook.Wait()
  25.  
  26.     if not subClass {
  27.         firstInput := firstHook.Input
  28.  
  29.         return firstHook.Input
  30.     }
  31.  
  32.     local secondHook := createHook()
  33.     secondHook.Start()
  34.     secondHook.Wait()
  35.  
  36.     firstInput := firstHook.Input
  37.     secondInput := secondHook.Input
  38. }
  39.  
  40. createHook() {
  41.     return InputHook(,"{Space}{Enter}")
  42. }
  43.  
  44. doubleEnter() {
  45.     Send("{Enter}{Enter}")
  46. }
  47.  
  48. writeConstructor(thisClass, superClass) {
  49.     writeDefinition(thisClass, superClass)
  50.     Send("{Enter}")
  51.     Send(thisClass . ".__index = " . thisClass)
  52.     doubleEnter()
  53.     Send("function " . thisClass . ".new()")
  54.     Send("{Enter}")
  55.     writeSelf(thisClass, superClass)
  56.     Send("{Enter}{Tab}{Enter}")
  57.     Send("{Tab}return self")
  58.     Send("{Enter}")
  59.     Send("end")
  60. }
  61.  
  62. writeFunctionsAndReturn(className) {
  63.     Send("function " . className . ":init()")
  64.     Send("{Enter}{Tab}{Enter}")
  65.     Send("end")
  66.     doubleEnter()
  67.     Send("function " . className . ":destroy()")
  68.     Send("{Enter}{Tab}{Enter}")
  69.     Send("end")
  70.     doubleEnter()
  71.     Send("return " . className)
  72. }
  73.  
  74. writeDefinition(thisClass, superClass) {
  75.     if not superClass {
  76.         Send("local " . thisClass . " = {{}{}}")
  77.     } else {
  78.         Send("local " . thisClass . " = setmetatable({{}{}}, " . superClass . ")")
  79.     }
  80. }
  81.  
  82. writeSelf(thisClass, superClass) {
  83.     Send("{Tab}")
  84.      if not superClass {
  85.         Send("local self = setmetatable({{}{}}, " . thisClass . ")")
  86.      } else {
  87.         Send("local self = setmetatable(" . superClass . ".new(), " . thisClass . ")")
  88.      }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment