Notifications
Clear all

[Closed] Weird result

I’m stuck


on growBtn pressed do
	(
		i = 0
		log.Caption = "Loop starting..."
		total =  selection.count
		p = 0
		for s in selection do 
		(
			i = i+1
			obj = instance pickBtn.object
			theMod = SpacePathDeform()
			thePath = getnodebyname selection[i].name
			addModifier obj theMod
			theMod.path = thePath
			obj.transform = thePath.transform
			p = i / total * 100;
			progressBar.value = p;
			lastObjects[i] = obj.name
			obj.parent = thePath
			--append lastObjects obj
			log.Caption = "Iteration: "+i as String+" done..."
		)
		theObj = pickBtn.object
		theObj.renderable = false
		theObj.xray = true
		log.Caption = "Done! Picked is not Renderable now!"
	)

p stays at 0 until the loop is finished. Thought if I put i or total into the log output – those values are changing.

I think it is something really simple, but I can’t find the reason.

Thanks!

4 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

look at your code again… you need the variable i to be float just only to calculate right value for a progress bar. in the text expression “Iteration: “+i as String+” done…” where a float doesn’t make sense…
so you shouldn’t make the i float. you just have to write correct expression for the progressbar value…
you have:


 i = 1
 count = 2
 p = i / total * 100
 

this expression returns an integer because all arguments are integers.
you can make any argument a float and it changes the return value to float.
i – is innumerate index – makes sense to stay integer
count – good to be number as well
so … 100 is your client
but if change to:


 i/count*100.0
 

it will make a float, but it will be wrong result because int/int gives int. so… just change the order of your arguments:


 i*100.0/count
 -- or 100.0*i/count
 

You are dividing an integer by a biger integer ( i / total). As a result you get 0.
Try this:

total =  selection.count as float

or

i = i+1.0
1 Reply
(@nixellion)
Joined: 11 months ago

Posts: 0

That worked, thanks! Never knew that you can’t divide int by int.

A useful trick is to times the first number by 1.0, this is useful when you’re dealing with variables and don’t know if you’re getting floats or integers.

5 / 2 = 2
2 / 5 = 0
1.0 * 5 / 2 = 2.5
1.0 * 2 / 5 = 0.4