Homebrew How can I put a "TIMER" so that later it executes an action

LoboNer

New Member
OP
Newbie
Joined
Jul 24, 2022
Messages
3
Trophies
0
Age
23
Location
Peru lmao
XP
40
Country
Peru
What I mean by "TIMER" is that after the time limit I gave to the timer it executes an action programmed in the code.

Something like I want to make a basic timer that counts as 6 seconds and after those 6 seconds pass it executes a code that I programmed like adding an image on screen etc.

Do you guys know how to do this? I am new to DS programming.

(I use libnds and the NightFox Lib)
 

mrparrot2

Well-Known Member
Member
Joined
Nov 29, 2021
Messages
110
Trophies
0
Age
29
Location
SP, Brazil
XP
588
Country
Brazil
Take a look at libnds/include/timers.h

The function `timerStart` last argument named `callback` receives a pointer to a function. This means you can do things like
```
void Add_Image_On_SeenBuff(void) {
// Do your things.
}
```
And then,

```
timerStart(arg1, arg2, arg3, Add_Image_On_SeenBuff)
```
I assume this works by issuing an interruption, so be careful about how long your code will take to run and possible race condititons by having a function interrupted in the middle of its execution.
 

plasturion

temporary hermit
Member
Joined
Aug 17, 2012
Messages
1,244
Trophies
2
Location
Tree
XP
3,561
Country
Poland
never used timer class, but it's very useful, other way you can define timer variable and place in the mainloop synchronized to vblank (60times per sec) something like...
int timer = 360;
If (timer && timer-- == 1){
// call function or do sort of things
}
or something similar in dozens different ways.
 

mrparrot2

Well-Known Member
Member
Joined
Nov 29, 2021
Messages
110
Trophies
0
Age
29
Location
SP, Brazil
XP
588
Country
Brazil
never used timer class, but it's very useful, other way you can define timer variable and place in the mainloop synchronized to vblank (60times per sec) something like...
int timer = 360;
If (timer && timer-- == 1){
// call function or do sort of things
}
or something similar in dozens different ways.
If you do not need your timer to be precise, then using a counter attached to vblank interrupt is a very good alternative. You can then use the hardware timer on tasks that actually require more precision.
 

Coto

-
Member
Joined
Jun 4, 2010
Messages
2,979
Trophies
2
XP
2,565
Country
Chile
Timer stuff you definitely want to use hardware for that since it's precise and wastes minimal CPU resources, here's an example implementing count in milliseconds and microseconds (DS is too slow for us,microseconds anyway):

https://bitbucket.org/Coto88/toolch...4403/src/common/hardware/timerTGDS.h#lines-54

https://bitbucket.org/Coto88/toolch...3/src/common/libhardware/timerTGDS.c#lines-27


Games use count in milliseconds (from timer or vblank counts) to make the game engine live at a certain pace.
I use it to synchronize the videoplayback to framerate in ToolchainGenericDS-multimediaplayer
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
  • a_username_that_is_cool @ a_username_that_is_cool:
    DDDS . Dual Developer Dedede System :)
    +1
  • BakerMan @ BakerMan:
    also happy birthday @Xdqwerty
    +1
  • BigOnYa @ BigOnYa:
    Is it me you're looking for?
  • MysticStarlight @ MysticStarlight:
    'Cause I wonder where you are
  • MysticStarlight @ MysticStarlight:
    wait I got the lyrics mixed up
    +1
  • BigOnYa @ BigOnYa:
    And I wonder what you do...Lol
  • BigOnYa @ BigOnYa:
    Its all good, I'm here to talk to you.
  • BigOnYa @ BigOnYa:
    Hello
  • BigOnYa @ BigOnYa:
    I've been playing fallout4 all day, I went into a elevator and it kept going, never stopped, waited 5 minutes, must of been a bug. How bout you, whatcha do today?
    +1
  • MysticStarlight @ MysticStarlight:
    I'm wondering why a stray tuxedo cat has a cloudy eye
  • MysticStarlight @ MysticStarlight:
    I've been playing TOTK all day also
    +1
  • MysticStarlight @ MysticStarlight:
    Oh you reminded me of those elevator nightmares
  • BigOnYa @ BigOnYa:
    Awesome game, fun how they force you to build, but you can build as you want
  • MysticStarlight @ MysticStarlight:
    It is super fun, I actually prefer it over BoTW
    +1
  • BigOnYa @ BigOnYa:
    The guy holding the sign always cracked me up, esp when you fail, and it falls
  • MysticStarlight @ MysticStarlight:
    Ah yes, that guy lol. I still need to find all his signs. The game has a lot to do though so I don't know what to finish first
  • MysticStarlight @ MysticStarlight:
    I'm 120 hours in and am still like 44.4% done
    +1
  • BigOnYa @ BigOnYa:
    Good times. Building is starting to get good around now for you. You making me want to make run thru again.
  • BigOnYa @ BigOnYa:
    Ok good chatting, enjoy your gaming, and avoid the lightning, or put your sword away. Good night.
  • K3Nv2 @ K3Nv2:
    All I gotta do is not spend $10 in the next 5 days and I'll break even this month lol
  • K3Nv2 @ K3Nv2:
    @BigOnYa, tell your wife not to forget her wallet
    +1
    K3Nv2 @ K3Nv2: https://youtu.be/YMAa6obDNZs?si=rymChLKeTdyS8nqk