Dynamic load script into a scene

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

Hi all,

I am wondering if it is possible to load a script on an object and then (re?)load another script.

The scenario I am thinking is the following:

You have an area2d which reacts to some mouse input. Depending on variables (either global or local) you can get different “actions”.

This is basically a description of making some adventure game verbs and interact with the said area2d. For example in a menu you could have a button for look and a button for push which will setup some variables and upon clicking the area2d object would load the look script or the push script.

I don’t have any proof at the moment the last time I tried, I couldn’t put the area2d to behave with the second script loaded (I’d assume it has something to do with .reload() method).

So anyone has something similar to share or any alternatives?

:bust_in_silhouette: Reply From: sunDalik

Changing a script from code should be as simple as


However when you change a script the old reference becomes invalid and you can’t access it anymore. To get a new reference with the changed script you should do this.

ulong objId = node.GetInstanceId();
Node newNode = (Node)GD.InstanceFromId(objId);

(Code is in C#)

That’s very interesting, let me try to do this in gdscript and I’ll come back for the tick!

Panagiotis Halatsako | 2022-05-12 18:20