Invalid set index 'global_position' (on base: 'Nil') with value of type 'Vector2'.

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

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.


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


:bust_in_silhouette: Reply From: jgodfrey

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 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():

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
		elif event.is_action_released("left_click"):
			selected = false

Thanks a lot! I’ll give a try :smiley:

Maranpis | 2022-03-16 06:30

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.

Maranpis | 2022-03-16 06:45