-
-
Notifications
You must be signed in to change notification settings - Fork 103
Description
Describe the project you are working on
Writing the foundation for my game, currently customizable settings specific to what type of game is being ran (singleplayer, multiplayer, split screen) and based on what platform it's being ran on (itch, steam, other future ports)
Describe the problem or limitation you are having in your project
Needed a way to dynamically show these settings to users based on the type of game & platform. Didn't want to make a ton of scenes and can't run some code on other platforms. So I came up with a system that uses a resource instance for a specific setting. Each specific setting has it's own value type though. Without having to hard code in implementations of my resource for all types (i.e. "IntResource", "StringResource" etc), this was difficult. I ended up using an Array (which allows for dynamic & varying types) and limiting it's size to one, then validating what was set in the editor (see code below) to prevent values of incorrect types.
Note on my code: I know I can just assume the type from the _default_value, but I want to be redundant to prevent issues so I created a separate field for the type.
## The type of value
@export var type: Variant.Type:
set(_type):
if _type == null || typeof(_default_value[0]) != _type:
_default_value[0] = null
type = _type
## The default value of the setting. Must be the same type as specified in [member GameSetting.type]
@export var _default_value: Array = [null]:
set(value):
if value == null || value.size() == 0: # Make sure the array exists & has 1 element
_default_value = [null]
push_warning("Warning: GameSettings.value must be an Array with size of 1")
return
if value.size() > 1: # Make sure the array doesn't have more than 1 element
_default_value = [value[0]]
push_warning("Warning: GameSettings.value must be an Array with size of 1")
return
if !is_value_valid_type(value[0]): # Make sure the type matches the expected type
push_warning("Warning: GameSettings.value type must match GameSettings.type")
return
_default_value = value
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Arrays & Dictionaries already let you export a dynamic type. My solution works, but is incredibly clunky in the editor. The ability to simply @export var my_var: Variant and then allow any type to be added in the editor would be great. Or anything similar that allows for exports of inexplicit types.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Simply
@export var my_var: Variant
Visually in the editor, I assume it'd look best as it currently does with exporting Arrays with inexplicit types, just without the "Size" and "Add Element" elements. The edit button on the right side would allow for changing the type. The value field would change based on what type is selected.
Such as: (excuse my sloppy photo editing)

If this enhancement will not be used often, can it be worked around with a few lines of script?
I think it could be used pretty often to create reusable systems like my own. The current workaround is a bit more than a few lines of code, but the main problem is that it's just very clunky in the editor, taking away from the beauty of Godot's simplistic properties inspector.
Is there a reason why this should be core and not an add-on in the asset library?
Arrays & Dictionaries support it already, so why not single variables?