#Dataclass in luau in Roblos Studio

1 messages · Page 1 of 1 (latest)

surreal heart
#

Hey all, so I am super new to Roblox Studio and I have a pretty simple question
I want to make sure I understand stuff correctly and wanted to ask, what is the best way make a "Dataclass" like in Roblox Studio?

Say I have an inventory system, how could I implement a simple class to just hold an "InvetoryItem" and its properties? so for example, if I had to do it in python, it would look something like -

from dataclasses import dataclass

@dataclass
class InvetoryItem:
  name: str
  amount: int
  ...

Also, I know that in this case I should probably use ModuleScript, but what's the reasoning behind it?
Thank you all very much in advance!

idle relic
#

im not the best scripter here by i think you can probably use nested dictionaries

#

inside a module script if you want wide accessibility

stuck cobalt
# surreal heart Hey all, so I am super new to Roblox Studio and I have a pretty simple question ...

First of all, VERY good post!
There are a few ways to accomplish this.
A good way is to use metatables and lua OOP if you wanna create different objects. Something like this (in a module script)

local InventoryItem = {}
InventoryItem.__index = InventoryItem

function InventoryItem.new(name: string, amount: number)
    local self = setmetatable({}, InventoryItem)
    self.name = name
    self.amount = amount
    return self
end

function InventoryItem:GetDisplayName()
    return string.format("%s (x%d)", self.name, self.amount)
end

return InventoryItem
local InventoryItem = require(moduleScriptPath)

local bottle = InventoryItem.new("Glass Bottle", 1)
print(bottle.name)

And you should also look into some OOP guides!

https://www.lua.org/pil/16.html

stuck cobalt
idle relic
#

thaks for the kind words

stuck cobalt
surreal heart
#

Either way I will for sure take the time to look more into OOP, thank you!

surreal heart
#

I see I see

surreal heart
#

Is it possible to add type hinting to metatables?
from my understanding, they are like dynamic data classes, so my IDE (in this case Roblox Studio) won't be able to to auto complete me for example, or give me warnings about wrong types

stuck cobalt
# surreal heart Is it possible to add type hinting to metatables? from my understanding, they ar...
surreal heart
#

Awesome, thank you!

stuck cobalt
spice snow
surreal heart
#

I'd love to hear alternative ways if you could

cunning nymphBOT
#

studio** You are now Level 1! **studio

stuck cobalt
lyric onyx
# surreal heart Is it possible to add type hinting to metatables? from my understanding, they ar...

yes, you can enable strict type checking mode by writing —!strict at the top of your script.

You can also define variable types as well as casting:

local x: Number = 5 —defining type

local x = calculate() :: Number — casting

Highly recommend you look up the documentation on typechecking, there’s a ton of cool stuff like union types and custom enums you can use, as well as exporting types for use elsewhere

stuck cobalt
lyric onyx
#

you can effectively get the same result with or types

#

type myEnum = “hello” | “goodbye”

#

I forgot the documentation name of these, but this is how I use it

spice snow
#

it does

#

just using like tables

stuck cobalt
# spice snow it does

not in the same way Java does. it’d be better if you could just do the following

enum myEnum {
  enum1,
  enum2
}
#

and again they don’t have built in custom enum support.

lyric onyx
#

it functionally does the exact same thing

#

as what I just described

stuck cobalt
# lyric onyx it functionally does the exact same thing

No. What you described is type restriction, it doesn't create a runtime object or enum, it just tells the type checker what values are allowed for a specific variable, and what is not.

local greeting: myEnum = "hello"  -- valid
local greeting: myEnum = "hi"     -- invalid
#

The closest you can get in any conventient way is just using tables, but those aren't enums

lyric onyx
#

it does the same damn thing bro

stuck cobalt