How to creat multiple variables by loop


#1

Hello,

I’m trying to creat multiple variables by loop. And I want to mix the variable name with number as this :

for i = 1 to 5 do (
(‘layer’+i+’_name’) = getINISetting renderinfoPath (’“layer’+i+’”’) “name”
)

But it’s not the good synthax (‘layer’+i+’_name’). How can I mix this kind of information ?

Thank you so much for your help


#2

You don’t need to.
Simply make nth_layer = getINISetting renderinfoPath (’“layer’+i+’”’) “name”
and do whatever you need with it
Or in case if you need to access its value later on in the script you can collect values into an array like this:

renderinfo = for i = 1 to 5 collect (getINISetting renderinfoPath ("layer" + i as string + "name"))
...
format "%\n" renderinfo[ 1 ]
format "%\n" renderinfo[ 3 ]

#3

Thank you very much it works great!

there was just the “)” to move :
renderinfo = for i = 1 to 5 collect (getINISetting renderinfoPath (“layer” + i as string) + “name”)


#4

Hello,
I come back with my question.

I want to creat a UI with text and checkbox. How can I make it with loop and not write it manually ? Like defining number of view, of layer, of sun and do it by loop to creat the appropriate number of checkbox. Is it possible ?

edittext layer1 width:250 height:15 offset:[0,0] align: #left type:#string text:layer_name[1]
checkbox layer1_view1 offset:[300,-18] checked:(visibility_l1[1] as BooleanClass)
checkbox layer1_view2 offset:[450,-18] checked:(visibility_l1[2] as BooleanClass)
checkbox layer1_view3 offset:[600,-18] checked:(visibility_l1[3] as BooleanClass)
checkbox layer1_view4 offset:[750,-18] checked:(visibility_l1[4] as BooleanClass)
edittext layer2 width:250 height:15 offset:[0,0] align: #left type:#string text:layer_name[2]
checkbox layer2_view1 offset:[300,-18] checked:(visibility_l2[1] as BooleanClass)
checkbox layer2_view2 offset:[450,-18] checked:(visibility_l2[2] as BooleanClass)
checkbox layer2_view3 offset:[600,-18] checked:(visibility_l2[3] as BooleanClass)
checkbox layer2_view4 offset:[750,-18] checked:(visibility_l2[4] as BooleanClass)
edittext layer3 width:250 height:15 offset:[0,0] align: #left type:#string text:layer_name[3]
checkbox layer3_view1 offset:[300,-18] checked:(visibility_l3[1] as BooleanClass)
checkbox layer3_view2 offset:[450,-18] checked:(visibility_l3[2] as BooleanClass)
checkbox layer3_view3 offset:[600,-18] checked:(visibility_l3[3] as BooleanClass)
checkbox layer3_view4 offset:[750,-18] checked:(visibility_l3[4] as BooleanClass)
edittext layer4 width:250 height:15 offset:[0,0] align: #left type:#string text:layer_name[4]
checkbox layer4_view1 offset:[300,-18] checked:(visibility_l4[1] as BooleanClass)
checkbox layer4_view2 offset:[450,-18] checked:(visibility_l4[2] as BooleanClass)
checkbox layer4_view3 offset:[600,-18] checked:(visibility_l4[3] as BooleanClass)
checkbox layer4_view4 offset:[750,-18] checked:(visibility_l4[4] as BooleanClass)
edittext layer5 width:250 height:15 offset:[0,0] align: #left type:#string text:layer_name[5]
checkbox layer5_view1 offset:[300,-18] checked:(visibility_l5[1] as BooleanClass)
checkbox layer5_view2 offset:[450,-18] checked:(visibility_l5[2] as BooleanClass)
checkbox layer5_view3 offset:[600,-18] checked:(visibility_l5[3] as BooleanClass)
checkbox layer5_view4 offset:[750,-18] checked:(visibility_l5[4] as BooleanClass)


#5

if you find it hard to grasp, take a look at RolloutCreator in mxs reference



fn MakeRollout rows:5 width:300 =
(
	local header = "	
	rollout X \"RolloutName\" width:{0}
	(
		local func1 = if func1 != undefined then func1
		local func2 = if func2 != undefined then func2
		local func3 = if func3 != undefined then func3
	"
	
	local template = "	
	button btn_{0} \"button {0}\" width:150 align:#center tooltip:\"{1}\" across:2
	checkbox cb_{0} \"\" offset:[-10,4]
	"
	
	local events = "
	on btn_{0} pressed       do func1 {0}
	on btn_{0} rightclick    do func2 {0}
	on cb_{0}  changed state do func3 {0} state
	"
	
	local footer = ")\n"
	
	
	fn _format template items =
	(
		if iskindof items array then
		(
			(dotNetClass "system.string").format template (dotNet.ValueToDotNetObject items (dotnetclass "system.object[]" ))
		)
		else
		(
			(dotNetClass "system.string").format template items
		)
	)
	
	local ss = stringstream ""
	
	format "%" (_format header width) to:ss
	
	for i = 1 to rows do
	(
		format "%\n" (_format template #( i ,"tooltip #" + i as string)) to:ss
	)
	
	for i = 1 to rows do
	(
		format "%\n" (_format events #(i)) to:ss
	)
		
	format "%" footer to:ss
	
	format "source: '%'\n" (ss as string)
	
	local _rollout = execute (ss as string)
	
	fn OnClick row_index =
	(
		format "click btn# %\n" row_index
	)
	
	fn OnRightClick row_index =
	(
		format "rightclick btn# %\n" row_index
	)
	
	fn OnChecked row_index state = 
	(
		format "Row:% Checked:%\n" row_index state
	)
	
	_rollout.func1 = OnClick
	_rollout.func2 = OnRightClick
	_rollout.func3 = OnChecked
	
	_rollout
)

createDialog (MakeRollout rows:3 width:450) pos:[100,100]

#6

Thank you, it look like a great direction. I will try to understand your code.


#7

Hi, would it be possible to create an ADD button to be able to automatically create a new layer with button and checkbox? I would need a mapbutton + ID checkbox, but I need a lot of layers.


#8

maxsscript doesn’t allow that. You’ll have to destroy and recreate new rollout with arbitrary count of controls. check the above example


#9

use WPF.