Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global class registration #95

Merged
merged 3 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions addons/mod_loader/mod_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,15 @@ func install_script_extension(child_script_path: String) -> void:
child_script.take_over_path(parent_script_path)


# Register an array of classes to the global scope, since Godot only does that in the editor.
# Format: { "base": "ParentClass", "class": "ClassName", "language": "GDScript", "path": "res://path/class_name.gd" }
# You can find these easily in the project.godot file under "_global_script_classes"
# (but you should only include classes belonging to your mod)
func register_global_classes_from_array(new_global_classes: Array) -> void:
ModLoaderUtils.register_global_classes_from_array(new_global_classes)
var _savecustom_error: int = ProjectSettings.save_custom(ModLoaderUtils.get_override_path())


# Add a translation file, eg "mytranslation.en.translation". The translation
# file should have been created in Godot already: When you improt a CSV, such
# a file will be created for you.
Expand Down
13 changes: 7 additions & 6 deletions addons/mod_loader/mod_loader_utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,26 @@ static func dir_exists(path: String) -> bool:


# Register an array of classes to the global scope, since Godot only does that in the editor.
# Format: { "base": "ParentClass", "class": "ClassName", "language": "GDScript", "path": "res://path/class_name.gd" }
# You can find these easily in the project.godot file under "_global_script_classes"
# (but you should only include classes belonging to your mod)
static func register_global_classes_from_array(new_global_classes: Array) -> void:
var registered_classes: Array = ProjectSettings.get_setting("_global_script_classes")
var registered_class_icons: Dictionary = ProjectSettings.get_setting("_global_script_class_icons")

for new_class in new_global_classes:
if not is_valid_global_class_dict(new_class):
continue
if registered_classes.has(new_class):
continue
for old_class in registered_classes:
if old_class.class == new_class.class:
if OS.has_feature("editor"):
log_info('Class "%s" to be registered as global was already registered by the editor. Skipping.' % new_class.class, LOG_NAME)
else:
log_info('Class "%s" to be registered as global already exists. Skipping.' % new_class.class, LOG_NAME)
continue

registered_classes.append(new_class)
registered_class_icons[new_class.class] = "" # empty icon, does not matter

ProjectSettings.set_setting("_global_script_classes", registered_classes)
ProjectSettings.set_setting("_global_script_class_icons", registered_class_icons)
var _savecustom_error: int = ProjectSettings.save_custom(get_override_path())


# Checks if all required fields are in the given [Dictionary]
Expand Down