0 votes

Hi I'm new in Godot and I was trying the tutorial for Dodge the creeps, but it gives me this error Invalid call. Nonexistent function "instance" in base "Pathfollow2D", i already check that the PackedScene is in the inspector, this is the code:

extends Node

export (PackedScene) var Mob
var score

func _ready():
    randomize()


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

func new_game():
    score = 0
    $Player.start($StartPosition.position)
    $StartTimer.start()
    $HUD.update_score(score)
    $HUD.show_message("Get Ready")


func _on_MobTimer_timeout():
    $MobPath/MobSpawnLocation.set_offset(randi())
    var Mob = Mob.instance()
    add_child(Mob)
    var direction = $MobPath/MobSpawnLocation.rotation + PI /2
    direction += rand_range( -PI/4, PI/4)
    Mob.rotation = direction
    Mob.linear_velocity = Vector2(rand_range(Mob.MIN_SPEED, Mob.MAX_SPEED), 0)
    Mob.linear_velocity = Mob.linear_velocity.rotated(direction)


func _on_ScoreTimer_timeout():
    score += 1
    $HUD.update_score(score)


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

the engine tell me that the problem is in this line: var Mob = Mob.instance(), anyone knows what is wrong with the code?

asked Sep 17 in Engine by Lucky_Mouse (14 points)
edited Sep 17 by volzhs

When you do a Mob.instance() you get an instance of the Mob scene returned. So you don't want to overwrite Mob variable. Instead in on_MobTimer try doing var mob = Mob.instance() (lowercase mob). And then in the same function change the Mob to mob everywhere.

Please log in or register to answer this question.

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.