How to create a copy of a script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By whooshfrosted
:warning: Old Version Published before Godot 3 was released.

Say I have a script that is created and made ready for use like so:

var script = preload("res://the_script.gd").new()

How would I duplicate this script?

I tried this but it doesn’t work:

var new_script = script.new()

Try .instance() instead of .new()

timoschwarzer | 2017-07-20 12:33

No good.

Invalid call. Nonexistent function ‘instance’ in base 'Reference

whooshfrosted | 2017-07-20 22:30

Yes, right. Since GDScripts are resources, it is .duplicate() here.

timoschwarzer | 2017-07-21 12:59

Same result, since once I call new on it, becomes a reference.
“Invalid call. Nonexistent function ‘duplicate’ in base 'Reference”

Duplicate would work in the code I posted below though.

whooshfrosted | 2017-07-21 15:24

:bust_in_silhouette: Reply From: whooshfrosted

Since there doesn’t seem to be a way of doing it, I’m attaching the resource to the reference and using it to create copies later.

On creation:

var fireball_res = preload(“fireball.gd”)
var fireball_ref = fireball_res.new()
fireball_ref.resource = fireball_res

To make copies later:

var new_fireball = fireball_ref.resource.new()