+1 vote

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

asked Sep 30, 2018 in Engine by RoyShaw (24 points)
edited Sep 30, 2018 by RoyShaw

1 Answer

0 votes

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.

answered Oct 1, 2018 by Diet Estus (1,504 points)
edited Oct 1, 2018 by Diet Estus
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 How to use this Q&A? before posting your first questions.