{"id":"vocksel/class","name":"class","scope":"vocksel","platform":"roblox","description":"Mirrored from the Wally registry.","version":"0.1.0","latest":"0.1.0","versions":["0.1.0"],"license":"MIT","licenseRating":"safe","licenseCaveats":[],"licenseVerified":true,"dependencies":{},"integrity":"a077f34a3796b242162d2cbaadd059d4a2b650c402cb372d8824c851daf14d7b","likes":0,"downloads":0,"install":"forest install vocksel/class","url":"https://forest.dev/p/roblox/vocksel/class","files":"https://api.forest.dev/ai/package/roblox/vocksel/class/files","readme":"# class()\n\nA super simple module for defining Lua classes.\n\nThis was designed for use with [Roblox](https://roblox.com), and as such follows\ntheir commonly used OOP conventions, but it works just fine in any Lua project.\n\n## Usage\n\n```lua\nlocal class = require(game.ReplicatedStorage.Class)\n\nlocal Person = class(\"Person\")\n\n-- This method defines the parameters your class takes\nfunction Person:__init(name, age)\n\tself.Name = name\n\tself.Age = age\nend\n\nfunction Person:Greet()\n\tprint(\"Hi, my names \" .. self.Name .. \" and I'm \" .. self.Age .. \" years old.\")\nend\n\n-- To instantiate, you call the `new` function\nlocal person = Person.new(\"John\", 18)\n\nperson:Greet() -- \"Hi, my names John and I'm 18 years old.\"\n```\n\n## Inheritance\n\nTo make a class inherit another, you pass in the class you want to be inherited\nas the second argument.\n\nThe only thing you need to do manually is call the `__init` method of the\nsuperclass, otherwise you won't inherit the super's properties.\n\nThe superclass is also returned after the newly created class so that you can\ncreate a reference to it. This saves you the trouble of using a direct reference\nto the superclass, and having to change it everywhere if you want to inherit\nsomething else.\n\n```lua\n-- This example continues from the one above\n\nlocal Guy, super = class(\"Guy\", Person)\n\nfunction Guy:__init(name, age, manliness)\n\t-- This calls Person.__init on this instance, and sets the `Name` and `Age`\n\t-- properties for you.\n\tsuper.__init(self, name, age)\n\n\t-- Now we can extend it and add on our properties.\n\tself.manliness = manliness\nend\n\nfunction Guy:AttackABearOrWhateverGuysDo()\n\tif self.manliness > 50 then\n\t\tprint(\"I could totally fight that bear.\")\n\telse\n\t\tprint(\"Here lies \" .. self.Name .. \". He tried to fight a bear.\")\n\tend\nend\n\nlocal guy = Guy.new(\"John\", 18, math.huge)\n\nguy:Greet() -- \"Hi, my names John and I'm 18 years old.\"\nguy:AttackABearOrWhateverGuysDo() -- \"I could totally fight that bear.\"\n```\n\nIf your subclass does not change anything about the parameters it takes, you\ndon't need to create an `__init` method for it. The subclass will automatically\nuse the `__init` of the superclass.\n\n## Contributing\n\nInstall [Rojo](https://github.com/rojo-rbx/rojo/) and then run the following commands:\n\n```sh\n$ rojo build -o place.rbxlx\n$ rojo serve\n```\n\nOpen the newly generated place file and start the Rojo plugin.\n\nFrom here you can modify anything under `src/` and your changes will be synced in.\n\nWhen you're ready to test, simply press F5 to play the game.\n","readmeTruncated":false}