Cannot call non-static function "insert()"

Godot Version



how do i fix this error
Cannot call non-static function “insert()” on the class “Inv” directly. Make an instance instead.
on this code:

func collect(item):

You will need to provide more context. The code you provided does not show us what inv is or where it comes from, so we can not tell you what is wrong.

sorry i was following a youtube tutorial for making a game and im really new to coding so i wasnt really sure what code i should send for context if it helps the code is part of this video at time stamp: 6:10

Can you show your entire script? There must be a difference between your script and the script in the video, as the one shown there should not produce the error you have.

Please put the code between ``` lines like this:

func _ready():
    print("hello world")

That will correctly format the code in the forum, so it looks like this:

func _ready():
    print("hello world")
extends CharacterBody2D

var speed = 100

var player_state

@export var inv = Inv

func _physics_process(_delta):
	var direction = Input.get_vector("left", "right", "up", "down")
	if direction.x == 0 and direction.y == 0:
		player_state = "idle"
	elif direction.x != 0 or direction.y != 0:
		player_state = "walking"
	velocity = direction * speed

func play_anim(dir):
	var anim = $AnimatedSprite2D
	if player_state == "idle":"idle")
	if player_state == "walking":
		if dir.y == -1:"n-walk")
		if dir.x == 1:"e-walk")
		if dir.y == 1:"s-walk")
		if dir.x == -1:"w-walk")
		if dir.x > 0.5 and dir.y < -0.5:"ne-walk")
		if dir.x > 0.5 and dir.y > 0.5:"se-walk")
		if dir.x < -0.5 and dir.y > 0.5:"sw-walk")
		if dir.x < -0.5 and dir.y < -0.5:"nw-walk")

func player():

func collect(item):

And this is where the class name “Inv” is first declared.

extends Resource

class_name  Inv

signal update

@export var slots: Array[InvSlot]

func insert(item: InvItem):
	var itemslots = slots.filter(func(slot): return slot.item == item)
	if !itemslots.is_empty():
		itemslots[0].amount += 1
		var emptyslots = slots.filter(func(slot): return slot.item == null)
		if !emptyslots.is_empty:
			emptyslots[0].item = item
			emptyslots[0].amount = 1

Ah I see your error! It’s very subtle and easy to glance over. You have this line in the player script:

@export var inv = Inv

…while in the video it is:

@export var inv: Inv

As an explanation, the equal sign = tells Godot “Assign this value to the variable” while the : means “Whatever value I assign to this variable later, it will be of this specific type”. So what the line @export var inv: Inv in the video means is "Create a variable that will later hold a value of type Inv. No value is assigned at that time.

My guess is that at another part of the video the variable will be actually assigned a value.

Thank you. My bad for putting you through all this, I appreciate the help.

Don’t worry, we’ve all been beginners at some point. I’m happy to help :tada:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.