![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | thinkingfield |
Making a simple asteroids game, when a large asteroid gets hit it explodes into a few smaller ones. Code works fine when I load() a new asteroid (the asteroid is a scene) but preload() gives me a parsing error and won’t run. Using load() causes a pause in the game when I hit an asteroid and before it splits. The code is below, but only the first few lines are necessary.
code_
extends “res://Weightless.gd”
#preload() does not work, gets a parsing error
var AsteroidSmall = load(“res://AsteroidSmall.tscn”)
var AsteroidMedium = load(“res://AsteroidMedium.tscn”)
export(float) var explode_force = 200
signal explode
enum Size {
SMALL, MEDIUM, LARGE
}
export(Size) var size = Size.LARGE
var radius
func _ready():
connect(“explode”, self, “_explode”)
radius = (get_node(“Sprite”).texture.get_width() / 2) * get_node(“Sprite”).scale
func _explode():
if size != Size.SMALL:
for i in range(0,3):
var offset_dir = ((PI * 2) / 3) * i
var asteroid
if size == Size.MEDIUM:
asteroid = AsteroidSmall.instance()
if size == Size.LARGE:
asteroid = AsteroidMedium.instance()
asteroid.position = position + radius.rotated(offset_dir)
asteroid.linear_velocity = linear_velocity + Vector2(explode_force, 0).rotated(offset_dir)
get_parent().add_child(asteroid)
queue_free()
sleeping = true
It might help, if you post the exact error message here.
Also make sure your code is formatted correctly (it needs to be intended, select all your code and click the editor-button with the two curly brackets “{ }”.
njamster | 2020-02-24 12:15
Thanks for responding, the error is (only when using preload, load works perfectly, everything else is the same):
Parser Error: Can’t preload resource at path: res://AsteroidSmall.tscn
I am trying to enter the code within the ‘Enter Code Here’ box, but only 1 line shows up in the box, the rest just lists out with no formatting. However, all the indents are correct. The code runs perfectly with ‘load()’ instead of ‘preload()’
extends "res://Weightless.gd"
var AsteroidSmall = preload(“res://AsteroidSmall.tscn”)
var AsteroidMedium = preload(“res://AsteroidMedium.tscn”)
export(float) var explode_force = 200
signal explode
enum Size {
SMALL, MEDIUM, LARGE
}
export(Size) var size = Size.LARGE
var radius
func _ready():
connect(“explode”, self, “_explode”)
radius = (get_node(“Sprite”).texture.get_width() / 2) * get_node(“Sprite”).scale
func _explode():
if size != Size.SMALL:
for i in range(0,3):
var offset_dir = ((PI * 2) / 3) * i
var asteroid
if size == Size.MEDIUM:
asteroid = AsteroidSmall.instance()
if size == Size.LARGE:
asteroid = AsteroidMedium.instance()
asteroid.position = position + radius.rotated(offset_dir)
asteroid.linear_velocity = linear_velocity + Vector2(explode_force, 0).rotated(offset_dir)
get_parent().add_child(asteroid)
queue_free()
sleeping = true
thinkingfield | 2020-02-24 22:24
I am trying to enter the code within the ‘Enter Code Here’ box, but only 1 line shows up in the box, the rest just lists out with no formatting.
That will happen, if you first click the “{ }”-button and then paste your code. Try to first paste your code, then select all of it in the editor and then click the “{ }”-button.
However, I think it’s unlikely anyway that the issue has to do with the rest of your code. While I can’t provide you a solution for your problem, here’s a list of things I would check if I were you. Maybe it helps:
- Have you tried commenting out all code except the preload-lines? Does the error still happen? Then the script obviously has nothing to do with it.
- Do you get the error for both preloading
AsteroidSmall
andAsteroidMedium
? Or just one of them? If so, which one? - Do you have issues preloading other scenes as well? If not, I’d suspect the scenes themselves are the issue and you should provide more details regarding them. If yes, do you have those issues just for this specific project?
- Do you already get the error in the editor? Or just when running the game?
njamster | 2020-02-25 00:14
Okay, so it is from the script or scene. The debugger gives further details:
E 0:00:00.381 load: Resource: ‘res://Asteroid.gd’ is already being loaded. Cyclic reference?
E 0:00:00.385 poll: res://AsteroidSmall.tscn:4 - Parse Error: [ext_resource] referenced
E 0:00:00.385 poll: res://AsteroidSmall.tscn:4 - Parse Error: [ext_resource] referenced
This script is the asteroid.gd script. If I remove the scripts from asteroidsmall and medium it will preload, however, none of the functionality is there. Do I need to copy the script for each scene separately? I cannot reference the same script from different scenes if I want to preload (but it does work for load). Seems a bit odd!
Thanks for your help.
thinkingfield | 2020-02-25 00:51
I just created two very simple scenes and attached the same script to both. Preloading them like you did works perfectly fine. So I’m pretty sure your asteroid.gd-script is the issue! Are you loading or preloading anything in there as well? You should post it.
njamster | 2020-02-25 12:02