What's a good method to creating class objects?

I have a few objects such as buildings and npcs that I create when my game starts, however, my current method is pretty clunky since I use tags and attributes which is sometimes a pain because if I forget to tag or add an attribute or make a typo it won’t work so I was wondering if there’s some other more efficient method I could use.

This is my current code that creates the interactable objects in my game

	local function createInteractableObject(model)
		local objectType = model:GetAttribute("ObjectType")
		if objectTypes[objectType] then
			local obj = Factory.create(objectTypes[objectType],model)

			if obj.positions then
				for _,position in pairs(obj.positions) do
					self.octree:CreateNode(position, obj)
				end
			elseif obj.position then
				self.octree:CreateNode(obj.position, obj)
			end
		end		
	end
	
	for _,model in pairs(CollectionService:GetTagged("Interactable")) do
		createInteractableObject(model)
	end
function Factory.create(objectType,model,...)
	if objectType == objectTypes.CustomizationStation then
		return CustomizationStation.new(model)
	end
	
	if objectType == objectTypes.NPC then
		return NPC.new(model)
	end
	
	if objectType == objectTypes.Building then
		local buildingData = {
			interiorModel = model.Interior,
			exteriorModel = model.Exterior,
			insidePart =  model.Interior.Inside,
			outsidePart = model.Exterior.Outside
		}

		return Building.new(buildingData)
	end
end

I also use a similar system for my entities or objects whatever.

Usually for this problem its not really a problem with the system but the interface and how you use it.

For example collection services is hard to use without the tag editor plugin which is default now I believe. Consequently a custom made debug system would be a good idea in this scenario.

Another idea is automatation, such as if there is a commonality such as an instance name like “Factory” which you can detect to give the attribute.

Perhaps if its in a folder of factory models in replicated storage you can automatically loop through give the tag and such and check using :FindAncestor. Much easier to drag and drop rather than manually adding the attribute.

Just some ideas.

1 Like