0 votes

If I make a signal on a node of a scene, and instance this scene in another scene via code. Will the signal be working on all instances or only the first one ?

Godot version 3.2.3
in Engine by (86 points)

1 Answer

0 votes
Best answer

Yes, signals are attached to all instances of instanced scenes. But signal connections are instance-based. So if you have scene (enemy) with signal, one instance (enemy1) that is connected via code to another scene (player) and second instance of enemy scene (enemy2) without connection to player. Then, when enemy2 will emit signal, player will not receive it, because it does not listen for signals from enemy2.

by (1,188 points)
selected by

Then use first code example

  func coinspawner():
  var rcoll = (collectibles[randi()%collectibles.size()])
  var coin = rcoll.instance()
  if rcoll == collectibles[0]:
      coin.connect("coin_collected", self, "_on_coin_collected")
  else:
      print("carrot spawned!")
      coin.connect("coin2_collected", self, "_on_coin2_collected")

It says Invalid call. Nonexistent function 'instance' in base 'Node2D (collections.gd)'.
for var coin = rcoll.instance().
I think because rcoll itself is an instance. Then What, Sir ?

How can I simply check if an instance is of a specific scene ?

Okay so thanks a lot Sir, I finally solved the problem with this little piece of code:

    var rcoll = (collectibles[randi()%collectibles.size()]).instance()
    #if rcoll == (collectibles[0]).instance():            #I tried these 
    #if rcoll is coins:                                  #but none worked

    if rcoll.get_filename() == coins.get_path():
        rcoll.connect("coin_collected", self, "_on_coin_collected")
    else:
        rcoll.connect("carrot_collected", self, "_on_carrot_collected")

I hope this approach has no problems. plz reply.
and marked your answer as best 'cuz it solved the initial question I had asked.
Thanks again @AlexTheRegent

If your collectables is array of load("*.tscn") scenes, it can't be Node2D. When load loads scene, it will be PackedScene that have instance method.
As for coin spawning function, I am still suggesting usage of int variable as switcher because it is faster than checking types of scenes (your method is working, but it is not optimal).

var coin_type = randi()%collectibles.size()
var coin = collectibles[coin_type].instance()
if coin_type == 0:
    coin.connect("coin_collected", self, "_on_coin_collected")
else:
    coin.connect("carrot_collected", self, "_on_carrot_collected")

This code is simply cleaner and faster.

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.