Page 1 of 1

time switch?

PostPosted: Wed Jun 13, 2018 9:36 pm
by alencroat
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.

Re: time switch?

PostPosted: Thu Jun 14, 2018 12:53 am
by Sillyasskid
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);
}

Re: time switch?

PostPosted: Thu Jun 14, 2018 5:13 am
by Scachi
++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();

Re: time switch?

PostPosted: Thu Jun 14, 2018 10:16 pm
by alencroat
interesting let me give it a try thanks boys

Re: time switch?

PostPosted: Thu Jun 14, 2018 10:44 pm
by alencroat
works good, but can you explain how to track it, I added t+=elapsed_time(); to the code but I see nothing. thanks again!

Re: time switch?

PostPosted: Fri Jun 15, 2018 4:56 am
by Sillyasskid
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.

Re: time switch?

PostPosted: Fri Jun 15, 2018 7:36 am
by Scachi
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.

Re: time switch?

PostPosted: Fri Jun 15, 2018 4:23 pm
by Sillyasskid
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().