Skip to content

Changes for inventory_grid.gd regarding create_and_add_item for implementing get_item_at_position #37

@filipinyo

Description

@filipinyo

Hey there. Since create_and_add_item function has been added yesterday, I believe that it needs a separate implementation for the inventory_grid.gd, because when we add and create an item in inventory_grid, it also gets added a position, but in the base inventory, item position is not added. I've noticed this because I wanted to implement a function that gets an item at the certain position in the inventory. This function would be useful for mapping items to hotkeys etc.

For getting item at the position I've made this function

func get_item_at_position(selected_position: Vector2) -> InventoryItem:
	if(
		selected_position.x < 0 || 
		selected_position.x > size.x ||
		selected_position.y < 0 ||
		selected_position.y > size.y
	):
		assert("Selected position out of bounds")

	for item in get_items():
		var item_position = item.get_property(KEY_GRID_POSITION, Vector2.ZERO)
		if (
			item_position.x == selected_position.x && 
			item_position.y == selected_position.y
		):
			return item
	
	return null

For overriding the base classes function we can use the same function which calls the inventory_grid's add_item functions that handles position properties.

func create_and_add_item(prototype_id: String) -> InventoryItem:
	var item: InventoryItem = InventoryItem.new()
	item.prototype_id = prototype_id
	if add_item(item):
		return item
	else:
		item.free()
		return null

If this is already possible in the current implementation, I'd like to know how, so I can use it the way it was meant to be used :D

PS: I really like the plugin

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions