GDL
About building parametric objects with GDL.
SOLVED!

Multiple on/off switches via array for switching within UI script

GDL Enthusiast
Advocate

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

2 ACCEPTED SOLUTIONS

Accepted Solutions
Solution

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

 

AllanP_14-1737680505987.png

 

master

i added a push back of the parameter array back into the object parameters,

 

DIM Switch[6]
parameters  Switch = Switch

 

 

AllanP_15-1737680836080.png

 

this updates the size of parameters to correct array size

 

AllanP_16-1737680995614.png

 

automatic code created from archicad

 

AllanP_2-1737679374196.png

 

parameters

 

AllanP_12-1737679760779.png

 

interface

 

AllanP_13-1737679776025.png

 

click on preview button

 

AllanP_17-1737681061869.png

 

when open the object and change to the graphical interface window,

 

AllanP_10-1737679694758.png

 

or changing to the parameter script

 

AllanP_9-1737679681385.png

 

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.

I have been using ArchiCAD continually since ArchiCAD 4.5, 4.5.5, 5, 5.1, 6, 6.5, 7, 8, 8.1, 9, 10, 11, 12, 13, 15, 18, 21, 22, 25, now testing 28
Member of Architalk since 2003, but missed the migration to Graphisoft.

View solution in original post

Solution

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.

Jochen Suehlo . AC12-27 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de

View solution in original post

9 REPLIES 9
runxel
Hero

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
Lucas Becker | AC 27 on Mac | Graphisoft Insider Panelist | Author of Runxel's Archicad Wiki | Editor at SelfGDL | Developer of the GDL plugin for Sublime Text | My List of AC shortcomings & bugs | I Will Piledrive You If You Mention AI Again |

POSIWID – The Purpose Of a System Is What It Does /// «Furthermore, I consider that Carth... yearly releases must be destroyed»
Jochen Suehlo
Moderator

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

Jochen Suehlo . AC12-27 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de

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


Initalised Array.PNG

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


Parameter Array Vs Parameter.png

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,

One of the forum moderators.
Versions 6.5 to 27
i7-10700 @ 2.9Ghz, 32GB ram, GeForce RTX 2060 (6GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11
Solution

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

 

AllanP_14-1737680505987.png

 

master

i added a push back of the parameter array back into the object parameters,

 

DIM Switch[6]
parameters  Switch = Switch

 

 

AllanP_15-1737680836080.png

 

this updates the size of parameters to correct array size

 

AllanP_16-1737680995614.png

 

automatic code created from archicad

 

AllanP_2-1737679374196.png

 

parameters

 

AllanP_12-1737679760779.png

 

interface

 

AllanP_13-1737679776025.png

 

click on preview button

 

AllanP_17-1737681061869.png

 

when open the object and change to the graphical interface window,

 

AllanP_10-1737679694758.png

 

or changing to the parameter script

 

AllanP_9-1737679681385.png

 

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.

I have been using ArchiCAD continually since ArchiCAD 4.5, 4.5.5, 5, 5.1, 6, 6.5, 7, 8, 8.1, 9, 10, 11, 12, 13, 15, 18, 21, 22, 25, now testing 28
Member of Architalk since 2003, but missed the migration to Graphisoft.
Solution

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.

Jochen Suehlo . AC12-27 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de

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

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

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!