Creating a shop system

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

Hello, I want to make a shop system for my game in which I will buy car parts, that have a category i.e brake pads and disks will be in the brakes category, spark plugs, oil filter will be in the engine category. And when I repair the shop that is in my inventory I want a pop up to appear saying that “I look At the rest of the parts and the brakes calipers need changing too”. Basically when I repair a part from a category it should look for other parts in that category. I don’t really know how to do it. I tried with a dictionary for the categories and one sub dictionary for parts that have a value named category and the respective category as value. But working with dictionary is awful for me. If you have any ideas I will be very grateful. Thanks

:bust_in_silhouette: Reply From: wyattb

My suggestion is for now keep it simple. Basically like a flat database.

var inventory: Array =[
	["Engine","Filter",10,20],
	["Engine", "Spark Plugs",10,20],
	["Brakes","Pads",40,20],
	["Brakes","Calipers",40,20],
]

Then just create a function to search or filter the database.

I guess something like a function called

const CATEGORY = 0
const PART = 1

func search_part(category, part_name)->Array:
	for i in inventory:
		if i[CATEGORY]==category and i[PART]==part_name:
			return i
	return []

func filter_parts(category)->Array:
	var cat_parts:Array=[]
	for i in inventory:
		if i[CATEGORY]==category:
			cat_parts.append(i)
	return cat_parts
    	
func _on_Button_pressed() -> void:
	var part_info
	part_info=search_part("Engine", "Spark Plugs")
	print("Not found" if part_info == [] else part_info)
	part_info=search_part("Engine", "Rusty Plugs")
	print("Not found" if part_info == [] else part_info)
	var cat_parts=filter_parts("Engine")
	print("Non found" if cat_parts==[] else cat_parts)
	return

Write your main code to make the shop work. An array is easy to manipulate and you can even do some sophisticated sorts as well as bsearch on it.

In the future you can always rewrite your search and filter methods or any others you have such as insert and delete parts.