time switch?

GPC script programming for Titan Two device. Code examples, how to, questions, requests.

Moderators: The_Rabid_Taco, pablosscripts, bonefisher

time switch?

Postby alencroat » Wed Jun 13, 2018 9:36 pm

Hey is there a way to make a time based switch. I'll explain. for example if I use right DPAD to turn on a switch, and the switch stay on if I keep using or clicking button for example Left Trigger. And if I dont use the Left Trigger with in 5 seconds the switch turns of automatically. I needed that script plenty of times as it would be useful for lot of things.
User avatar
alencroat
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 192
Joined: Sun Oct 15, 2017 5:58 pm

Re: time switch?

Postby Sillyasskid » Thu Jun 14, 2018 12:53 am

Code: Select all
int Switch;
uint32 t;
main {
 
    if(event_active(BUTTON_10) & !Switch || (t > 5000)){
            t = 0;
            Switch = !Switch;
        }
 
    if (Switch){
        t+=elapsed_time();
        if (event_active(BUTTON_8)) { t = 0; }
 
        // code while switch is active
        ...
        combo_run(example);
    }
}
 
combo example {
    set_val(20,100);
}
Last edited by Sillyasskid on Fri Jun 15, 2018 4:58 am, edited 1 time in total.
User avatar
Sillyasskid
Lieutenant
Lieutenant
 
Posts: 307
Joined: Sat May 14, 2016 3:07 am

Re: time switch?

Postby Scachi » Thu Jun 14, 2018 5:13 am

++t will be a value increasing at some variating speed (1ms and faster).
If you want it to track time passed (milli-seconds) you have to use
t+=elapsed_time();
User avatar
Scachi
Captain
Captain
 
Posts: 627
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: time switch?

Postby alencroat » Thu Jun 14, 2018 10:16 pm

interesting let me give it a try thanks boys
User avatar
alencroat
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 192
Joined: Sun Oct 15, 2017 5:58 pm

Re: time switch?

Postby alencroat » Thu Jun 14, 2018 10:44 pm

works good, but can you explain how to track it, I added t+=elapsed_time(); to the code but I see nothing. thanks again!
User avatar
alencroat
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 192
Joined: Sun Oct 15, 2017 5:58 pm

Re: time switch?

Postby Sillyasskid » Fri Jun 15, 2018 4:56 am

Scachi wrote:++t will be a value increasing at some variating speed (1ms and faster).
If you want it to track time passed (milli-seconds) you have to use
t+=elapsed_time();


++t will increase every time the main{ } block is called.
Which is 1000 times per seconds.
And there is 1000ms in 1 second.

I'm not saying your wrong though, it was my silly mistake, ideally yes using elapsed_time() is the correct way to go for the most accurate results, I forgot to change that part of the script when I submitted it

Usually when I am testing a script, and I want to implement a timer, I tend to go with ++t, because I'm just testing the timer aspect in general, and not the actual timer value. (I'm lazy) I know that ++t will yield a faster outcome than elapsed_time().

Which is expected because the input controller will generate additional main { } block calls. With every input that gets sent to the titan.

Although, that being said, theoretically as long as the connected input controller is not actually active. ++t and t+=elapsed_time() should produce the exact same results.
User avatar
Sillyasskid
Lieutenant
Lieutenant
 
Posts: 307
Joined: Sat May 14, 2016 3:07 am

Re: time switch?

Postby Scachi » Fri Jun 15, 2018 7:36 am

I didn't know I have to use elapsed_time() a few scripts ago. I tried to get a timer working to test something against the time_active function and used ++t. It did take some time before I recognized the cause of my problem.
For me there was a huge difference as it looks like a connected and actively used ds4 controller can result in a much faster rate of main block calls than 1ms.
User avatar
Scachi
Captain
Captain
 
Posts: 627
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: time switch?

Postby Sillyasskid » Fri Jun 15, 2018 4:23 pm

Scachi wrote:I didn't know I have to use elapsed_time() a few scripts ago. I tried to get a timer working to test something against the time_active function and used ++t. It did take some time before I recognized the cause of my problem.
For me there was a huge difference as it looks like a connected and actively used ds4 controller can result in a much faster rate of main block calls than 1ms.

I'm sure this is due to the gyro values on the PS4 controller.

Every time the titan 2 polls the controller, it will generate an additional main block call if any of the inputs from the controller have actually updated since the last poll interval.

So if the gyros were updating non stop while testing. you would have an additional ~250(max) main block calls. Making the ++t update 1.25 times faster than t+=elapsed_time().
User avatar
Sillyasskid
Lieutenant
Lieutenant
 
Posts: 307
Joined: Sat May 14, 2016 3:07 am


Return to GPC Script Programming (Titan Two)

Who is online

Users browsing this forum: No registered users and 21 guests