0 votes

Started learning godot(gdscript). making game with official guide(if i can name it like this), and.. code dont work :/
Error(in question but i repeat it here, why not?): Parser Error: Use "onready var mobspawnlocation = get_node(...)" instead.

 extends Node

export(PackedScene) var mob_scene
var score

func _ready():
  randomize()
  new_game()


func game_over():
  $ScoreTimer.stop()
  $MobTimer.stop()

func new_game():
  score = 0
  $Player.start($Player.StartPosition.position)
  $StartTimer.start()

func _on_ScoreTimer_timeout():
  score += 1

func _on_StartTimer_timeout():
   $MobTimer.start()
   $ScoreTimer.start()

func _on_MobTimer_timeout():
   #Create a new instance of the Mob scene.
   var mob = mob_scene.instance()

#Choose a random position in Path2D
var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
mob_spawn_location.offset = randi()

#Set the mob's direction perpendicular to the path direction.
var direction = mob_spawn_location.rotation + PI / 2

#Set the mob's position to a random location.
mob.position = mob_spawn_location.position

#Add some randomness to the direction.
direction += rand_range(-PI / 4, PI / 4)
mob.rotation = direction

#Choose the velocity for the mob.
var velocity = Vector2(rand_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)

#Spawn the mob by adding it to the Main scene.
add_child(mob)

srry for bad formatting, i newbie here. and srry for my english - i from russia and know english.. on basic level

Godot version 3.5.1
in Engine by (17 points)

As the error message suggests, use onready like so onready export(PackedScene) var mob_scene that ensures the _ready function of whatever object you're trying to assign runs before being assigned to a variable.

didn't quite understand how to use it
when i adding

onready export(PackedScene) var mob_spawn_location

in script editor window i see this error:

expected "var"

in debugger i see

Parser Error: Variable "mob_spawn_location" already exists in this class (at line: 5).

1 Answer

0 votes

I think it's because your indentation isn't right. When copying code from the documentation, you need to redo the indentation.
Line :
var mobspawnlocation = getnode("MobPath/MobSpawnLocation")
it's supposed to be in the _on
MobTimer_timeout function.
the editor don't understand why it's outside everything and suggest to add the keyword onready in front of the line because you can. But in fact that's not the solution here since all other line need to be as well in the timeout function.

Hope I'm clear enough, I'm not English too ^^

by (58 points)
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 Frequently asked questions and 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 [email protected] with your username.