I’m wondering if anyone has any feedback about how I’m trying to set my game up. It’s my first attempt at making something that is modular and object oriented. I’ll share a bit of code from my Tile plot object script, but I want to keep everything private unless something is specifically suggested. Plot items are stored in the ‘PlotItems’ folder, and I set it up so that it’s easy to just add something there and have it in the game without much extra work. all the gui stuff is also set up automatically using the same sort of code on the local side.
Basically, the system gives each player a massive data table with their plot, inventory, and stats, which is saved in a JSON string using a regular datastore; no special DSS module like ds2. The focus of this project was basically to learn to structure a game properly, I’m not too concerned about the final product. I think I’m doing a good job organizing and making sure the code is easily modifiable, but any suggestions are welcome!
(keep in mind that the game is extremely unpolished, and just under 800 lines of code)
Tile Module Script:
local itemIds = require(script.Parent.Parent.ItemIds)
local Tile = {}
Tile.__index = Tile
local plotItems = game.ReplicatedStorage.PlotItems
local tiles = plotItems.Tiles
function Tile:setCrop(crop, state)
if not self.Crop or crop == nil then
return
end
local newCrop = {}
newCrop.Name = crop
newCrop.State = state
newCrop.Object = plotItems.Tiles.Crops[crop]:Clone()
newCrop.Object:SetPrimaryPartCFrame(CFrame.new(self.Object.CenterWeld.Position))
newCrop.Object.Parent = self.Object
self.Crop = newCrop
end
function Tile:clearCrop()
if self.Crop then
self.Crop.Object:Destroy()
self.Crop = {}
end
end
function Tile.new(name, plot, position, size, crop, LoadingData)
local newTile = setmetatable({}, Tile)
newTile.Name = name
newTile.Object = tiles[name]:Clone()
if LoadingData then
newTile.Object.Position = plot.base.CFrame:ToWorldSpace(CFrame.new(position)).Position
else
newTile.Object.Position = position
end
newTile.Object.CenterWeld.Position = newTile.Object.Position + Vector3.new(0,0.6,0)
newTile.Pos = plot.Vector3ToTable(plot:getRelativePosition(newTile.Object))
newTile.Size = plot.Vector3ToTable(Vector3.new(3,3,3))
newTile.Object.Parent = plot.base.Objects.Tiles
newTile.Id = itemIds.new(newTile, plot)
plot.contents.Tiles[newTile.Id] = newTile
newTile.Crop = {}
if crop and crop ~= {} then
--print(crop)
newTile:setCrop(crop.Name, crop.State)
end
--newTile:setCrop("Wheat", 1)
return newTile
end
function Tile:updatePos(plot)
local baseCF = plot.base.CFrame
if plot.TableToVector3(self.Pos) ~= self.Object.Position then
self.Pos = plot.Vector3ToTable(baseCF:ToObjectSpace(CFrame.new(self.Object.Position)))
end
end
return Tile