0 votes

I've got two nodes which are very similar. They are probably going to use the same if not very similar code. Can I attach the same script to both of them? Would it be better to create two scripts for each of them?

in Engine by (24 points)

2 Answers

+2 votes
Best answer

Why not have scripts on the two nodes inherits from the original script? The two nodes can share the same code, yet still have code which differentiates them. Look to the documentation for more information.

by (2,462 points)
selected by
0 votes

Short answer:

Yes, you can definitly do this

Long answer:

If you have 2 Nodes, that should do exactly the same, but look different, then it's ok.
If they should behave different, then you should probably use inheritance.
Here is a nice tutorial: KidsCanCode-Tutorial

To make it more clear:
If you have to write 2 times the very same code, then you are doing something wrong.
If you want to change the behavior of something and have to replace it on 2 places in your code, then you should have made a function for that, or have inherited another script/node/whatever.
I hope you understand what i mean. If not, feel free to ask!

by (1,480 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 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 webmaster@godotengine.org with your username.