frames/seconds since function started?

Posted By: DJBMASTER

frames/seconds since function started? - 11/15/09 22:41

Hi, i'm wondering if there is a variable/function that gives you the frames or seconds past since a particular function was started.

Currently i'm using a local var and incrementing it each frame. I had a quick look through the manual but didn't find anything. Just asking in-case i may have missed it.

Thanks.
Posted By: Sepiantum

Re: frames/seconds since function started? - 11/16/09 03:48

If you can write a timer, now is a good time.
Posted By: Lukas

Re: frames/seconds since function started? - 11/16/09 13:59

Nope, there is no such variable or function.
Posted By: flits

Re: frames/seconds since function started? - 11/16/09 14:58

you got those functions only for the game itself

total_frames & total_ticks
Posted By: Germanunkol

Re: frames/seconds since function started? - 11/16/09 17:21

a better way than to increment that thing would, in my oppinion, be:
Code:
void theFunction()
{
  var startingTicks = total_ticks;
  var ticksPassedSinceStart;
  while(1)
  {
    //do something
    if(blah)
    {
      ticksPassedSinceStart = total_ticks - startingTicks;
    }
    wait(1);
  }
}


Posted By: DJBMASTER

Re: frames/seconds since function started? - 11/16/09 19:06

Hmmm, i'm trying to find the simplest solution that can accomodate for both frames and seconds. Here is my code so far...
Code:
var alarm_timer = 0;

while(1)
{
if(alarm_timer == 10) // after 10 frames have passed
{
  beep();
}
if(alarm_timer == 20) // after 20 frames have passed
{
  beep();
}

alarm_timer += time_step; // or time_frame?
wait(1);
}



But this is buggy, because it sometimes beeps and sometimes doesn't. I think i may be getting confused with time_step and time_frame. I'm not 100% sure what the difference is between them.
Posted By: Rei_Ayanami

Re: frames/seconds since function started? - 11/16/09 19:09

you can't be sure that if you add time_step, that it is exactly 10 or 20 because time_step is usually between 0.2 - 0.5 .

If you want frames, just add "1"
Posted By: DJBMASTER

Re: frames/seconds since function started? - 11/16/09 19:42

yeh ok, i guess i can sort it out for frames, by just doing a 'alarm_timer++;". What about for seconds? Can i just do a calculation on the same variable - alarm_timer? I want to stick to using the minimum amount of variables necessary.

Something to do with a division or multiplication by 16?

Thanks for your help.
Posted By: Lukas

Re: frames/seconds since function started? - 11/16/09 19:52

For seconds, add time_step/16 each frame.
Posted By: DJBMASTER

Re: frames/seconds since function started? - 11/16/09 20:01

Originally Posted By: Lukas
For seconds, add time_step/16 each frame.


Yeh i originally tried that but it doesn't always work when using it in a comparison statement...
Code:
var alarm_timer = 0;
while(1)
{
if(alarm_timer == 5) // after 5 seconds
{ 
beep();
}
alarm_timer += time_step / 16;
wait(1);
}



It seems to only work sometimes, and not 100% of the time.
Posted By: JibbSmart

Re: frames/seconds since function started? - 11/16/09 22:05

Change the comparison statement:
Code:
var alarm_timer = 0;
while(1)
{
if(alarm_timer >= 5) // after 5 seconds
{ 
beep();
}
alarm_timer += time_step / 16;
wait(1);
}


This way, it is still caught if it skips 5 altogether.

Jibb
Posted By: DJBMASTER

Re: frames/seconds since function started? - 11/16/09 23:05

Already thought of that. It will fire continuously after 5 seconds. Setting 'alarm_timer = 0;' will fix this but any comparison tests after this, will not be met.
Posted By: rojart

Re: frames/seconds since function started? - 11/17/09 00:10

Maybe this code helps:
Code:
#include <acknex.h>
#include <default.c>

function main()
{
	fps_max = 60;
	// repeat the beep sound after 5s every time, play with 300 and 60
	while(1)
	{
		if ((total_frames % 300) == 1) beep();	
		wait(1);
	}
}


Posted By: DJBMASTER

Re: frames/seconds since function started? - 11/17/09 00:14

Thanks, that is useful, but unfortunately i can't limit the fps. This whole thing is for more my application 3DGS Easy Scripter, so if i seem to reject ideas it's because i'm trying to build a generic framework that can fire events at certain frames/seconds elapsed since the start of an action.
Posted By: Dillinger

Re: frames/seconds since function started? - 11/17/09 00:32

"any comparison tests after this, will not be met"

Not quite sure what you mean by that.
I'm guessing yeah, if you need to still access either the timer or the flipped switch, you'll probably need to declare a whole 'nother variable. Hope that doesn't blow your memory budget.
Since I'm uncertain exactly what you want to do, here's the kitchen sink:

Code:
var alarm_timer = 0 ; 
var my_switch = 1 ; 
var my_counter = 0 ; 
var elapsed_time = 0 ; 

if ( my_switch == 1 ) elapsed_time = 0 ; 

while ( my_switch == 1  )
{
alarm_timer += time_step / 16 ;
elapsed_time = alarm_timer  ;  // copy time into display variable
if(alarm_timer >= 5) // after 5 seconds
{ 
beep();
alarm_timer = 0 ; // reset timer for next time
my_switch = 0 ; // turn off the alarm
my_counter++ ; // log how many times alarm has been triggered
}

wait(1);
}


Posted By: JibbSmart

Re: frames/seconds since function started? - 11/17/09 08:59

Originally Posted By: DJBMASTER
Already thought of that. It will fire continuously after 5 seconds. Setting 'alarm_timer = 0;' will fix this but any comparison tests after this, will not be met.
Will you need more than one of these functions running simultaneously? If so, you'll also want a way to stop the timer for each of those.

Jibb
Posted By: Locoweed

Re: frames/seconds since function started? - 11/18/09 05:39

Think this is closer to what you want.

Code:
var framesCheck = 0;
var secondsCheck = total_secs;

while(1)
{
if(framesCheck == 10) // after 10 frames have passed
{
  framesCheck = 0;
  beep();
}
if(total_secs >= (secondsCheck+5)) // after 5 seconds have passed
{
  secondsCheck = total_secs;
  beep();
}

framesCheck++;
wait(1);
}



Loco
Posted By: Locoweed

Re: frames/seconds since function started? - 11/18/09 05:53

Or actually after reading your original post.

Code:
var framesCheck = 0;
var secondsCheck = total_secs;
STRING* strFrames = "#30";
STRING* strSeconds = "#30";

while(1)
{
str_for_num(strFrames, frameCheck);
draw_text(strFrames,0,50,vector(250,0,0));
str_for_num(strSeconds, (total_secs-secondsCheck));
draw_text(strSeconds,0,70,vector(250,0,0));

framesCheck++;
wait(1);
}




Although the frames counter will rack up like you are pumping gas. wink

Loco
Posted By: DJBMASTER

Re: frames/seconds since function started? - 11/18/09 15:20

Originally Posted By: Locoweed
Think this is closer to what you want.

Code:
var framesCheck = 0;
var secondsCheck = total_secs;

while(1)
{
if(framesCheck == 10) // after 10 frames have passed
{
  framesCheck = 0;
  beep();
}
if(total_secs >= (secondsCheck+5)) // after 5 seconds have passed
{
  secondsCheck = total_secs;
  beep();
}

framesCheck++;
wait(1);
}



Loco


Almost, but it will beep every 5 seconds.

I may have to re-think my logic on this mechanism...
Posted By: rojart

Re: frames/seconds since function started? - 12/20/09 14:50

Originally Posted By: DJBMASTER
Thanks, that is useful, but unfortunately i can't limit the fps.

Hmm, I'm not really sure what your goal is, but I've no problem to limit the fps with this code:
Code:
//#include <acknex.h>
#include <default.c>

function my_limit_startup()
{
	fps_max = 60; int my_fps = 1;
	
	while(my_fps<10) // limit me to 10s
	{
		if ((total_frames %  60) == 1) my_fps++;
		if ((total_frames % 300) == 1) beep();   // repeat the beep sound after 5s
		wait(1);
	}
}


© 2024 lite-C Forums