Help: How To Save Coins

Godot Version

Version 4.2.1

Question

I’m making a 2d platformer game and have been implementing a saving system. The save files are saved as a JSON file as a dictionary and so far store the character’s position and stats. Next I have to save the coins on the map (whether they are collected or not) which brings me to this problem, how should I save the coins? I am new to this I don’t know all the tricks and can’t think of any way to do this except having a separate script for each coin containing a variable of whether it is picked up, which is then saved. Please tell me there is a better way to do this more simply.

I would suggest creating an autoload. An autoload is a script resource that is present throughout your game and is not tied to any specific scene.

You can name the Autoload whatever you find fitting and then add a variable to it called var coins: int = 0 and then increase this value whenever you get a coin.

When you want to save it to a file, you can always refer to the coins in your autoload. Autoloads are simply the best, you can even have several Autoloads at the same time.


Sorry, i misunderstood.
Here would be my attempt to crafting a coin tracking system.

  1. Every coin on each scene has their own id/number
  2. The savefile has a dictionary with an array of coins collected
var collected_coins: Dictionary = { 
   "level1" : [0, 1, 3, 5, 6, 7], 
   "level2" : [1, 2 , 5, 6]
}
  1. In each scene’s _ready() method, check the collected_coins variable for the specific level. Loop through the array. Every coin with the matching id will be hidden
  2. Whenever the player collects a coin, append the id/number of the coin to the specific dictionary entry: collected_coins.level2.append(8)
1 Like

How do I give/find the coins’ ID numbers

You might have a coin scene and also a script for the coin.
You can add the following code to create an export variable
@export var coin_ID: int
afterwards, you can check the Inspector of each placed coin and then set the coin IDs

thank you