0 votes

I have many instances of an Object in my game who all point to the same .gd - script.

i think for myself i dont need to create a specific class to organise this because every instance uses the same script. in this script is determined if the instance is a boy or a girl, and their movement.

My question is: Do i have anny disadvantages by not using a self created spezific class?

in Engine by (68 points)

1 Answer

0 votes

As long as the complexity of the code doesn't affect your development it's OK. Do not refactor your code unless you need to and you understand why you're doing it. But there are few guidelines which you could follow. If the objects differ only by parameters and you have no duplicated functions (e.g. move_boy or move_girl) you don't need multiple classes. If it does it would be more readable to have a script with common methods and parameters and to inherit it in another class which has special functions just for your class. E.g. script "human.gd" which is inherited by "boy.gd" and "girl.gd".

Having multiple scripts for each object type with same functions is also considered a bad practice because you'll have a lot of duplicated code. So what you tend to achieve with extending/inheriting classes is to have zero duplicated code and have classes define a single model/abstraction. So having a classes like BoyGirl.gd or CarMotorbike.gd is not advisable, it should be for example vehicle.gd, which is extended by either car or motorbike.

You can check extending scripts in Godot to see how it's done.

by (709 points)
edited by
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.