Godot Version
4.1.3
Question
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):
inv.insert(item)
4.1.3
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):
inv.insert(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 https://www.youtube.com/watch?v=fyRcR6C5H2g&list=PL3cGrGHvkwn2NOT1LSwf5d2XZmlc5Bjsn&index=5&t=912s at time stamp: 6:10
Can you show your entire player.gd
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
move_and_slide()
play_anim(direction)
func play_anim(dir):
var anim = $AnimatedSprite2D
if player_state == "idle":
anim.play("idle")
if player_state == "walking":
if dir.y == -1:
anim.play("n-walk")
if dir.x == 1:
anim.play("e-walk")
if dir.y == 1:
anim.play("s-walk")
if dir.x == -1:
anim.play("w-walk")
if dir.x > 0.5 and dir.y < -0.5:
anim.play("ne-walk")
if dir.x > 0.5 and dir.y > 0.5:
anim.play("se-walk")
if dir.x < -0.5 and dir.y > 0.5:
anim.play("sw-walk")
if dir.x < -0.5 and dir.y < -0.5:
anim.play("nw-walk")
func player():
pass
func collect(item):
inv.insert(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
else:
var emptyslots = slots.filter(func(slot): return slot.item == null)
if !emptyslots.is_empty:
emptyslots[0].item = item
emptyslots[0].amount = 1
update.emit()
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
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.