Is there a better way to do this?

Hello, I have been working on a cutscene module for fun, but Im wondering if there’s a better way to define the types

local CutsceneService = {}
CutsceneService.__index = CutsceneService

local CutsceneCode = {}
CutsceneCode.__index = CutsceneCode

local temp = {
	["Name"]=nil,
	["Lenght"]=0}
setmetatable(temp,CutsceneCode)

type Cutscene = typeof(temp)

function CutsceneService.new(CutsceneName:string,CutsceneLength:number)
	local tab = {Name = CutsceneName,Lenght = CutsceneLength}
	setmetatable(tab,CutsceneCode)
	local c:Cutscene = tab
	return c
end

function CutsceneCode:Play()
	print("Playing: "..self.Name)
end

return CutsceneService
1 Like
local CutsceneService = {}
CutsceneService.__index = CutsceneService

local CutsceneCode = {}
CutsceneCode.__index = CutsceneCode

function CutsceneService.new(CutsceneName, CutsceneLength)
    local tab = setmetatable({
        Name = CutsceneName,
        Length = CutsceneLength
    }, CutsceneCode)
    return setmetatable(tab, CutsceneService)
end

function CutsceneCode:Play()
    print("Playing: " .. self.Name)
end

return CutsceneService

I’ve removed the global temp variable and the type definition for Cutscene . Instead, we directly set the metatable for tab to both CutsceneService and CutsceneCode .

I’ve also fixed the typo “Lenght” to “Length” for consistency.

You can basically use it like this:

local CutsceneService = require("CutsceneService")

local myCutscene = CutsceneService.new("My Cutscene", 60)
myCutscene:Play()

I wanted to use types but this works too!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.