How can I give players random loot (scenes)

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

I’m making an RPG game where players will get random loot (scenes) when opening chests.
I would like for my scenes to have attributes or tags (such as “sword”, “uncommon”, “freezing”, etc.) that my game will query for when it determines whats in the chest.
For example, it may query for all scenes with “rare” and “staff” then choose a random one from the results.

I have thought of using a folder structure, but that may become impractical if some items have complex tags (such as being both “freezing” and “fire”)
Also, I think that instancing each scene just to see its attributes may be cumbersome in game

:bust_in_silhouette: Reply From: Diet Estus

You could create a singleton called Items.tscn that is always in your tree, and whose children are scenes corresponding to your items.

You could then query these children’s attributes.

Or you could classify these children using Godot’s groups. Then from your Chest scene you could do something like this:

func spawn_rare_item():
    var rare_items = get_tree().get_nodes_in_group("rare")
    var choice = randi(rare_items.size()) - 1
    var item = rare_items[choice].duplicate()
    return item

But you would want to remove the duplicate item from the group, otherwise it would throw off your probabilities.