0 votes

Hi,
I am new to Godot and I'm trying to understand how I can implement inheritance. I am writing my first game and in the game there will be 20-25 RigidBody2D objects which will have exactly the same behaviours: when clicked (inputevent signal), they play a sound and then disappear (queuefree()). Only difference between them is that they have different sprites (different shapes, i.e. rectangle, circle, square etc.) and different CollisionPolygon2D's. Can you please kindly suggest a way to achieve this? I started with a RigidBody2D, made it a class and inherited from that. But that way, signalling the input_event in the script gets messed up. The scenes inherited from that class gets created with the signal of the original class. Maybe this is a stupid question but as I said I am a newbie just trying to learn the engine.
Thank you in advance

asked Apr 13 in Engine by korayozbay (18 points)

1 Answer

0 votes

Hello
I'm also beginner but may be my advice could help. I'm not quite understand your design flaw but if you have a mess with binding signals and functions you probably should try doing it not in editor. Try to use constructor (func _ready) of a base class and bind it in runtime. https://docs.godotengine.org/en/3.2/getting_started/step_by_step/signals.html#connecting-signals-in-code

In this case you have concrete self and function and signal name. Also you could just hook up one script to several nodes. May be also you can rid of inheritance.

answered May 7 by ntfshard (18 points)

Hi,
Actually my intention was not to connect signals in code, but that is also very helpful.
Thank you.

I'm working on such design now (may be you could take some ideas from it):

I have multiple Area2D(to have inputevent) as a parent/root of item and couple child-plugins, like context menu or leftbuttonaction. Each Area2D has specific for such type of item code in 'agreed'-named functions like doinspect, dotake, do...
Each child-plugin can be added as a child(wow) to any Area2D. In child-plugin's ready function he binds to parent's inputevent handler.
For example, context menu has code like: if it's rightmousebutton pressed, then show menu. Menu has several buttons. Each button has code(inside plugin):
if parent.hasmethod("dotake"): parent.do_take(some args from event) else assert(false)
Also it's possible to hide buttons, if you wish.

May be it's reinventing something already made inside engine but I don't find anything. This mechanism is more like poor man's virtual functions/polymorphism

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.