0 votes

I hope I can articulate, what I want to achieve. I tried to find an answer by myself, but I just coulnd't find anything, maybe I googled the wrong questions? I already asked on Discord, but there was a lot going on and I think this is a better place. It's my first game, everything is coupled which is bad, I just wanted to get my hands dirty. But I'm struggling with a basic concept: signals

So I'm making an Earthbound-like RPG. You wander around in an Overworld and if you come close to an enemy, you enter a turn-based battle with said enemy. Let's say, my Level has mutliple Rats (the enemy), which I placed there, so they are not instanced at runtime. Every Rat has a scene as a child: BattleStart. That scene is an Area2D and on entering, a little animation plays and when the animation is finished, the BattleStart scene emits a custom signal.

My problem: In my Main-script, I need to connect the signal, but in order to connect to the BattleStart scene of every Rat, i need to know their path. But what if there are multiple rats, then also skeletons and so on? Do I need to connect to every single one of them, since they all have a different name?

$World/Environment/Rat/BattleStart.connect("enter_battle", self, "_on_transition_animation_ended") $World/Environment/Rat2/BattleStart.connect("enter_battle", self, "_on_transition_animation_ended") $World/Environment/Skeleton/BattleStart.connect("enter_battle", self, "_on_transition_animation_ended") $World/Environment/Skeleton2/BattleStart.connect("enter_battle", self, "_on_transition_animation_ended")

Do I need to do that for tens or hundreds of enemies? I always want to connect to that one BattleStart scene, which is part of every enemy. Or shouldn't I do that? If my approach is completely wrong, please tell me how to do it. I think this is actually a very common case, isn't it? Because you often have multiple instances of scenes and all of them should send a signal to one parent somewhere up above. What do I miss? Please explain to me elaborately, I'm a beginner.
Thanks a lot in advance

Godot version 3.3
in Engine by (12 points)

1 Answer

+1 vote

Instead of creating many battlestart scene which is very tiring, autoload a one battlestart so you can use it anytime anywhere.
And then on the playing problem, create a method on the battlestart scene that you autoloaded and rename it to your custom signal.

And then on the ready function of your rat, connect your signal to autoload battle start

by (378 points)

Thanks for your answer. The problem with autoload is, that this BattleStart scene should be a child of every enemy, since it has the CollisionShape and I need to adjust the CollisionShape for every enemy. The BattleStart scene is automatically part of every enemy anyways.
Thanks to you I came to some kind of a solution. BattleStart scene calls a function in my autoloaded Global scene which just calls a function in its sibling Main scene. That's kind of a solution, but highly coupled...

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.
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 webmaster@godotengine.org with your username.