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?

asked Jun 25 in Engine by Gamersen (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.

answered Jun 25 by Ertain (1,253 points)
selected Jul 4 by Gamersen
0 votes

Short answer:

Yes, you can definitly do this


Long answer:

Depends.
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


EDIT:
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!

answered Jun 25 by whiteshampoo (976 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.