Invalid Call. Nonexistent function 'instance' in base 'GDScript'.

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

I am learning Godot through HeartBeast’s tutorial and get this error while using the exact same code.
EDIT: sorry I accidentally post it before putting up the code.

extends Node2D

func _process(delta):
if Input.is_action_just_pressed(“attack”):
var grassEffect = load(“res://Action RPG Resources/Effects/grass”)
var Grasseffect = grassEffect.instance()
var world = get_tree().current_scene
I saw the solution in another similar question but the method does not apply to me. Instead it says “invalid type in function ‘add_child’ in base Node2D, the object-derived class of argument 1 (packed scene) is not a subclass of the expected argument class”

“invalid type in function ‘add_child’” you aren’t trying add Grasseffect instead grassEffect? you needed understand what variable is for what.

Moreus | 2023-03-10 11:13

you could link tutorial and use code as code not text

Moreus | 2023-03-10 11:16

yeah thats the reason, thank you very much

D.Q.Li | 2023-03-10 11:22

Animal welfare is an important issue that affects us all. It’s not just about being kind to animals, but also about preserving the balance of nature and protecting the environment for future generations. Please checkout: Animal Welfare

kitticpet | 2023-03-11 07:13

:bust_in_silhouette: Reply From: zhyrin

Into your grassEffect variable you load a file with a .gd extension, this means the variable will have a type of GDScript. The type GDScript doesn’t have a function called instance() (which is what the error message tells you.

In you code you treat GrassEffect as if it’s a scene, so you need to load your scene file (.tres) into grassEffect.

:bust_in_silhouette: Reply From: aidave

For instancing a .gd file, you use “.new()”

For a scene you use “.instance()” or in v4 it’s now “.instantiate()”

I usually do this to avoid confusion:

var grass_effect_tscn = load("res://grass.tscn")
var grass_effect = grass_effect_tscn.instance()

And also consider on top, for optimization:

const grass_effect_tscn = preload("res://grass.tscn")