Skip to content

Mod Config: Add API method to store a config value #489

@KANAjetzt

Description

@KANAjetzt

Currently, the only way to update the config file is via ModLoaderConfig.update_config(). Adding a method to update a specific value within the config would be a nice quality-of-life improvement.

## Updates an existing [ModConfig] object with new data and saves the config file.[br]
## [br]
## [b]Parameters:[/b][br]
## - [code]config[/code] ([ModConfig]): The [ModConfig] object to be updated.[br]
## [br]
## [b]Returns:[/b][br]
## - [ModConfig]: The updated [ModConfig] object if successful, or null otherwise.
static func update_config(config: ModConfig) -> ModConfig:
# Validate the config and check for any validation errors
var error_message := config.validate()
# Check if the config is the "default" config, which cannot be modified
if config.name == DEFAULT_CONFIG_NAME:
ModLoaderLog.error("The \"default\" config cannot be modified. Please create a new config instead.", LOG_NAME)
return null
# Check if the config passed validation
if not config.is_valid:
ModLoaderLog.error("Update for config \"%s\" failed validation with error message \"%s\"" % [config.name, error_message], LOG_NAME)
return null
# Save the updated config to the config file
var is_save_success := config.save_to_file()
if not is_save_success:
ModLoaderLog.error("Failed to save config \"%s\" to \"%s\"." % [config.name, config.save_path], LOG_NAME)
return null
# Return the updated config
return config

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions