• 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!

[Need Help] Setting a best time on title screen

D

deadant88

Guest
Hi all,

I have hunted around for a solution to this and tried a bunch of different approaches but nothing seems to work.

I am trying to get my game to display the time a player survived on the title screen. When the player hits enter on the "title_rm" the game starts, the timer starts, when the player dies, the player is returned to the "title_rm" I'd like to make it so the time survived in the previous round is then displayed on the the title screen.

Here is my code in the obj_player create:

Code:
global.timer = 0
Here is my code in the obj_player step:

Code:
global.timer = global.timer + 1/30;
I then created an "obj_title_timer" to draw the previous time achieved onto the title but this is where I get stuck trying to call up the timer from the previous round. Predictably because the "global.timer" is set to 0 it just shows 0.

Code:
draw_set_colour(c_red);
draw_text(300, 192, "best time: " + string(global.timer));
I hope that makes sense, I'd be grateful for any advice.

Cheers.
 
A

arirish

Guest
you need 2 variables. one for the current timer and one for the best time.

in the player step: if global.timer>global.besttime {global.besttime=global.timer}

Don't initialize those variables in your player object. Do it in a persistent control object.

Now, do you want the best time to save between plays? ie, if you exit the game and come back to it, the best time is still displayed? If so, you'll probably want to use an ini file.
 
D

deadant88

Guest
Ah this is immensely helpful. Thank you. I will try it as soon as I get home.

Yes a persistent best score that saves between games is on my roadmap, is incorporating a .ini file difficult?
 
D

deadant88

Guest
H
No, especially not if that's the only piece of information you want to record. You can find the necessary functions here: https://docs.yoyogames.com/source/dadiospice/002_reference/file handling/ini files/index.html
Thank you for your help so far.

So I've created two objects: obj_besttime and an obj_timer here is the create code for each

Code:
//obj_bestttime
global.besttime = 0;
if global.timer > global.besttime
{
    global.besttime = global.timer;
}
else
global.besttime = global.besttime;

//obj_timer
global.timer = 0;
Unfortunately when i try to initialize the game this is what I get:

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object obj_best_time:
global variable name 'timer' index (100006) not set before reading it.
at gml_Object_obj_best_time_Create_0 (line 3) - if global.timer > global.besttime
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_best_time_Create_0 (line 3)

I'm sure it is a simple solution to a newb mistake, but I appreciate your help. :)
 
A

arirish

Guest
Here. I've included some basic ini stuff too.

Your create event should look like this:
Code:
ini_open("besttime.ini"); //GM should create the file automatically if it doesn't exist
global.besttime = ini_read_real("times", "besttime", 0); //will default to 0
ini_close(); //you should always close files after reading from or writing to them
global.timer=0;
Then your Step event should look like this:
Code:
if global.timer > global.besttime
{
    global.besttime = global.timer; //sets besttime to current time - you don't need your 'else' case
}
Wherever your game ends, in your code, and you return to the main menu, put this first:
Code:
ini_open("besttime.ini"); //opens the file to write to
ini_write_real( "times", "besttime", global.besttime); //writes the best time to the file
ini_close(); //closes the file
global.timer=0; //resets timer to 0
 
D

deadant88

Guest
Hi thanks so much for this. I will try and work it into my game this evening. One question:

Should I create 1 object for timer (the ingame timer) and a 2nd object for best time (the time that interacts with the .ini file) and records the besttime onto game's main menu?

Thanks again for your help so far.
 
N

nicoltoons

Guest
In the create event of a persistent object, write this. scr_Loadfile.

scr_Loadfile should contain this as @arirish said.
ini_open("besttime.ini"); //GM should create the file automatically if it doesn't exist
global.besttime = ini_read_real("times", "besttime", 0); //will default to 0
global.prev_time = ini_read_real("Previous","previoustime",0) //previous score of the player
ini_close(); //you should always close files after reading from or writing to them


timer = 0 // create event of persistent object or any object in the game that is checking for when the player's time changes.

step event of this object.
if timer > global.besttime
{
global.prev_time = global.besttime //get the previous score before you equate the time to the best time.
global.besttime = timer

scr_Savefile() //
}
scr_Savefile() should contain this

ini_open("besttime.ini"); //opens the file to write to
ini_write_real( "times", "besttime", global.besttime); //writes the best time to the file
ini_write_real("Previous","previoustime", global.prev_time) // save the previous time
ini_close(); //closes the file


when the player is returned to the title screen and you need to draw the best time
create event of the object on the title screen / the persistent object( if you used one)
scr_Loadfile() so the game can get all the details of the times then you can draw like this

draw_text(32,32, "Previous Time: " +string(global.prev_time)
draw_text(64,32,"Best Time" + string(global.best_time)
 
Top