im trying to figure out add child and I was following a tutorial where he used
extends Node2D
var Laser = preload("res://Scenes/Laser.tscn")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
spawn(Vector2(0,0))
func spawn():
var instance = Laser.instantiate()
instance.position = pos
add_child(instance)
I changed the function name to spawn otherwise its the same what do I replace pos with since i tried putting a vector 2 there and stuff but it isnt working what do I have to put there?
be more specific. does it throw an error or just nothing happens?
what did you change?
your function needs an argument. you either skipped doing it or deleted it at some point.
func spawn(pos):
var instance = Laser.instantiate()
instance.position = pos
add_child(instance)
also, any tutorial that doesn’t use static typing is a bad tutorial. I recommend the godot official docs, as that explains everything very well and broken up into small, easy to understand parts.
this is what the code should look like with static typing. you need it to catch possible bugs and understand it better. it also improves performance. for instance, pos should be a Vector2, and then only accept a Vector2, and with static typing, the editor will tell you more clearly what is wrong.
other things I changed: function returns void, meaning it doesn’t return anything.