|
|
|
|
Reply From: |
gon123 |
I’m starting with Godot 4 and I have been programming in OOP for years, so some times I feel like I kinda need interfaces, inheritance, etc.
Right now I’m implementing a very generic inventory (we could call it container or slot container, for inventories, boxes, quickbars, hotbars, etc) and I really want to have the responsabilities well distributed.
May be I should use C# or C++ but I started learning godot with GDSCript and I’m on fire, so my approach right now is use composition and inheritance, as there is no interfaces or abastract classes, what I’m doing is implementing abstract methods with a “pass” body and may be a print message.
TL;DR: Use inheritance and composition, implement abstract methods con parent with an empty body, for example.
Parent class:
class_name Animal
walk():
print("Method not Implemented) # You could throw an error
pass
Child class:
class_name Dog
extends Animal
walk():
print("Walk speed of 8 km/h")
This thread is very old, but I’m going to throw my opinion into here anyway.
I am also not a fan of duck typing. There is absolutely nothing in GDScript that prevents using good OOP design principles such as interfaces. GDScript won’t help you, as you will be responsible for ensuring your interfaces act as interfaces, but it also won’t stop you. I usually end up less with interfaces and more with inheritence, but it works fine.
I frequently have usage trees such as: Weapon → Projectile Weapon → Handgun. Weapon has all the code common to all weapons, Projectile Weapon has all the code common to projectile weapons, and Handgun has all the code specific to handguns.
Sometimes I end up with a Weapon base class that leaves decision making to the subclass, so the Weapon implementation method consists of the method name and a “pass” body. The subclass implementation then overrides the Weapon base class.
stormreaver | 2023-03-29 01:56