# Question About Script Tabs [solved]



## Claud9 (Apr 21, 2017)

Hello,
I have an instrument with two script tabs (main engine - effect engine).
I have a button in the main engine to randomize volume, pan, is it possible with a script to have that button in the main engine tab to randomize also the effects in the effect engine tab?
Thanks in advance for any suggestion!


----------



## d.healey (Apr 21, 2017)

Yes, you'll need to use PGS keys


----------



## Claud9 (May 1, 2017)

d.healey said:


> Yes, you'll need to use PGS keys


Thanks!


----------



## Claud9 (May 1, 2017)

d.healey said:


> Yes, you'll need to use PGS keys


Hi David,
I have successfully used PGS keys and it works.
But I have made the same thing in an instrument with 3 script tabs and it doesn't work properly.
I have Main, Effects, Motion. I want that when I click the Random button in Main it randomize also the other two tabs.
It looks like one PGS key create problems to the other and vice-versa, infact I have tried to disable one of the Keys and the other works (both keys works perfectly alone if the other is disabled), as soon as I enable the other one it wont work as it should (sometimes randomize , sometimes not). Any idea what could be the problem?
Thanks in advance for any help!


----------



## EvilDragon (May 1, 2017)

All you need is one PGS key that will trigger the random functionality, then randomize in other two tabs.


----------



## Claud9 (May 2, 2017)

EvilDragon said:


> All you need is one PGS key that will trigger the random functionality, then randomize in other two tabs.


Hi ED thanks, I tried but I think the problem is that my situation is a little more complicated.
In the Main Engine Tab I have 3 buttons: Random, Effect, Motion.
The Random button always randomizes the Main Engine. The Effect Engine Tab and Motion Engine Tab are randomized only if the Effect and Motion buttons in the Main Engine tab are in the "on" position.
I have done this with an if statement in the Main Engine script

```
on ui_control ($randomize)
if ($effect=1)
pgs_set_key_val(FIRST_KEY,0,1)
end if
if ($motion=1)
pgs_set_key_val(SECOND_KEY,0,1)
end if
end on
```

But this solution works perfectly only with one Key active, as soon as I enable both they don't work properly.
I have also tried with only a Key (with two elements in it) but It's the same the problem is still there.
Any idea why? Thanks in advance for any suggestion!


----------



## EvilDragon (May 2, 2017)

You're doing it wrong.

Have one key for just the state of randomize button, then another two keys for effect and motion. Always update the state of effect and motion PGS keys (in Effect and Motion UI control callbacks). Then in each slot you can query against all the values and do what you want in each slot.


----------



## Claud9 (Feb 7, 2018)

EvilDragon said:


> You're doing it wrong.
> 
> Have one key for just the state of randomize button, then another two keys for effect and motion. Always update the state of effect and motion PGS keys (in Effect and Motion UI control callbacks). Then in each slot you can query against all the values and do what you want in each slot.


Sorry I was thinking to have it solved but I was wrong.
Any help much appreciated thanks.

I have this code in the main engine:


```
on init
declare ui_button $randomize
declare ui_button $effect
declare ui_button $motion
pgs_create_key(FIRST_KEY, 1)
pgs_create_key(SECOND_KEY, 1)
pgs_create_key(THIRD_KEY, 1)
end on


on ui_control ($randomize)
pgs_set_key_val(FIRST_KEY,0,1)

end on

on ui_control ($effect)
pgs_set_key_val(SECOD_KEY,0,1)

end on

on ui_control ($motion)
pgs_set_key_val(THIRD_KEY,0,1)
 
end on
```

This code in the effect engine:


```
on init
 
declare ui_slider $test (0,1000000)
end on


on pgs_changed
if(pgs_key_exists(SECOND_KEY))
 
 
 
$test := random(0,1000000)
 end if
 
end on
```

This code in the motion engine:


```
on init
 
declare ui_slider $test2 (0,1000000)
end on


on pgs_changed
if(pgs_key_exists(THIRD_KEY))
 
 
 
$test2 := random(0,1000000)
 
 end if
end on
```


----------



## EvilDragon (Feb 7, 2018)

This is not good because every button press you do will execute things across all script slots. You need to do some more checking apart from just pgs_key_exists() in order to ensure it's meant to be for that particular script slot. Example:


```
on pgs_changed
    if (pgs_key_exists(SECOND_KEY) and pgs_get_key_val(SECOND_KEY, 0) = 1)
        <do randomization>
        pgs_set_key_val(SECOND_KEY, 0, 0)
    end if
end on
```

Likewise in the third slot. This will make sure that ONLY when you change SECOND_KEY to 1 in the main script, randomization will be executed in the relevant script slot.


----------



## Claud9 (Feb 7, 2018)

EvilDragon said:


> This is not good because every button press you do will execute things across all script slots. You need to do some more checking apart from just pgs_key_exists() in order to ensure it's meant to be for that particular script slot. Example:
> 
> 
> ```
> ...


Great now is more clear and when I push for example the "effect" button it randomize only the effect engine without affecting the motion one. and this is a big step forward.
But what I want to do is that The Random button always randomizes the Main Engine. The Effect Engine Tab and Motion Engine Tab are randomized only if the Effect and Motion buttons in the Main Engine tab are in the "on" position. This part is not clear...


----------



## Claud9 (Feb 7, 2018)

EvilDragon said:


> This is not good because every button press you do will execute things across all script slots. You need to do some more checking apart from just pgs_key_exists() in order to ensure it's meant to be for that particular script slot. Example:
> 
> 
> ```
> ...


I think is working now I don't know if Its the best way but It seem to work but I have to test it in my main instrument... :

main engine:


```
on init
declare ui_button $randomize
declare ui_button $effect
declare ui_button $motion
pgs_create_key(FIRST_KEY, 1)
pgs_create_key(SECOND_KEY, 1)
pgs_create_key(THIRD_KEY, 1)
end on


on ui_control ($randomize)
if ($effect=1)
pgs_set_key_val(SECOND_KEY,0,1)
end if
if ($motion=1)
pgs_set_key_val(THIRD_KEY,0,1)
end if
end on
```

effect engine:


```
on init
 
declare ui_slider $test (0,1000000)
end on


on pgs_changed
if (pgs_key_exists(SECOND_KEY) and pgs_get_key_val(SECOND_KEY, 0) = 1)
 
 
 
$test := random(0,1000000)

pgs_set_key_val(SECOND_KEY, 0, 0)
 end if
 
end on
```

motion engine:


```
on init
 
declare ui_slider $test2 (0,1000000)
end on


on pgs_changed
if (pgs_key_exists(THIRD_KEY) and pgs_get_key_val(THIRD_KEY, 0) = 1)
 
 
 
$test2 := random(0,1000000)

pgs_set_key_val(THIRD_KEY, 0, 0)
 end if
 
end on
```


----------

