need help with simple counter function

Pixilang programming language
Post Reply
rototom
Posts: 24
Joined: Mon Mar 14, 2022 4:12 pm

need help with simple counter function

Post by rototom »

hi, i need help writing a simple counter function.
when i try to transform the following into a reusable function with two arguments, it simply does not work.
i have no idea why!?
anybody?

Code: Select all

set_pixel_size( WINDOW_XSIZE / 500 )
resize( get_screen(), WINDOW_XSIZE, WINDOW_YSIZE )

//_---------------
spd = 500 // speed in milliseconds
count = 0
str=""
brk = 0
//_-----_------
start_timer(0)

while( brk == 0 ) {  
// this works!
 ms = get_timer(0)
 if ms % spd == 0 { 
	count = count + 1
	num2str(str,count)
	clear()
	print(str,0,0)
	frame()
 }
//_-------------------

while( get_event() ) { if EVT[ EVT_TYPE ] == EVT_QUIT { brk = 1 } }
}

// this does NOT work!
fn counter($ti,$spd){ // args: timer, speed
$ms = get_timer($ti)
if $ms % $spd == 0
 {
 $count = $count + 1 
 ret($count)
 }
}
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: need help with simple counter function

Post by NightRadio »

1) $count is a local variable which is only available inside the counter() function. It has nothing to do with the global variable count
So you need: ret( count + 1 )

2) You also need the following instruction at the end of the counter() function:
ret( count )

3) $ms % $spd == 0 is a bad way to work with a counter, because you may lose some cycles if the device is not too fast.
In some cases the $ms values might be like this:
995
999
1002
1008
etc.
So your condition will always be false
rototom
Posts: 24
Joined: Mon Mar 14, 2022 4:12 pm

Re: need help with simple counter function

Post by rototom »

thanks for your answer @nightradio
the ms % spd == 0 thing is working fine on my devices, as long as i don't try to pack it into a function...

but i would like to use several counters at different speeds in my programs, that's why i would prefer a counter function.
also in a next step the counter should be able to count forwards/backwards and maybe within a given range.

what would you consider a good way for such a counter function in pixilang?
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: need help with simple counter function

Post by NightRadio »

You can try using multiple timers:

Code: Select all

text_y = -get_ysize(get_screen()) div 2

tnum = 2 //number of timers (from 1 to 16)
speeds = new( tnum, 1, INT )
speeds[ 0 ] = 500
speeds[ 1 ] = 1000
for( $tn = 0; $tn < tnum; $tn + 1 )
{
    start_timer( $tn )
}

while 1
{
    for( $tn = 0; $tn < tnum; $tn + 1 )
    {
        counter( $tn )
    }
    while get_event() { if EVT[ EVT_TYPE ] == EVT_QUIT { halt } }
    frame()
}

fn counter( $tn )
{
    $speed = speeds[ $tn ]
    $t = get_timer( $tn )
    if $t >= $speed
    {
        $ts = ""
        sprintf( $ts, "TICK %d\n", $tn )
        print( $ts, 0, text_y, WHITE, TOP ) text_y + 8
        start_timer( $tn )
    }
}
rototom
Posts: 24
Joined: Mon Mar 14, 2022 4:12 pm

Re: need help with simple counter function

Post by rototom »

thank you! :)
Post Reply