#Class

1 messages · Page 1 of 1 (latest)

lean island
#
  Modules = {
    class = "github.com/Boumety/module/class:acbfafe"
  }

Create classes that you can make objects inherit from. It allows you to not repeat code.

class(config) This function return a class using config (table). You can then create then create objects that inherit that class. Check example below.
You can use 'init' that is called when the object is created and other properties like Tick, Name etc.

Example:

Client.OnStart = function()
    plant = class({
        grow = function(self)
            self:SetParent(nil)
        end,
    })

    plant = plant(Shape(Items.voxels.wheat))
    plant:grow()
end

https://github.com/Boumety/module/tree/main/class

GitHub

Contribute to Boumety/module development by creating an account on GitHub.

ocean anvil
#

i think that syntax is bad for classes

#

you can make plant = class({}) instead of class:createClass("plant") and wheat = class(plant, {}) instead of class:inherit

#

and creating object from class will be possible from plant(...) or wheat(...)

lean island
ocean anvil
#

uhh

ocean anvil
# lean island Something like that? ```lua plant = class({ grow = function() end, }) ...
-- creating classes
plant = class({
  __init__ = function(self, shape_name)
    self.shape = Shape(shape_name)
  end
})

local wheat = class(plant, {
  __init__ = function(self, shape_name)
    self.super().__init__(self, shape_name)
  end,
  grow = function(self)
    -- some functionality
  end
})

-- creating objects
cool_plant = plant(Items.Boumety.Cool_plant) --works, does plant __init__
cool_wheat = wheat(Items.Boumety.Cool_wheat) --works, does plant __init__ first then wheat __init__

cool_wheat:grow()
#

@lean island

lean island
ocean anvil
#

wheat.super() returns plant because wheat is 'child' of plant

#

plant.super() returns nil because this is original class

lean island
#

I'll need to try

ocean anvil
lean island
lean island
# ocean anvil you will need to use many metatables

alright I think it's better

    -- module start
    local class = {}

    local mt_class = {
        __call = function(t, shape)
            for k, v in pairs(t) do
                shape[k] = v
            end

            return shape
        end
    }

    local mt = {
        __call = function(_, myClass)
            setmetatable(myClass, mt_class)
            return myClass
        end
    }


    setmetatable(class, mt)
    -- module end

    plant = class({
        grow = function()
            print(true)
        end,
    })

    wheat = plant(Shape(Items.voxels.wheat))
    wheat:grow() -- true
ocean anvil
#

yeah, it`s better

#

but i think you need to add __init__ and it`s call i think

#

like

local mt = {
  __call = function(_, myClass, ...)
    setmetatable(myClass, mt_class)
    myClass:__init__(...)
    return myClass
  end
}
#

... is magic arg getter

#

you can parse all next args with ... (but only once in a defined function tree (functions in a function))

#

so it will work like this:

wheat = plant(Shape(Items.voxels.wheat), "hello", "world")

... will be something like {"hello", "world"} (not a table)

#

@lean island

lean island
ocean anvil
#

runs on object when created from class

#

like once

lean island
#

yeah like enter but I don't need it no? I can just have a table as a config

ocean anvil
#

you can't run for loops in a table i guess

#

like when you create plant from shape, you can loop in pallete colors

#

i guess

#

something like that

#

but you can still just do object:init() when loaded from class

#

but my thing will be easier to use

#

without any other function calls

#

but good job

#

i think i made class thing

#

but idk where it is

lean island
#

I just want an object to inherit functions, variables etc. but after that you can do whatever you want with it like changing its health and other stuff (in another line of code)

lean island
#

thank you for your help, i've started to learn metatable thanks to you