what does "invalid get index 'global_position'(on base: 'Node (Laser_weapon.gd)')."

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ceo-Potato

I’m trying to make an asteroids clone and when I try to use the laser it crashes

Laser_weapon.gd

extends Node

var laser_red := load("res://Scenes/common/Lasers/laser_red.tscn")
var laser_blue = load("res://Scenes/common/Lasers/laser_blue.tscn")

func shoot():
     	var laser = laser_red.instance()
        laser.global_position = self.global_position
        get_node("res://Scenes/World/Level.tscn").add_child(laser)

Here I call the “shoot” function
ignore Control_mode

func _unhandled_key_input(event: InputEventKey) -> void:
if Control_mode == 1:
	if (event.is_action_pressed("Space")):
		$Laser_weapon.shoot()

maybe problem is here :
get_node(“res://Scenes/World/Level.tscn”).add_child(laser)

onready var current_scene = get-tree().current-scene
current-scene.add-child(laser)

ramazan | 2022-01-05 11:56

:bust_in_silhouette: Reply From: Inces

Problem is in this line :

laser.global_position = self.global_position

and most likely it is because of script inside laser_red scene. Erro indicates that “global_position” property does not exist there. It may be so if script of red_laser extends Node instead of any Node2D inheritants.

By the way, what is this syntax := in

var laser_red := load("res://Scenes/common/Lasers/laser_red.tscn")

is it a mistake ?

var laser_red := load("res://Scenes/common/Lasers/laser_red.tscn")

no it’s not a mistake I was just trying if it works

and can you make your answer more clear, please

Ceo-Potato | 2022-01-05 15:04

Your scene, “res://Scenes/common/Lasers/laser_red.tscn”, it has a script (Laser_weapon.gd). Error indicates, that “global_position” property does not exist in this script. Global_position is a property that is built in for Node2D, Canvas Item and Control Node and their inheritants. Only pure Node class doesn’t have this property. You propably made this script to extend from pure Node class. Show this script please

Inces | 2022-01-06 12:33

the script is in Laser_weapon and laser_weapon is a “Node”, not “Node2d” just a node
and laser_red/laser_blue are just scenes that will load they don’t have a script they will have for them to move and stuff but in the meantime, they don’t

Laser_weapon.gd

extends Node

   var laser_red := load("res://Scenes/common/Lasers/laser_red.tscn")
   var laser_blue = load("res://Scenes/common/Lasers/laser_blue.tscn")
   onready var current_scene = get_tree().current_scene

   func shoot():
     var laser = laser_red.instance()
         laser.global_position = self.global_position
         get_node("res://Scenes/World/Level.tscn").add_child(laser)

Ceo-Potato | 2022-01-06 13:32

Let me repeat :
Laser script must be Node2D if You want to use global_position.

Make first line of laser script:

extends Node2D

or change Node to Node2D in editor

Inces | 2022-01-06 20:45