0 votes

I am making a bullet hell game, I want to be able to programmatically add and subtract bullet spawners. These bullet spawners listen to the parent object to know when to do their on_shoot methods. I am coming from Unity so naturally I tried an observer pattern script first, didn't work out. Using signals, I can't figure out how to assign new listeners from the script that emits the signal.

class_name Ship
extends Area2D

# Declare member variables here.
export var gunCount = 0 #number of weapons
export var health = 1 #health points
export var speed = 150.0 #movement speed
export (PackedScene) var Gun #gun prefab
export var guns = [] #list of guns

signal shoot
func bomb():
    pass#bombs
func addGun(PackedScene, GunCount): #adds guns based on count
    for i in range(0,GunCount):
        guns.append(PackedScene)
        print("%d connected" % i)
    for j in range(0,GunCount):
        self.connect("shoot", guns[j], "_On_Shoot")
# Called when the node enters the scene tree for the first time.
func _ready():
    pass

This is my last attempt at trying to achieve this, but it results in an error that reads connect: Parameter "p_to_object" is null.

Godot version v3.3.3stable
in Engine by (22 points)

1 Answer

0 votes

Why do it in reverse?
Connect signal from Gun to Parent node in Gun's _ready function.

by (889 points)
  1. doing it this way just gives me an error that I "attempt to connect to nonexistant signal." Also worth noting, I've instantiated 5 gun scenes, but this error only occurs once.

  2. I'm instantiating the gun in the parent node, so I was making it the same way I'd do an observer script in c#, observer assigns listener, not the other way around. Makes it easier when instantiating tons of listeners.

I can't see you instance any gun. guns[j] is null then self.connect("shoot", guns[j], "_On_Shoot") is called. To instance Gun do something like this (depend on your Scene hierarchy plan) add_child(Gun.instance()) then let gun add itself to list of guns in it's _ready

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.