+1 vote

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

in Engine by (15 points)

1 Answer

0 votes

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.

by (805 points)
edited by
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 Frequently asked questions and 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 [email protected] with your username.