• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Collectibles

S

Steve Walsh

Guest
Hi, I've recently made working collectibles in my game, but whenever I die after a checkpoint or when moving to the next room, the collectible counter resets and they all respawn where they were.

Can anyone help me make it so that collectibles are saved/carried over to the next room? It's probably really simple, but I've relatively new to the engine and can't seem to find anything useful online.
 

NicoFIDI

Member
1st - dont reset them anywhere.
then in how to save them you have multiples ways:
A- make the variables global
B- make a persistent object to store them
C- make persistent objects that represent each collectible and check if exists to know if it's there
D- Something i didnt think
 

TheouAegis

Member
You need to set the variable(s) for the collectibles in a room that exists outside the scope of your game (a preloader room). Obviously this means you'll need to either a) have a persistent object in that room which will exist throughout the rest of the game, or b) make the variable(s) for the collectibles global so that it/they exist throughout the rest of the game.

If you need the variables to be saved between sessions, encrypt the variable data then write it to an INI or binary or text file. Or use a password system if you feel old-school.
 
S

Steve Walsh

Guest
Thanks for the suggestions, this is taking a while for me to understand. Basically I've made my score hud persistent, which makes the number of collectibles save between rooms and it saves when I die too, now the only thing wrong is that when I die, the collectibles still respawn, so it would be an easy way to farm them, I'm going to try and see if I can permanently destroy them after collecting them, so that they never respawn after death, but any suggestions would be greatly appreciated.
 

TheouAegis

Member
if whatever_variable_is_keeping_track_of_collectibles
instance_destroy()
Just put that in the Create Event of the objects. If the variable that is keeping track of them is set, destroy them immediately.
 
N

NodziGames

Guest
I'm under the assumption you're still using save states to load and save the game? If you are I suggest you learn how to use .ini file as soon as you can. They're a little bit intimidating, but it's very rewarding once you get to know them. I suggest you follow this tutorial to learn it:

So as an example, I'd typically store collectible data as follows.

global.collectible1 = 0;
global.collectible2 = 0;

0 being false (In other words I didn't get it yet), and 1 being true. You'd typically call the following code when you start up the game, or click play:

ini_open("save.ini");
global.collectible1 = ini_read_real("COLLECTIBLES", "collectible1", 0);

What ini_open does, is it looks for a save file stored in your localappdata folder called save.ini, and if the file exists it loads it into your memory, otherwise it creates the file. the next line creates a global variable (global means it can be accessed from any object, not just the one it's initialized on), and reads data from the file. ini_read_real means it reads a real number, ini_read_string reads a string from the file. The first parameter is basically a section in the file, in our case we call it COLLECTIBLES. It's good practice to capitalize the sections to distinguish them easier. the next parameter reads "collectible1", that's the name of the value in the COLLECTIBLES section. The 3rd parameter is a default value, so in case the player hasn't played before or collectible1 doesn't exist in the file yet, it default global.collectible1's value to 0.

In your collectible object, you want to add something along the following lines in your create event:



if (global.collectible1 == 1) //In other words if it has been collected
{
instance_destroy(); //Destroy itself
}



Remember that you need to load up from the ini file at the start of the game, otherwise you'll run into errors since the variable doesn't exist.

Okay, so now onto collecting the collectible. In the Collides with player event on the collectible object, add the following lines of code:



global.collectible1 = 1; //We're now setting the value to one so that the game knows we collected it.

//Let's make sure to tell the save file too!
ini_write_real("COLLECTIBLES", "collectible1", global.collectible1);

//And last but not least, we now need to remove the collectible from the playfield.
instance_destroy();



ini_write_real has a very similar structure to ini_read_real. First parameter is "COLLECTIBLES", which once again is the section, "collectible1" is the name of the value, technically called the Key, and the third parameter is the value we want to put into the file. In our case we're putting global.collectible1's value in which equates to 1 since it's now collected.

With all that done, when the player dies simply restart the room. Your collectible will now no longer appear a second time. Keep in mind that using save states alongside ini files will cause a lot of issues, so rather move to ini files completely. After all, they're much more reliable. If you need any help, feel free to drop me an email at "[email protected]". I'd be more than happy to help you in my free time.
 
Top