TheTonyPo
TheTonyPo

Reputation: 3

Godot3 How can I call a method of an object created in another scene

I am attempting to create a Space Blaster game. I have created the main scene which includes the player sprite and a script attached.

I can create an instance of the object (a bullet) and it appears in the main scene when the space bar is pressed. When I try to call the start_at() method it cannot find the method/function. non existent function 'start_at()' in base Node2D

extends Sprite

# Declare member variables here.
export var rot_speed = 2.6
export var thrust = 500
export var max_vel = 400
export var friction = 0.65
var bullet = preload("res://Scenes/Firing.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
    position = Vector2(get_viewport().size.x/2, get_viewport().size.y/2)


func _process(delta):
    rotation = self.rotation + deg2rad(90 * delta)
    ...

func shoot():
    var b = bullet.instance()
    #bullet_container.add_child(b)
    add_child(b)
    #b.set_position(position)
    #b.set_rotation(rot - PI/2)
    b.start_at(rotation, position)

Code for the bullet with the called function in:

extends Area2D

# Declare member variables here. Examples:
var vel = Vector2()
export var speed = 1000

# Called when the node enters the scene tree for the first time.
func _ready():
    set_physics_process(true)


func _physics_process(delta):
    vel = vel * delta
    position = position + vel  * delta
    #position = position + vel * delta

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#   pass

func start_at(dir, pos):
    self.rotation = dir
    self.posiiton = pos
    vel = Vector2(speed, 0).rotated(dir)

Please be gentle this is my first attempt with Godot.

Upvotes: 0

Views: 1964

Answers (1)

hola
hola

Reputation: 3500

The error message tells us that the instance that you are calling start_at() on is not a bullet. The instance is also of type Node2D and not Area2D. Is the scene `res://Scenes/Firing.tscn' the correct scene? Maybe the bullet script is not on the root of that scene?

Upvotes: 1

Related Questions