error (31,5) the method "instance" isnt delclared on base string ! please help. im copying a tutorial

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BigKing

extends KinematicBody2D

const SPEED = 60
const GRAVITY = 10
const JUMP_POWER = -200
const FLOOR = Vector2(0, -1)

const FIREBALL = (“res://characters/fireball.gd”)

var velocity = Vector2()
var on_ground = false

func _physics_process(_delta):

if Input.is_action_pressed(“ui_right”):
velocity.x = SPEED
$AnimatedSprite.play(“run”)
$AnimatedSprite.flip_h = false
elif Input.is_action_pressed(“ui_left”):
velocity.x = -SPEED
$AnimatedSprite.play(“run”)
$AnimatedSprite.flip_h = true
else:
velocity.x = 0
if on_ground == true:
$AnimatedSprite.play(“fall”)

if Input.is_action_pressed(“ui_up”):
if on_ground == true:
velocity.y = JUMP_POWER
on_ground = false

if Input.is_action_just_pressed(“ui_focus_next”):
var fireball = FIREBALL.instance()
get_parent().add_child(fireball)

velocity.y += GRAVITY

if is_on_floor():
on_ground = true
else:
on_ground = false
if velocity.y < 0:
$AnimatedSprite.play(“jump”)
else:
$AnimatedSprite.play(“idle”)

velocity = move_and_slide (velocity, FLOOR)

:bust_in_silhouette: Reply From: jgodfrey

A few things…

First, please edit your post and put the code inside code tags so it’ll be easier to read in the forum.

You can do that by:

  • Paste the code into your post
  • Select the code
  • Press the “{}” icon in
    the editor’s toolbar

Now, the problem is here:

var fireball = FIREBALL.instance()

Looking for the definition of FIREBALL, that’s this:

const FIREBALL = ("res://characters/fireball.gd")

So, it’s just a string, that contains a path to a script.

A string doesn’t have an instance function.

I assume you intended to load/preload FIREBALL (as a scene?) prior to attempting to instance it. Take a look at the tutorial again, and pay special attention to what else is happening with FIREBALL in the code. You’re missing something.

If you can’t find it, link the tutorial here for more assistance.

:bust_in_silhouette: Reply From: njamster

The error message tells you the line number in which the error occurred: 31. It also tells you, that you are trying to use a method called “instance” on something that is actually a “string”, thus didn’t declare such a method. And that’s precisely the problem here: you define FIREBALL as a string, but further on treat it like a resource.

What you should do instead is passing this string to the load-function. Then it would no longer be a constant expression though, so you would need to change const into var. Lastly, your loading a script here (a file ending in .gd) but likely want to instance a scene instead (a file ending in .tscn). So here is the corrected version:

var FIREBALL = load("res://characters/fireball.tscn")

TL;DR: Your tutorial sucks or you were to careless when copying from it.