Recently i i have started to desenvolve in Godot but i need a little help in something...

if Input.is_action_just_pressed("ui_accept"):
	var laser_instance = PRE_LASER.instance()
	if laser_instance:
		var laser = laser_instance as PRE_LASER
		get_parent().add_child(laser)
		laser.global_position = global_position

With this, im facing the following error:

     Invalid call. Nonexistent function 'instance' in base 'GDScript'.

How exactly can i resolve this?

  • xyz replied to this.

    Zura You should not call a nonexistent function.

    • Zura replied to this.

      If PRE_LASER is a PackedScene, instance() in Godot3 was renamed to instantiate() in Godot 4.

      But then as PRE_LASER isn't correct. Instead, use the proper node type for the root node of that scene.

      Ignore the above, I missed this: in base 'GDScript'.

      • Zura replied to this.

        xyz Yeah, but isn't there anything that can replace "instace"? As far as i know, in version 3 it existed 🙁

        DaveTheCoder But even leaving the script like this:

        if Input.is_action_just_pressed("ui_accept"):
        	var laser_instance = PRE_LASER.instantiate()
        	if laser_instance:
        		var laser = laser_instance as PRE_LASER
        		get_parent().add_child(laser)
        		laser.global_position = global_position

        I receive the following error:

          Invalid call. Nonexistent function 'instantiate' in base 'GDScript'.
        • xyz replied to this.

          Which version of Godot are you using?

          How is PRE_LASER declared and initialized?

          • Zura replied to this.

            DaveTheCoder Godot v4.1.1-stable

            PRE_LASER starts like this:

            const PRE_LASER = preload("res://scripts/laser.gd")

            Zura Post the complete code. We don't see how PRE_LASER is created in your snippet.

            If you want to instantiate an object from a script class, you need to use new(), not instantiate() which is a PackedScene method.

            When you get such errors always look into class reference in the official docs to check if the method actually exists and if there are differently named methods that can do what you want.

            • Zura replied to this.

              xyz Got it!

              extends Node2D

              const PRE_LASER = preload("res://scripts/laser.gd")

              var vel = 90

              Called when the node enters the scene tree for the first time.

              func _ready():
              pass # Replace with function body.

              Called every frame. 'delta' is the elapsed time since the previous frame.

              func _process(delta):
              var dirX = 0
              var dirY = 0

              if Input.is_action_pressed("ui_left"):
              	dirX += -2
              if Input.is_action_pressed("ui_right"):
              	dirX += 2
              if Input.is_action_pressed("ui_up"):
              	dirY += -2
              if Input.is_action_pressed("ui_down"):
              	dirY += 2
              	
              if Input.is_action_just_pressed("ui_accept"):
              	var laser_instance = PRE_LASER.instantiate()
              	if laser_instance:
              		var laser = laser_instance as PRE_LASER
              		get_parent().add_child(laser)
              		laser.global_position = global_position
              
              
              	
              translate(Vector2(dirX, dirY) * vel * delta)
              
              global_position.x = clamp(global_position.x, 20, 140)
              
              global_position.y = clamp(global_position.y, 15, 273)
              • xyz replied to this.

                Zura Yeah, you need to use new() to instantiate the node.