0 votes

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()
Godot version 3.4.2
in Engine by (17 points)

maybe problem is here :
getnode("res://Scenes/World/Level.tscn").addchild(laser)

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

1 Answer

0 votes

Problem is in this line :

laser.global_position = self.global_position

and most likely it is because of script inside laserred scene. Erro indicates that "globalposition" 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 ?

by (5,220 points)
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

Your scene, "res://Scenes/common/Lasers/laserred.tscn", it has a script (Laserweapon.gd). Error indicates, that "globalposition" property does not exist in this script. Globalposition 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

the script is in Laserweapon and laserweapon is a "Node", not "Node2d" just a node
and laserred/laserblue 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)

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

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.