+1 vote

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!

in Engine by (21 points)
recategorized by

2 Answers

+1 vote
Best answer

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: https://www.youtube.com/watch?v=wuxal3C0800

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.

by (1,242 points)
selected by

Sounds great. Will report back.

+1 vote

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

by (32 points)
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.