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
587
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,242
Trophies
2
Location
Tree
XP
3,553
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
587
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
    AncientBoi @ AncientBoi: And I'll get off my butt n make ME some lunch :D