Attribute Enum Support

Trying to migrate over some configurations over to Attributes in my code base. Previously, I used StringValues for Enum storing, but I think it would be much cleaner on the user-end if I can directly read/write Enums.
(Also maybe an EnumValue class type too?)

29 Likes

Isn’t this just EnumItem? EnumItem is already a datatype so hypothetically it could be supported as a datatype for attributes, since the other attribute types are also datatypes.

2 Likes

EnumItem doesn’t exist within the DOM. An EnumValue (If it were added) could exist within the DOM, like other ValueBase objects.

But yes, since EnumItem is already a Roblox datatype, that’s why I would love for it to be able to be assigned with Attributes

7 Likes

I’m sorry for necro posting… But I think a better suggestion now is can we have EnumItem added to the dropdown list when adding an attribute directly in the properties window? (I can imagine with so many enum types, this may have conflicts.)

Rather than the only method being via scripting, I can imagine a string box when added with no value, and when a valid Enum type is recognized (perhaps an updated, shortening list of enum types to select), use that type and switch to a dropdown (as shown below). Double click to switch back to string box. Just an idea :man_shrugging: (Same with this, I can imagine there could be conflicts. With what is beyond me.)

I was able to store KeyCode enums as an attribute using TargetPath:SetAttribute("Name", Enum.KeyCode.LeftShift), for example. And afterwards, I could edit it in the properties menu to anything from the KeyCode list. So if anyone needs to store them, you can do it with attributes no problem, just has to be through scripting.

And it is listed in the documentation:
AttrubuteList

4 Likes

Better yet, imagine we could extend instances, essentially make our own instances that inherit from existing instances, thus giving us infinite powa

But that’s another feature request entirely… wish i could make one

7 Likes

Holy crap. This is a genius plan!

It would make dropdown menus even more viable if Roblox were to let us make our own Enums too

1 Like

Context: Enum support via scripting was added about 6 months ago, but yes, there’s no affordance in the GUI to add them yet. The difficulty is that there’s more than 100 different enum types, so making a convenient dialog to pick enum type is non-trivial. Totally doable but it hasn’t been done yet.

This is a lot harder to support. Imagine if you had a dropdown list of 20 items. Ideally you don’t want to bring copies of those 20 items along with every single Instance that has the attribute.

2 Likes

Regardless, Dropdown attributes would nice to support as it can ease development. I would hope that can be considered someday

Thanks for getting back to us! I understand the difficulty, it’s definitely something I thought could be an issue–having so many types.

However, with all due respect, I don’t see it as a massive task, especially when using a string attribute type to detect if it’s an Enum:

I just ran a small test (though in an empty baseplate, so not much going on) and detected attribute changes game-wide for new descendants (with two instances having attribute changes. Again, not a lot going on at all), with no performance issues. I’m not knowledgeable enough to know whether this would impact performance in Studio or not when handling a lot more instances having attribute changes in a relatively short period. But something like this doesn’t seem very hard to implement?

Whacky code
function waitForEnum(self)
	self.AttributeChanged:Connect(function(attributeName)
		if typeof(self:GetAttribute(attributeName)) == "string" and string.find(self:GetAttribute(attributeName),"Enum.") then -- Make sure attribute type is a string & the inputted text is predicted to be an Enum
			local newEnumTable = string.split(self:GetAttribute(attributeName),".") -- eg. "Enum, Keycode, ButtonA"
			if not newEnumTable[2] then return error("Invalid EnumType") end -- No enum type inputted
			if (newEnumTable[2] == "" or newEnumTable[2] == " ") then return end -- Kill function if "Enum." is the only string inputted
			if not Enum[newEnumTable[2]] then return error("Invalid EnumType") end -- Return error if no enum type exists for the given input (roblox will handle the error before reaching mine)
			if not newEnumTable[3] or not Enum[newEnumTable[2]][newEnumTable[3]] then return error("Invalid EnumType value") end -- Return (my) error if an invalid value is inputted for Enum's third position in the above table 
			if Enum[newEnumTable[2]] and Enum[newEnumTable[2]][newEnumTable[3]] then -- Confirm enum exists
				self:SetAttribute(attributeName, Enum[newEnumTable[2]][newEnumTable[3]]) -- Set enum
			end
		end
	end)
end

game.DescendantAdded:Connect(waitForEnum) -- Do for all new descendants

*(I used the above code in both a local and server script. I assume this would work on plugin level to be implemented into Studio.

Edit 2: It does, plugin created and can be found here

Using the above code eliminates the whole original idea of a dropdown of all enum types, and the difficulty with it, no?

Edit: I know there would be potential conflicts with using a string type for the attribute enum detection. I myself have used string attributes to store and retrieve enums by name. But I can imagine an Enum attribute type work similarly to the video. Detection from an initial string input is the idea.

3 Likes