Help : Rand fuction

GPC2 script programming for Titan Two. Code examples, questions, requests.

Help : Rand fuction

Postby A1rookie » Sat Sep 15, 2018 5:54 pm

Hi, need help with the Rand function please.
Documentation says :
Code: Select all
fix32 rand();

"Generate a random decimal number between 0.0 and 1.0"
But I want to Generate a random decimal number from 1-10 to choose of 10 different combos by random. How to Do that?
Thanks
User avatar
A1rookie
Staff Sergeant
Staff Sergeant
 
Posts: 13
Joined: Mon Sep 10, 2018 5:26 pm

Re: Help : Rand fuction

Postby Scachi » Sat Sep 15, 2018 5:57 pm

This should help you:
Code: Select all
 
//  example: irand(40, 120);  <- random value between 40 and 120, including min and max value itself
#define irand(a,b) (((int)(rand() * (fix32)(b+1 - a))) + a)
 
init {
  printf("rand: %d", irand(1,10));
}
 
main {
 
}
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Help : Rand fuction

Postby A1rookie » Sat Sep 15, 2018 6:17 pm

Know it was Titan one but as I didn't See it in t2 Documentation I thought it's gone. Thank you
User avatar
A1rookie
Staff Sergeant
Staff Sergeant
 
Posts: 13
Joined: Mon Sep 10, 2018 5:26 pm

Re: Help : Rand fuction

Postby Scachi » Sat Sep 15, 2018 6:24 pm

irand itself is gone, not available on the T2.
The macro above, the line with # at the beginning, mimics the irand function of the T1.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Help : Rand fuction

Postby A1rookie » Sat Sep 15, 2018 7:15 pm

Ahh okay. Yes I see now.Thank you very much . Offtopic :I always wondered, if I use one Button to trigger a combo, and press Button again, while the executed combo is already und running, will the running combo be disturbed by the trigger, or will it finish like normal? If it would not wait to finish, how to Script on t2 to Block the trigger until the combo is finished?
Because there is no :
Code: Select all
! Combo_running 
like on t1
User avatar
A1rookie
Staff Sergeant
Staff Sergeant
 
Posts: 13
Joined: Mon Sep 10, 2018 5:26 pm

Re: Help : Rand fuction

Postby Scachi » Sat Sep 15, 2018 8:08 pm

The combo will only be started if it is not running already.

To check if a combo is active you can do
if (comboA)
or for testing if it is not running
if (!comboA)

Example: this will restart the combo after it has finished when you are still holding down R1/RB,
there won't be two instances of comboA active.
Code: Select all
bool last;
 
main {
  if ( get_val(BUTTON_4)) combo_run(comboA); // R1 / RB
  if (comboA != last) {
    last=comboA;
    if ( comboA ) printf("active");
    if ( !comboA ) printf("not active");
  }
}
 
combo comboA {
  printf("combo started");
  wait(0);
  set_val(BUTTON_9,100); // L3/LS
  wait(1000);
  wait(1000);
}
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Help : Rand fuction

Postby A1rookie » Thu Sep 20, 2018 5:35 pm

Perfect, everything is working as expected.thank you very much
User avatar
A1rookie
Staff Sergeant
Staff Sergeant
 
Posts: 13
Joined: Mon Sep 10, 2018 5:26 pm


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 72 guests