hi, you could check these out, the first one seems shorter but it loops and prevents other code from executing, the second code doesn't stop other code from executing so you could add other stuff within the "WHILE TRUE - END" loop
[shcode=cpp]{$CLEO .cs}
0000:
REPEAT
WAIT 0
UNTIL 0AFA: SAMP_IS_READY
// 32@ and 33@ variables are different from the 0@ - 31@ variables because they are timers. After you set 32@ or 33@ to some value they will automatically increase by 1 each milisecond
1@ = 10 // 1@ = amount of seconds for the timer
31@ = 191 // key
32@ = 0 //just initialising
WHILE TRUE
WAIT 0
if 0AB0: key_pressed 31@ // /
then
0085: 0@ = 32@ // (int)
0085: 2@ = 1@ // 2@ becomes a copy of 1@ (2@ will be manipulated, 1@ will be kept the same, that's the point of copying it)
2@ *= 1000 // 2@ seconds turns into miliseconds
005A: 0@ += 2@ // current time (0@) + amount of countdown time in miliseconds (2@)
while 0AB0: key_pressed 31@
wait 0
0085: 2@ = 0@
0062: 2@ -= 32@ // 2@ becomes amount of time that is left (in miliseconds)
2@ /= 1000
if 001B: 0 > 2@
then
0AD1: show_formatted_text_highpriority "The countdown just finished" time 2000 2@
wait 2000
break
else
0AD1: show_formatted_text_highpriority "%d" time 1000 2@
end
end
end
END[/shcode]
[shcode=cpp]
{$CLEO .cs}
0000:
REPEAT
WAIT 0
UNTIL 0AFA: SAMP_IS_READY
// 32@ and 33@ variables are different from the 0@ - 31@ variables because they are timers. After you set 32@ or 33@ to some value they will automatically increase by 1 each milisecond
1@ = 10 // 1@ = amount of seconds for the timer
3@ = 191 // key
32@ = 0 //just initialising
WHILE TRUE
WAIT 0
call @Timer 5 timingVariable 32@ amountOfSeconds 1@ booleanNeededForLogicControl 2@ key 3@ endingTimeVar 5@ _returnedbooleanNeededForLogicControl 2@ _returnedResult 4@ _returnedendingTimeVar 5@
if 4@ == 1
then
//timer has been cancelled by releasing the button
end
if 4@ == 2
then
//timer successfully finished the countdown
end
END
//functions
:Timer
if 0AB0: key_pressed 3@ // /
then
if 2@ == false
then
2@ = true
call @StartTimer 2 timingVariable 0@ amountOfSeconds 1@ _returnedEndingTime_ms 4@
//chatmsg "started timer" -1
end
else
2@ = false
ret 3 2@ 1 4@
end
if 2@ == true
then
call @ManageTimer 2 timingVariable 0@ endingTime_ms 4@ _isFinished 2@ _timeLeft 29@
//chatmsg "%d %d %d %d" -1 0@ 4@ 2@ 29@
if 2@ == true
then
0AD1: show_formatted_text_highpriority "%d" time 100 29@
else
0AD1: show_formatted_text_highpriority "The countdown just finished" time 2000
wait 2000
ret 3 2@ 2 4@
end
end
ret 3 2@ 0 4@
:StartTimer
1@ *= 1000 // 2@ seconds turns into miliseconds
005A: 0@ += 1@ // current time (0@) + amount of countdown time in miliseconds (2@)
ret 1 0@
:ManageTimer
0062: 1@ -= 0@
1@ -= 1
1@ /= 1000
1@ += 1
if 001B: 1 > 1@
then
ret 2 false 0
end
ret 2 true 1@[/shcode]