Help removing item from inventory array

I edited my previous comment. Let me mess around with it tonight and I’ll give you an update!

1 Like

It’s going to take a bit to really test what we have so I’m going to mark this as solved since you did fix my immediate concern to just get the item to be removed. Thank you so much and if I get stuck again maybe you’ll see my next post! Cheers.

I checked the solution box on your post. If I need to do more to mark this complete let me know. I don’t see anything else that stands out.

1 Like

That’s all, marking my response as a solution is enough.
Good luck in your project then!

I meant to ask one thing. Couldn’t this syntax be used to add items as well? Maybe just changes the two counter numbers? If it’s more than that then don’t worry about looking into it. I have a way working but it involves disabling and enabling area2d that the video made for picking items up off the ground and its not efficient at all for what I need.

maybe change var counter: int = -1 to +1 and counter += 1 to -=1?

func add_item(item_data: ItemData, quantity: int) -> bool:
	var counter: int = -1
	for slot_data in slot_datas:
		counter += 1
		if slot_data == null:
			continue
		elif slot_data.item_data != item_data:
			continue
		elif slot_data.quantity < quantity:
			continue
		elif slot_data.quantity == quantity:
			slot_datas[counter] = null
			inventory_updated.emit(self)
			print("SlotData removed")
			return true
		else:
			slot_data.quantity -= quantity
			inventory_updated.emit(self)
			print("SlotData quantity updated")
			return true
	print("No item of this type found")
	return false

No, the counter is not adding or removing items. It is holding the current index of the for loop iteration.
See below updated code. I wrote this on mobile, so there might be some issues with it, let me know if there are any.

func add_item(item_data: ItemData, quantity: int) -> bool:
	var counter: int = -1
	var first_empty: int = -1
	for slot_data in slot_datas:
		counter += 1
		if slot_data == null:
			first_empty = counter
			continue
		elif slot_data.item_data != item_data:
			continue
		elif slot_data.quantity + quantity > slot_data.MAX_STACK_SIZE:
			continue
		else:
			slot_data.quantity += quantity
			inventory_updated.emit(self)
			print("SlotData quantity updated")
			return true
	if first_empty == -1:
		print("No empty slot found")
		return false
	else:
		slot_datas[counter] = SlotData.new()
		slot_datas[counter].item_data = item_data
		slot_datas[counter].quantity = quantity
		inventory_updated.emit(self)
		print("SlotData added")
		return true

It works but it adds it to the ‘last’ slot. I’m not certain how the array is formed and the slot that appears to be last might actually be the first but any thoughts on getting the item to add from top left onwards and not bottom right? Using a placeholder blank texture for now but the grey is where the item is being added. I’ll do some research on for loops because I got that completely wrong.

image

Test some more and it actually overrides that slot with each item. Say I mined iron and it goes in the bottom right slot. I then go to mine some coal and the coal overrides the iron that was in the slot with coal. So doesn’t quite work.

I can make a new post since this one was technically completed if you prefer since it might not be a quick update?

I updated the code, try again with this now

Nah, it’s fine, let’s fix it here :slight_smile: don’t spam the main board :smiley:

Same issue. Items goes to the bottom right. If there is already an item in the bottom right it just overrides it. Thanks for sticking with me!

I don’t see a reason why it would override anything honestly.
I’d need to look into the actual project to debug, can you share it through GitHub?

That would be a little embarrassing and I’ve never used git before. Let me look at it.

If it helps to call this function out while I look into git. This function was in the video that made you pick up an item from the ground. I don’t want ground items for my project but I tested it and it adds items properly but I couldn’t get it to function through a timer. Maybe something in it can help?

func pick_up_slot_data(slot_data: SlotData) -> bool:
	
	for index in slot_datas.size():
		if slot_datas[index] and slot_datas[index].can_fully_merge_with(slot_data):
			slot_datas[index].fully_merge_with(slot_data)
			inventory_updated.emit(self)
			return true
	
	for index in slot_datas.size():
		if not slot_datas[index]:
			slot_datas[index] = SlotData.new()
			var picked_up_slot_data = slot_datas[index]
			picked_up_slot_data.item_data = slot_data.item_data
			picked_up_slot_data.quantity = slot_data.quantity
			inventory_updated.emit(self)
			return true
	
	return false

It was triggered through an area body and I couldnt get it to work without using a body which doesn’t work well with what I’m doing.

extends StaticBody2D

@export var slot_data: SlotData

@onready var sprite_2d: Sprite2D = $Sprite2D

var body = Global.player

func _ready() -> void:
	sprite_2d.texture = slot_data.item_data.texture

func _process(delta):

func _on_area_2d_body_entered(body):
	if body.inventory_data.pick_up_slot_data(slot_data):
		queue_free()

Did some research and it says to add it as a repository? I try and it says it should be fewer than 100 files.

Does this work? I had to add everything manually and it wouldn’t accept folders.
https://github.com/AnimalBiscuit/TestGame

I can’t access the project unfortunately, you’d need to make it public.
You can look into the GitHub Desktop app to manage your git projects, it makes it very easy:

Ah, it was private. Try now.

It is public now, but without the folders it’s not possible to run it, as there are way too many dependencies broken.
Try the GitHub Desktop app as I mentioned in my previous message, to upload the whole project with the folder structure.
You should be using git for all your projects anyway, so if you’re not using it yet - you’re making yourself a huge favor by learning it now :slight_smile:
See e.g. this topic, where a guy lost a year-old project because he didn’t have a proper backup. Don’t be that guy please :slight_smile:

ok I’ll look into it tonight and come back if I get it set up.

1 Like

It’s asking me to confirm the author and using git bash which doesnt seem to come with the desktop app? I’m just trying to add an item into this array and I’ve spent too long trying to mess with Github. Thanks for your help.

image