0 votes

this is the code i am very new to godot i really need help please tell me whats wrong..

extends Spatial

onready var main = gettree().currentscene
var enemy = load("res://enemy.gd")

func spawn():``
var enemy = enemy.instance()
main.addchild(enemy)
enemy.transform.origin = transform.origin + Vector3(rand
range(-15,15), rand_range(-10,10)

func _on_Timer_timeout():
spawn()

I am getting a error on func onTimer_timeout(): line please help..

Godot version 3.4.1
in Engine by (12 points)

Quote the error, what does it say ?

1 Answer

0 votes

A couple of things:

  1. You defined the variable enemy twice, once as a member variable and once as a variable in a method. That'll throw an error for redefining a variable. My convention is to call the add scene onto the name of the scene as below.
  2. You had a Vector3 with only 2 components (I just put zero in the third, you might want to change that)
  3. I randomized the seed for you so it's not the same every time.
  4. I changed the member variable to a preload - not an error as such, it's just a better approach imo.

That's all I think unless I missed anything.

extends Spatial

onready var main = get_tree().current_scene
var enemy_scene = preload("res://enemy.gd")

func spawn():
    randomize()
    var enemy = enemy_scene.instance()
    main.add_child(enemy)
    enemy.transform.origin = transform.origin + Vector3(rand_range(-15, 15), rand_range(-10, 10), 0)

func _on_Timer_timeout():
    spawn()
by (1,789 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 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.