How to structure and populate game data

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

Hello folks!
I am super new to Godot, so excuse my newbie questions.
I have a bunch of weapons. Each weapon has an id, an icon, a name and a price.
What is the best way to represent and populate this data in a script?

My wish would be to have a listing of all items accessible from within my main scene where I can adjust the price a little bit without having to go into the code/a .json file.

What are the best practices for things like this?
Also what are ways to make it a bit scalable? Let’s say I have arms dealers who sell different weapons to different prices and there are different classes of players who get bonuses for different weapons (or something like that).

Thanks a lot!

:bust_in_silhouette: Reply From: MrMrBlackDragon

You could store the data in a Dictonary like this:

var shop : Dictionary = {
Normal = {
    Sword = {
             Price = 12,
             PicturePath = "res://PathToPicture"
    Armor = {}
Rare = {
    Sword = {},
    Armor = {}}

Within script you could call it like so:

var PriceOfItem : int = shop.Normal .Price 

And you get the price of´an Item

:bust_in_silhouette: Reply From: Bernard Cloutier

The typical solution to that is something like Unity’s Scriptable Objects. Here are a couple of videos on how to do that in Godot:

The point is to define a new type of Resource. In your example, a Weapon. You define the Weapon’s properties. Then, in the filesystem window, you right click and create a new resource (you should see your new Weapon resource in the list). You then name and save this new resource (ex: Revolver.res) and edit its properties (icon, name, price, sprite, model, etc.) right in the inspector. Your vendor could simply have a path to the weapon resources folder (ex: res://Items/Weapons/) and iterate on the files in that folder to fill its inventory.

Once you’ve defined your resource, you can keep creating new weapons without having to change one line of code.

Sounds great. Will report back.

JamesButler | 2020-02-07 12:34