About mod loading with ProjectSettings.load_resource_pack() at runtime

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Harrygiel

I’m trying to implement a mod manager in the game and I’m in a bit of trouble…
I am loading a zip folder with a ProjectSettings.load_resource_pack() command. once the mod is loaded, I am trying to replace the previous file with the new one. For example:

var scene = preload("res://ship/myship.tscn")
var modifiedScene = scene.instance()
var new node =  # a new created node

ModLoader.appendNodeInScene(modifiedScene, new node)
ModLoader.saveScene(modifiedScene, "res://ship/myship.tscn")

This code is working well, and I don’t have any problem with it. But in another case, I’m trying to only replace a script. and in this case the same method doesn’t seem to work:

func takeOverScript(newScriptPath:String, OldScriptPath:String):
    var savedScript = load(newScriptPath)

Once I’m calling this function, the OldScriptPath does have the code of the new script. Yay ! However, when I’m using a scene ( let call it myShypyard.tscn) with an external reference to my OldScriptPath:

[ext_resource path="res:///old/Script/Path.gd" type="Script" id=1]

The OLD script is called (I checked with some print, and the scene return the print of the old script and not the new one).

I even tried to use the same method as previously by loading an already modified copy of myShypyard.tscn, pointing to a new path, where I store the new script at first (res:///overwride/old/Script/Path.gd), but it’s STILL the initial copy of the scene who is loaded, and not the new one.
I think godot is preloading my scene, and I can’t find a way to reload it in the cache, or with my new scene, or with my new script…
Does somebody have an idea of how to make the script replace the old one at runtime ?
Or does anybody have a way to add a patch package BEFORE the game compilation ?

your title is misleading. there is not a single line containing ProjectSettings.load_resource_pack()

ULViDO | 2023-03-07 10:57

:bust_in_silhouette: Reply From: Harrygiel

I found the solution with some help. Obvious, yet not intuitive:
The script was used in a sub-nod of a preloaded screen. So once I overwrited the file, the game ALREADY preloaded the scene that was gonna use it during compilation time.