Create modding in game (load external scripts)

Godot Version

Godot 4.2.1 linux x86_64

Question

How can I load a GDScript file from an external folder and use the logic from it?

Right now I am loading mod.json and creating a button for it.

just resource load like usual then stringify it with JSON class JSON — Godot Engine (stable) documentation in English

for reading outside .pck
https://www.reddit.com/r/godot/comments/zwdqsp/how_to_add_an_external_resource_file_to_a_pck/

Apparently I didn’t ask the question correctly. There are no problems with loading JSON file.

My problem is that I want to load a script initially not existing in the project and lying next to the game in the folder mods.

But I don’t know how to do it.

This how i try do it:

func _load_mod_script(mod_scipt_name, full_path):
	print("Load script: " + mod_scipt_name)
	
	var script = load(full_path)
	
	print(script.do_logic())

script that i try load:

extends Resource
class_name RandomItem

func do_logic() -> String:
   return "Hello world!"

Good. Now I use Packer to load mods, and the console gives me something like this (just debugging info):

[1/1 - 100%] PCKPacker flush: /home/zaksen/Godot/mods/test_mod/random_item.gd -> res://mods/mod0/random_item.gd
Mod [Random very good mod that add many features!] was loaded! 
Mod [Base mod] was loaded!

How can I verify that these files have been downloaded? For example, use functions from them or something like that?

ok, i never knew pckpacker existed
what do you mean by downloaded?

load_resource_pack() should return true if it’s a success

That is, they are connected to the project and I can use them without actually knowing about them.

yes, it returning true

Now i see the problem. I can’t remove mod because it’s added like a pkg file

Maybe someone knows how I can do this in a different way?

there is this in the documentation:

it exactly tells to use the load_resource_pack and then load the scene for mods

then just dont load the mod if you dont want the mod to be active?

1 Like

About just not loading is a good idea, but if a player loads 1000 mods and after that remove them all, the size of the final game could be very large.

Now I’m trying to rewrite the mod loading system and I get an error invalid parameter

Upload path: res://mods/mod0/random_item.gd
Load path: /home/zaksen/Godot/mods/test_mod/random_item.gd
Unable to load script: Invalid parameter

there is code:

func _load_mod_script(_full_path, _file_name, packer, mod_engine_path):
	print("Upload path: " + mod_engine_path + "/" + _file_name)
	print("Load path: " + _full_path)
	var error = packer.add_file(mod_engine_path + "/" + _file_name, _full_path)
	if error:
		print("Unable to load script: %s" % error_string(error))

I fix this

1 Like

Okay, I’ll just try to do that.