I love how easy it is to just take an object and hit cntrl D to duplicate it. But, I started to run in to problems when i have scripts that need to be duplicated but need certain aspects changed. Maybe there is a better practice. Is it wasteful to have the same data with just certain aspects changed. Especially if the script if large? I try to make my code as modular as possible but do you ever need to make unique versions of scripts?

  1. I would have to delete the duplicated script.
  2. Select the script I need and use "save as" to rename it to make it unique
  3. Then reload the new script.

I try to make my code as modular as possible but do you ever need to make unique versions of scripts?

I tried looking into extend script but that just add-ons to your script???

Hello Dismantled. Your question is an age old programming problem that I think everyone struggles with from time to time. Depending on what you're trying to accomplish there are likely quite a few things you can do.

First, my number one programming rule is, if it does what you want and works for your project, then it isn't intrinsically wrong. If making lots of unique scripts that contain a lot of the same code works for you and your project then there's nothing wrong with that. There will always be ways to make you project better, but usually its a trade off between finding the best solution and finding solutions that work well enough.

That being said here are a few ideas that might help to reduce redundant code.

  1. You said your script is large. Maybe consider splitting its responsibility logically across several nodes, especially the parts that you find are unique across instances. That way, you'd have one base node with the code that doesn't change and lots of unique nodes that support it that contain unique behavior. Then implementing unique behavior is as easy as dragging and dropping nodes into the scene tree.

  2. Export an enum list to the inspector so that you can pick which functionality you'd like to use from a drop down list. Then feed the selected enum into a match statement such that each case handles the unique code.

  3. Last, consider "dependency injection". In this case you have three types of scripts. 1. The script that you want to give unique behavior 2. A script that contains the unique behavior in the form of functions. 3. A script that decides which behavior each node requires at runtime and injects the behavior in the form of some kind of reference to a function.

The list kind of goes from easy to more advanced. If #3 makes no sense, don't worry about it, but its a good concept to be aware of. If you can be a bit more specific about what is unique about each node, I might be able to give a more specific suggestion, but I hope there's something useful for you in the above. Good luck!

Thanks so much Dave the Dev! I really appreciate your advice and insight. I i will look into those 3 concepts! I'm working toward building VR titles and finding that interaction systems can get pretty involved. I am very excited that Godot is working out great!!! I've mainly been trying to mimic a lot of the things they came up with in the game "job simulator" and having great success! I will try to share some of the code I am working on when I get a chance. I just have find time to fully explain what i am doing correctly.

Is it possible to duplicate a script and make it unique? The method I used to make my script unique breaks all my signals and i have to rewire them?

3 years later