0 votes

Hi guys:

I am trying to create an inventory system. When I move the item through the different slots between 10 or 30 times, it gives me an error that says : "Invalid set index 'global_position' (on base: 'Nil') with value of type 'Vector2'." I will leave you a link to the project in case someone can guide me a bit and tell me exactly what I'm doing wrong. As far as I know can be a problem related to when the function update items is called, the global position is not ready to get the value. I know that the way I'm doing the inventory is not the most efficient one, but somehow it is the way I understand how to do it.

GodotInventoryProject

I would really appreciate your help because I've reached a point where I don't know what I can do.

thanks.

Godot version 3.4
in Engine by (26 points)

1 Answer

0 votes

Looks like you're mixing some input concepts, which is causing your item_release() function to be called multiple times in a row. That, in turn, causes update_items() to be called with NONE of your array items being selected. That causes it to return nothing to the caller, which is then used to try to set global_position(), resulting in your error.

Without attempting to redesign everything, here's one way to fix it. Replace your existing Blue_Potion.gd with the following:

extends Node2D
class_name Blue_Potion

signal blue_potion_released
signal blue_potion_picked
onready var N_Inventory: Control = find_parent("Inventory")
var selected = false

func _ready():
    connect("blue_potion_picked",N_Inventory,"on_blue_potion_picked")
    connect("blue_potion_released",N_Inventory,"on_blue_potion_released")

func _process(delta):
    if selected:
        global_position = get_global_mouse_position()

func _on_TextureRect_gui_input(event):
    if event is InputEventMouseButton:
        if event.is_action_pressed("left_click"):
            selected = true
            emit_signal("blue_potion_picked")
        elif event.is_action_released("left_click"):
            selected = false
            emit_signal("blue_potion_released")
by (12,477 points)
edited by

Thanks a lot! I'll give a try :D

It works! I knew that emitting that signal every frame will give me further problems. Thanks for your help. I have learned a very important concept for coding.
:)

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.