Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | standinonstilts |
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 = get_node(“.”) //this gets the kinematicbody2d which is the root of all my mob scenes
b.connect(“input_event”,self,“on_click”,[viewport,event,shape_tx])//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?
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
standinonstilts | 2019-04-09 01:09
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)
Eric Ellingson | 2019-04-09 06:15
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.
standinonstilts | 2019-04-09 15:15
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.
Eric Ellingson | 2019-04-09 17:46