0 votes

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

in Engine by (18 points)

2 Answers

0 votes

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.

by (18 points)

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

0 votes

Not sure I understand the problem completely, but you can also extend the script, not just the scene.

So don't define the signal in the base script, but in the extended ones for each type extended from that base type.

by (18 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 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.