Thursday - last edited Thursday by Laszlo Nagy
Hi,
I have some code with a lot of on/off switches, each with a values{2} command and associated parameters i.e.
!! Parameters Script
values{2} "Switch_1", 0, `Off`,
1, `On`
values{2} "Switch_2", 0, `Off`,
1, `On`
!Etc...
!! UI Script
UI_INFIELD{3} Switch_1, 100, 100, 20, 20, 2, "Switch_Image", 2, 2, 0, 0, 20, 20,
1,"", 0,
2,"", 1
UI_INFIELD{3} Switch_2, 100, 140, 20, 20, 2, "Switch_Image", 2, 2, 0, 0, 20, 20,
1,"", 0,
2,"", 1
!Etc...
I am trying to use an array to code these switches instead so that instead of a lot of switch parameters I can just have a single parameter array for all the switches. Is it possible?
My best attempt at the code so far:
!!Define Parameter Switch as an array.
!! Master Script
DIM Switch[]
Switch[1] = 0
Switch[2] = 0
Parameters Switch[1] = Switch[1]
Parameters Switch[2] = Switch[2]
!Etc.
!! UI Script
UI_INFIELD{3} Switch[1], 100, 100, 20, 20, 2, "Switch_Image", 2, 2, 0, 0, 20, 20,
1,"", 0,
2,"", 1
UI_INFIELD{3} Switch[2], 100, 140, 20, 20, 2, "Switch_Image", 2, 2, 0, 0, 20, 20,
1,"", 0,
2,"", 1
!Etc...
But alas it doesn't work or allow switching. My goal is to reduce the number of switch parameters to a single array parameter.
Many thanks, Matt
Solved! Go to Solution.
Friday - last edited yesterday by Laszlo Nagy
Hi,
i have tried to replicate the error in a new blank object.
i drew 2 lines, made them into an object, then copied your code in:
i repeated your code:
parameters
master
i added a push back of the parameter array back into the object parameters,
DIM Switch[6]
parameters Switch = Switch
this updates the size of parameters to correct array size
automatic code created from archicad
parameters
interface
click on preview button
when open the object and change to the graphical interface window,
or changing to the parameter script
as an array, the values are not seen in the parameters list unless you open the array
I hope this helps.
example shown was in archicad 27.
Friday
Hi Matt,
if you use VALUES{2} you must obligatory use an Integer Type of Parameter. Length is not allowed.
As Barry wrote you can use Boolean as well, but then you do not need VALUES{2} at all.
Please check the manual for the according UI_INFIELDS for Booleans.
Please do not declare an Array in the Master-Script, when you have defined it before in the Parameters Section.
Attached an example with ingteger and boolean parameters.
Thursday
Why do you need the Master script?
Do you not know how many switches there are because they are dynamic and generated on the fly?
That needs some extra logic.
Other than that you don't need to define it in the Master script. You'll just have to make a parameter. (You actually would get an error message if you try to re-define an array in the Master...).
Also a last tip: PARAMETERS
does not work on the level of a single index of an array.
If you have indeed a dynamic array you'll need to work with temporary arrays, and then at last do something like this
switch = temparray
parameters switch = switch
Thursday
My suggestion would be as follows:
Create an array parameter in the parameter field, e.g. ‘switch’.
You can enter the size, i.e. the number of array indices there manually, but you can also control this dynamically in the parameter script.
Let's assume you enter 6 fields (rows).
Then you write into the parameter script:
VALUES{2} ‘switch’, 0, ‘off’, 1, ‘on’
This command then applies to all sub-array parameters.
In the user interface, you work through the whole thing with a loop:
FOR i = 1 TO 6
UI_INFIELD{3} Switch[i], 100, 100 + (i - 1) * 25, 80, 20,
8, ‘’, 2, 2, 0, 0, 20, 20,
1, ‘off’, 0,
2, ‘on’, 1
NEXT i
Friday - last edited yesterday by Laszlo Nagy
Hi Jochen,
The code looks neat, I am struggling to get it to work, a "bad indexed value reference" is popping up in the check. I have attached an image of the initialised array with 6 rows, hopefully I have that right. Could you check this code if possible?
!!Master Script
DIM Switch[6]
!!Parameter Script
VALUES{2} "switch", 0, 'off', 1, 'on'
!!Interface Script
but I am struggling to get it to work. A "bad indexed value reference" is popping up in the check. I have attached an image of the initialized array with 6 rows; hopefully, I have that right. Could you check this code,
Many thanks, Jochen! From Matt
Friday - last edited yesterday by Laszlo Nagy
Hi Runxel,
Thank you for responding! In one of my objects I have some 40+ switch parameters with associated Values{2}. These are simple 0,1 state switches. Each time I create a new file for testing, I end up with a tedious process of copying and pasting the parameter names of the switches.
I have a lot of repeating code for the 40 switch parameters within the interface and parameters script. I thought it would be cool if I could use a for i=1 to 40 loop and reference array values instead.
Attached is an image of the two options:
When I call a parameter ui_infield{2} "switch_1", 110, 140, 100, 160, it works.
When I call a parameter array with ui_infield{2} switch[1], 110, 140, 100, 160, it returns an error.
Is it correct that one cannot use an array with ui_infield{2} or is my syntax incorrect?
Thank you so much for your help, from Matt
Friday
I am not sure about the array technicalities (I would need to play around later).
But wouldn't it be better to use a boolean parameter rather than a length?
Barry,
Friday - last edited yesterday by Laszlo Nagy
Hi,
i have tried to replicate the error in a new blank object.
i drew 2 lines, made them into an object, then copied your code in:
i repeated your code:
parameters
master
i added a push back of the parameter array back into the object parameters,
DIM Switch[6]
parameters Switch = Switch
this updates the size of parameters to correct array size
automatic code created from archicad
parameters
interface
click on preview button
when open the object and change to the graphical interface window,
or changing to the parameter script
as an array, the values are not seen in the parameters list unless you open the array
I hope this helps.
example shown was in archicad 27.
Friday
Hi Matt,
if you use VALUES{2} you must obligatory use an Integer Type of Parameter. Length is not allowed.
As Barry wrote you can use Boolean as well, but then you do not need VALUES{2} at all.
Please check the manual for the according UI_INFIELDS for Booleans.
Please do not declare an Array in the Master-Script, when you have defined it before in the Parameters Section.
Attached an example with ingteger and boolean parameters.
Saturday
Hi Jochen,
You are amazing!
I love seeing the code working, such joy. Thank you especially for the sample object, that helped so much, I can now code away.
Best regards and thank you again, from Matt
Saturday
Hi AllanP,
Thank you for taking the time to test the code and posting all the screen captures, super helpful for me and most appreciated!
Wishing you the best from Matt