0 votes

I have script called Mob and a script called goblin.

My mob is defined as class_name Mob and has a few functions that goblin will inherit.
all the mobs I make will have a kinematicbody2d, sprite, and collidershape2d.

when a mob is clicked I want to send a signal: ideally using the input_event(viewport, event, shape) signal defined in kinematicbody2d

my Mob class is being treated as an abstract class essentially. Instead of rewritting and configuring the signal in each mob, I want to write it once in my Mob class, and have all mobs run Mob.init() when they load in to register and connect the signal.

the problem is: my mob is not in a scene, and therefore I cannot simply click on the signal and register it in the class

instead I have tried to code it:

var b = getnode(".") //this gets the kinematicbody2d which is the root of all my mob scenes
event",self,"onclick",[viewport,event,shapetx])//this should ideally connect the event to a function in my mob class

unfortunately, I get an error: Identifier not found: viewport
so what i can tell is each of my arguments for the signal are not defined, and I have to define them before i connect the signal, but I don't know how I'm supposed to get these values to make this work.

Is there a reliable way to do this, or am I approaching the problem wrong?

in Engine by (12 points)

I have since removed [viewport,event,shaptx] from the b.connect(). It compiles but the signal does not emit when the sprite is clicked, so I don't think the signal connected properly

Where are you calling Mob.init()? In the _init() method of the child classes? Have you tried doing it in _ready()?

Can you just put the connect() statement in the _ready() method of Mob? I haven't tested it at all, and I don't know if it still works that way in 3.1, but I think methods like _ready() get called for each level of inheritance automatically (which in my opinion is weird, but whatever)

So i don't have a ready method in the Mob class. Instead my goblin class that inherits from it has one. I wasn't sure if having two ready methods: one in child and one in the parent; would allow both ready methods to run in the correct order. So instead the Mob class has init(), and init() is called in the goblins _ready() method.

I'm not sure what the order is, or if it can be guaranteed to be in a specific order, but can you just try putting the connect() call in Mob._ready() and see if that works? Both parent and child classes will call their _ready() methods.

Please log in or register to answer this question.

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.