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

GML Stopwatch final result

Atbit

Member
(In advance I apologize for mistakes in the text) I have a simple stopwatch for game, which is visible in game. The main problem is, that I can't understand how numbers of sec,min,hrs should be shown in another room with results after game completed. There are two stopwatch objects: one for in game display, and second for the final result when game is completed. 50% of code is "draw_text", don't be afraid of amount. And yes, I am sorry for tabulation, it's awful. I have the main timer in game room:

Create event:
Code:
count = true;
rs = room_speed;
s = 0;
m = 0;
h = 0;
Draw event:
Code:
var cx = camera_get_view_x(view_camera[0]);
var cy = camera_get_view_y(view_camera[0]);
var cw = camera_get_view_width(view_camera[0]);

//Calculating
if (count)
{
    s += 1;
    s %= (rs*60);
    if (s==0)
    m += 1;

    if (m > 59) {
        m = 0;
        h += 1;
    }
}

//Draw1
draw_set_font(munro_fnt);
draw_set_color(make_color_rgb(0,135,172));
draw_set_halign(fa_left);

if (m < 10)
{
if (s < ( 10*rs ))
{
    draw_text(cx+cw/1.19,cy+27,string("0") + string (h) + string (":0") + string (m) + string (":0") + string (s/rs))
}else{
   
    draw_text(cx+cw/1.19,cy+27,string("0") + string (h) + string (":0") + string (m) + string (":") + string (s/rs))
    }
   
}else{
   
    if (s < (10 * rs))
    {
    draw_text(cx+cw/1.19,cy+27,string("0") + string (h) + string (":") + string (m) + string (":0") + string (s/rs))
    }else{
        draw_text(cx+cw/1.19,cy+27,string("0") + string (h) + string (":") + string (m) + string (":") + string (s/rs))
    }
}
And have another one with "final result":

Draw event:
Code:
var cx = camera_get_view_x(view_camera[0]);
var cy = camera_get_view_y(view_camera[0]);
var cw = camera_get_view_width(view_camera[0]);

var rs = global.resulte;
var s = global.resulte;
var m = global.resulte;
var h = global.resulte;

//Draw1
draw_set_font(munro_fnt);
draw_set_color(make_color_rgb(0,135,172));
draw_set_halign(fa_left);

if (m < 10)
{
if (s < ( 10*rs ))
{
    draw_text(cx+cw/1.19,cy+27,string("0") + string (h) + string (":0") + string (m) + string (":0") + string (s/rs))
}else{
   
    draw_text(cx+cw/1.19,cy+27,string("0") + string (h) + string (":0") + string (m) + string (":") + string (s/rs))
    }
   
}else{
   
    if (s < (10 * rs))
    {
    draw_text(cx+cw/1.19,cy+27,string("0") + string (h) + string (":") + string (m) + string (":0") + string (s/rs))
    }else{
        draw_text(cx+cw/1.19,cy+27,string("0") + string (h) + string (":") + string (m) + string (":") + string (s/rs))
    }
}
And in collision event of the player (after collision with enemy, timer should stops and the room changes to another room with the final score):

Collision with player event:
Code:
room_goto(end_menu);
var count = global.resulte;

if (count)
{count=false;}
else
{count=true;}
So, if the game ends, the result is not showing. Instead of time, timer displays on the screen "0:00:NaN"
I think the problem is in global.resulte or with the moment between when variables stopping and displaying.
Help, please! Brain does not work a little bit. :D
 

CloseRange

Member
So a few things:
Code:
var count = global.resulte;

if (count)
{count=false;}
else
{count=true;}
This will NOT break your code however for future:
Code:
var count = !global.resulte
using ! (aka 'not') will reverse the statement.

ok for the problem:
Code:
var rs = global.resulte;
var s = global.resulte;
var m = global.resulte;
var h = global.resulte;
global.resulte = 0 at this point this i can 100% guarantee.
i also am confused as hell why you do this.
rs, s, m, and h are ALWAYS the exact same because they are always refrencing the same variable.
so just know they are all the same, and they are all 0.
Code:
string("0") + string (h) + string (":0") + string (m) + string (":0") + string (s/rs)
that's where you are drawing the timer.
first you have a... string("0") btw you can just say "0" you don't need the string part because quotations means it's a string by default.
then you say string(h) and that's fine but h is 0 so it becomes
"00"
then string(":0") + string(m) and m is 0. Now it's drawing:
"00:00"
then you add string(":0") again and finally string(s/rs) HOWEVER like i said rs is 0. so is s. you can't devide 0 by 0. that's why it says NaN because 0/0 is NaN (btw NaN stands for not a number)

Anyhoo. To talk about the fix now.
am sorry for tabulation
tabulation is putting data into a table? I'm not sure how you think you're doing that. The only bad thing you've done is made 2 objects with their own independent timer variables.
the main timer has it's own variables:
Code:
s = 0;
m = 0;
h = 0;
and this main object does everything from drawing to incrementing these variables (and ONLY these variables)
however the final timer object does not have access to the main timer's variables.
Instead you take this global.resulte that always equals 0 and make temporary timer variables all set to those variables.

The hard fix would be to have you go back, make the s m and h varialbes global. Or make this all one persistent object. That way you learn for next time.
But part of me says that I'll give you the quick and dirty fix because you read all of my rant.

go to collision with player:
Code:
room_goto(end_menu);

global.sec_ = obj_timer_main.s;
global.min_= obj_timer_main.m;
global.hou_= obj_timer_main.h;
global.rs_ = obj_timer_main.rs;
go to the final timer draw where you declared those s h m and rs:
Code:
var rs = global.rs_;
var s = global.sec_;
var m = global.min_;
var h = global.hou_;
and there you have it. It stores your variables in global scope, allowing them to be accessed in the final screen.
of course you need to change the obj_timer_main to the actual name of the object.
 

Atbit

Member
Thank you so much for your explanation and sorry for exploded brain.
I promise, no more stupid mistakes like that.
 
Top