0
\$\begingroup\$

I have level 0 rusted sword, then I upgrade it and save my game.

I implemented my save system by following Godot tutorials. But with this approach, after exiting and re-launching the game, all rusted swords in the game are now upgraded.

What do I have to do so that only that specific sword is upgraded?

My current system works like this:

detect item on floor:
       press key to pick:
        check what type of item it is:
          check if item slot is viable:
              delete item from floor, add into the item slot 
         

And I reverse the order for dropping items, which of course means that item ID changes every time it is dropped and instanced again, so using that is problematic.

func savePlayerData():
    var data = {
       "strength": strength,}
    var dir = Directory.new()
    if !dir.dir_exists(SAVE_DIR):
        dir.make_dir_recursive(SAVE_DIR)
    var file = File.new()
    var error = file.open_encrypted_with_pass(save_path, File.WRITE, "P@paB3ar6969")
    if error == OK:
        file.store_var(data)
        file.close()

func loadPlayerData():
    var file = File.new()
    if file.file_exists(save_path):
        var error = file.open_encrypted_with_pass(save_path, File.READ, "P@paB3ar6969")
        if error == OK:
            var player_data = file.get_var()
            file.close()
            if "strength" in player_data:
                strength = player_data["strength"]
\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

Let us talk about inventories.

Real world inventories.

An object in a store have a unique id (e.g. a Serial Number) and a model (e.g. "Rusted Sword 2000").

And there might be other information attached to it, for example a manufacturer/provider (e.g. "Acme Inc."). Now, is that attached to the unique id or the model?

Well, all objects of the same model are by the same manufacturer ("Acme Inc." makes "Rusted Sword 2000"), so it does not make sense to attach that to the unique id, but to the model.

Other attributes might be different per item. For example if you need to track where it is in the store, or if it is damaged, and so on. That information need to be attached to the unique id.


Now, in your game, you have rusted sword object. Does that object represent the unique item, or does it represent the model? Well, you might need both.

You will have an object that represents the item, which have information about it such as the durability, or upgrades that the player has added to it.

And that object must have a reference to another object that represents the model, which will have information common to all items of the same kind, for example its base stats.


Now, as you can imagine, if you put the upgrades in the object that represents the model, then it will apply to all items. Which seems to be the issue at hand. And you would solve it by moving that information to the object that represents the unique item.

It might be the case that you do not have an object to represent the unique item, in which case you need to come up with it.

In any case, I believe you need to think about the structure of your data. And this is not a problem of how you store or load it.

Addendum: I've been saying object, but these might be rows in a table, or items in an array, whatever works.

\$\endgroup\$
1
\$\begingroup\$

Hey Blue the post of Theraot perfectly describes your problem.

Consider to create an identifier to identify a specific item.

Your item class:

// Set properties
var item_id : int = uniqueID
var name : String = ""
var description : String = ""
var strength_bonus : int = 0

// then we define a constructor
func _init(item_id: int, name: String, description: String strength_bonus: int):
self.item_id = item_id
self.name = name
self.description = description
self.strength_bonus = strength_bonus

// we define a method to upgrade an item stength
func upgrade_item(new_strength_bonus: int) -> void:
strength_bonus = new_strength_bonus

When we saving our data we save an array of items:

    var data = {
    "items": []  // array to store items of type itemclass
}

When loading our data we loop through the items:

    for item_data in player_data["items"]:
    var item_id = item_data["item_id"]
    var item = find_item_by_id(item_id) // this would be a helper method that needs to implemented first
    

I hope this was helpful :)

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.