need help with currency saving and a little more

D

darakin

Guest
my game has currency that drops when you kill enemies that works totally fine but i have no idea how to make the money save when they leave the game im also trying to use the money to buy the next level with the amount needed increasing after each level how can i save the money they made when they play the level so it doesnt reset back to zero and how do i make levels that unlock after paying so much in-game money?
 
You can use a variable to show if a level is open or not and a array to check the price to get to the next level.
If you want to be able to skip levels you would have to save unlocked levels in a array.
Code:
levels = 4
level_price[0] = 1000
level_price[1] = 3000
level_price[2] = 10000
level_price[3] = 25000
Then when you unlock a level you just do the following:
Note that activate_next_level() is just a made up function, you need to decide how this is triggered
Code:
if activate_next_level() {
    if money >= level_price[current_level] {
        money -= level_price[current_level]
        current_level += 1
    }
}
The easiest way to save variables is to use ini files:
Code:
// Load - When game start
ini_open("save.ini")
money = ini_write_real("game", "money", 0) // <- put start money here if it shouldn't be 0
current_level= ini_write_real("game", "current_level", 0)
ini_close()
Code:
// Save - When game end
ini_open("save.ini")
ini_write_real("game", "money", money)
ini_write_real("game", "current_level", current_level)
ini_close()
 
D

darakin

Guest
thank you very much last thing though where do i put the codes in? im very new to this level of coding sorry
 

spe

Member
The first code can go in the create event of a persistent game controller object.
The second code can go in the step event of a persistent game controller object, or in a mouse button pressed event for a button to buy the next level, or whatever. It just has to run when you want to buy the next level.
The third one would probably go in a mouse button pressed event for a 'load' button object, or just in the create event or game start event of the persistent game controller object if you want it to automatically load data right from the start.
The fourth one can be in the mouse pressed event for a 'save' button object, the game end event for your game controller object, or, if you want to do some kind of auto-save system, you could put it in a code that runs on a timer, runs whenever you pause the game, runs whenever you enter a new room, runs whenever you buy anything or do anything, et cetera. This is mostly up to you where you want to put it. Each location will have slightly different results.
 
Top