Notifications
Clear all

[Closed] loop not working with dialog

the below script is working good in selection of loop from first object in array to the last object in array, but why its not working with the press button…,

it should be like., when i press button, it should then select the next selection from the array of objects…

rollout test_buttons “Testing Buttons”
(
button theButton “Press me!”
on theButton pressed do
(
for i = 0 to 5 do
(
myobjects = $* as array
select myobjects [i+1]
)
)
)
createDialog test_buttons 150 50

with a click its looping selection to all objects, but it should be like every click to go next objects

3 Replies

The script you wrote does not express what you wrote in your post
When you press the button, it grabs the objects, then loops from 1 to 6 and selects each one of the objects, most probably leaving the last one selected. (Haven’t run it for real, just in my head).

What you really want to do is not a loop, but a local variable in the rollout which keeps track of what you selected the last time you pressed the button. Each time you press the button, you would increment it by one, and when it reaches the desired limit, you would reset it back to repeat again.

A loop on the other hand is a construct which runs instantly within the body of the the button’s event handler, and there is nothing in there that tells it to advance one object at a time, it always does them all…

Of course, there are other potential problems with your code, like what would happens if there are less than 6 objects in the scene? Why does the loop go from 0 to 5 and not from 1 to 6?

Anyway, here is a possible implementation:

rollout test_buttons "Testing Buttons"
(
	local counter = 0 --init. the counter
	button theButton "Press me!"
	on theButton pressed do
	(
		myobjects = $* as array
		if myobjects.count > 0 do --make sure there are any
		(
			counter += 1 --increase the counter
			if counter > myobjects.count do 
				counter = 1 --exceeded the object count? then start again
			select myobjects[counter] --select the next object
		)
	)
)
createDialog test_buttons 150 50

Thank you very much, Bobo.

sorry for my unclear questioning , but you have got what exactly i am expecting and need to function…

sorry for my poor scripting too, i am a newbie for scripting.

for the same…,

can i make a split with function, “fn”

rollout test_buttons “Testing Buttons”
(
local counter = 0 –init. the counter
button theButton “Press me!”
on theButton pressed do run
)
createDialog test_buttons 150 50

fn run =
(
myobjects = $cloth as array
if myobjects.count > 0 do –make sure there are any
(
counter += 1 –increase the counter
if counter > myobjects.count do
counter = 1 –exceeded the object count? then start again
select myobjects[counter] –select the next object
)
)