0 votes

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()
in Engine by (249 points)

Try .instance() instead of .new()

No good.

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

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

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.

1 Answer

0 votes

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 fireballres = preload("fireball.gd")
var fireball
ref = fireballres.new()
fireball
ref.resource = fireball_res

To make copies later:

var newfireball = fireballref.resource.new()

by (249 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.