How can i instance a scene with GDscript after a signal?

OrgamekOrgamek Posts: 6Member
edited June 2019 in Programming


i'm trying to generate a bullet as a variable of a scene, the model is a variable of a weapon, which it self is a variable of my character.

I tried to rubber ducking my code but i didn't manage to address the issue.

extends "res://Personnages/"

func init():
    poid = 75
    max_speed =800 
    max_health =100
    accelBotes =600
    var sceneArme = preload("res://Armes/poing.tscn")
    var Arme = sceneArme.instance()

func control(delta):
    if (Input.is_action_just_pressed('Sauter')):
        accel += Vector2(0,-max_speed*150).rotated(rot)
    if Input.is_action_pressed('MGauche'):
        velocity = Vector2(-max_speed, 0).rotated(rot)
    if Input.is_action_pressed('MDroite'):
        velocity = Vector2(max_speed, 0).rotated(rot)
    if Input.is_action_just_pressed('Inventaire'):
        if $Inventaire.is_visible():
    if (Input.is_action_just_pressed('UtiliserArme') and !$Inventaire.is_visible()):
        $Arme.tire($SuppViseur/viseur.global_position, $SuppViseur/viseur.global_rotation )`

So this code allow me to call the shoot function when i click. The said function is in the weapon.
 `func tire(positionGlobale, rot):
    if can_shoot:
        can_shoot = false
        var dir = Vector2(1, 0).rotated(rot)
        if num > 1:
            for i in range(num):
                var a = -dispersion + i * (2*dispersion)/(num-1)
                emit_signal('shoot', balle, positionGlobale, dir.rotated(a))
            emit_signal('shoot', balle, positionGlobale, dir)

When the function is called, it emit an signal. I linked the signal 'shoot' to the following function in the scene, which call the generating function of the bullet.

The function is the following.

func start(_position, _direction):
    position = _position
    rotation = _direction.angle()
    velocity = _direction * speed

I'm a bit stuck. Sorry if i was not clear, ask questions if so.
Any idea of the reason it doesn't generate anyhting?

Tags :

Best Answer

  • OrgamekOrgamek Posts: 6
    Accepted Answer

    I finally applied the fix i described earlier: basically gun don't kill peoples. Peoples kill peoples. xD
    I will close the tread.


  • twondaitwondai Posts: 2Member

    From what i can gather, you're passing 3 parameters along with your shoot signal, those being the bullet object "balle", the position "positionGlobale" and rotation "dir". While the position and rotation are fairly straightforward, I can't see where you've declared the "balle" variable. Can you show us the bit of code where you declare or initialize the "balle" variable?
    Additionally, does the debugger raise any error messages with this script? If so, it would be helpful for us to see them.

  • TwistedTwiglegTwistedTwigleg Posts: 2,624Admin
    edited June 2019

    In the script with the tire function, are you sure can_shoot is set to true correctly? Other than that, I don’t really know. As @twondia said, any information the debugger might have could be helpful.

    Something you could try to help debug the issue is add a bunch of print statements at various points in your script(s) to see which part of the code is not executing. It is long and tedious, but generally I find it helps narrow down what could be causing an issue.

    (Side note: Welcome to the forums!)

  • OrgamekOrgamek Posts: 6Member
    edited June 2019

    Thank for your input, and the welcome guys.

    So, the part of code that allow me to declare the balle variable is the following:

    extends "res://Armes/"
    func init():
        balle = load("res://Armes/balles/coupPioche.tscn")
        gun_cooldown = 3
        num = 1
        degats =10
        dispersion =0

    To clarify what i did: i created a classe which contain the code shared by all the weapon. It is a virtual classe. Then i created an herited class of this function for each weapon ingame, that i can instency. The balle (bullet, sorry for the french bit. ^^') is a variable of the weapon.

    As for the debugger, there is indeed an error i didn't check:
    "Can't emit non existing signal 'shoot'. "

    Finally the variable can_shoot is created with the value 'true', so this part is ok.

    I might have missed something about signal. Also, is there a way to link them with the cript? Because, if i want to generate ennemy dynamicaly, they won't be abble to shoot since they won't be linked.

    Anyway, thank for the help. :+1: :D

  • TwistedTwiglegTwistedTwigleg Posts: 2,624Admin

    Alright, now I think I know some of what the problem is and how to fix it.

    To fix the debugger error, you need to add the following to script with the weapon:

    extends Node2D # or whatever node being used
    signal shoot # Defines a new signal

    This will tell Godot to define a new signal for this node, that will allow you to emit it with the emit_signal function.

    Hopefully that will fix the signal issue and then the script will work as intended.

    @Orgamek said
    I might have missed something about signal. Also, is there a way to link them with the script? Because, if i want to generate enemy dynamically, they won’t be able to shoot since they won’t be linked.

    You can connect signals through the connect function. For example, to connect the on_body_enter function in an Area2D node through code, you could do this:

    extends Node2D
    func _ready():
        var area2d_node = get_node(“Area2D”)
        area2d_node.connect(“on_body_enter”, self, “body_entered_function”)
    func body_entered_function(body):
        # NOTE: the body variable is passed by the signal!
        print ( + “ Collided with area!”)

    I only glanced through it, but the documentation has a page explaining how signals in Godot works that appears to be pretty good and explains both how to connect signals in code and through the editor.

    Hopefully this helps :smile:

  • OrgamekOrgamek Posts: 6Member
    edited June 2019

    Hello all, thank for the help.

    I added the bit of code you advised me to put @TwistedTwigleg , and tried to make the code work this week end. Well, from the methode 'print("i_ve_reached_this point")' it seems that the signal is emitted now. However, the scene does not seems to listen to said signal, and the debugger only tells me that the error was droped. I tried to expand the error part but it only gave me the following line:

    E 0:00:00:0619 Too many errors! 9 errors were dropped.
    "<"C Source ">" :24

    Anyway, i will keep trying to find th mistake.

    .If found_something():
    . print(explanation)

    See you peps. :D

    Edit: i added the " around the < and >, because otherwise, C Source was not visible.
    Also, the indent is to small. xD

  • OrgamekOrgamek Posts: 6Member

    Hi all,

    I think i found a part of the problem: it came from the fact i created another signal with the same name for the node 'personnage', which did not contain the required information to implement a bullet. I removed it.

    My problem is now the the following:
    my caracter has a variable "weapon", which can emit a signal to generate bullet. When i click, the function is called, (good), the shoot is realised (good) but the bullet is not generated (meh...). The problem come from the fact that i can't link the signal of the weapon to the scene, as it does not appear in the "node / signal" part of Godot.
    Now, an easy work around would be to just put the function of the weapon into the 'personnage' node, making the variable "weapon", just a tank of data. I will do that is there is no other alternatives. But i would like to try to connect the signal of "weapon" to the main scene.

    Any suggestions?

    Anyway, thank for reading, and sheer. :)

  • OrgamekOrgamek Posts: 6Member
    Accepted Answer

    I finally applied the fix i described earlier: basically gun don't kill peoples. Peoples kill peoples. xD
    I will close the tread.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file