Best way to create an object attribute system

Hey DevForum… or should I say… DevForum! (couldn’t think of a Halloween pun)

I’m looking for the best ways to create an attribute system that is controlled by the player.

For example, say Bob has a box. Attributes I have implemented for the box are something like box Color.

By default it’s red, but the Player can change it into any other color. The problem I have is looking for the best way to hold the data & save the data.

One solution I have is, I could pretty much save all tags that the box has, but I have a lot of tags to save (multiple boxes, can commonly go over 100), and if the player doesn’t change the box color, I’d rather not waste space by saving the default color of the box.

Let me know if you have any tips/ questions, thanks!

1 Like

If you’re looking to solve the issue of saving a ton of unchanged values, you’ll want to try simply checking them against a set of defaults.

I’m not quite sure how your tag system works, so here’s an example with a simpler property-based system:

--a list of the default properties of items. Included items are just examples.
local DEFAULT_PROPERTIES={
	
	--defaults for a "box" item
	Box={
		BrickColor=BrickColor.new("Medium stone grey");
		Anchored=true;
		--and so on
	}

}
--function that accepts a list of properties and returns the list without defaults
function saveProperties(itemProperties)
	local propertiesToSave={}
	local defaults=DEFAULT_PROPERTIES[itemProperties.Name] --get the list of default properties
	if defaults then
		for key,value in pairs(itemProperties) do --cycle through the item's properties, with the key variable being assigned to the property's name and the value variable being assigned to the property's actual value
			if v~=defaults[key] then --is the property different from the default?
				propertiesToSave[key]=value --if so, then we need to be sure to save it.
			end
		end
	else
		propertiesToSave=itemProperties --if there are no defaults to compare to, just return the entire thing
	end
	return propertiesToSave
end
--function that accepts a reduced list and returns it with any missing default properties
function loadProperties(itemProperties)
	local completeProperties={}
	local defaults=DEFAULT_PROPERTIES[itemProperties.Name] --get the list of default properties
	if defaults then
		for key,value in pairs(defaults) do --cycle through the list of defaults this time.
			completeProperties[key]=itemProperties[key] or value --include the saved property in the list, or if it doesn't exist, include the default value instead.
		end
	else
		completeProperties=itemProperties --if there are no defaults to include, just return the entire thing
	end
	return completeProperties
end

As you can see it’s a bit involved, but it boils down to a simple principle: when saving, only save the changed properties, and when loading, add the default properties back in to properly display the item. Hope this helped!

3 Likes