Grasshopper

algorithmic modeling for Rhino

I want to make a counter - each time i`l push boolean toggle counter number will rise on one. And i need start number and reset option.

But I have no idea how to do this without adding more and more all the time boolean is true.

 

I want to use this counter for file name for each model export in local code.

any help will be great  

 

Now i made something like this:

 

Dim m As Boolean = C    

Dim cnt As Integer = 0
    If m = True    

 Then cnt = cnt + 1    

m = False  

 Else cnt = cnt  

End If  

A = cnt  

print(A)

 

But it is not working at all :-(

 

Views: 251

Replies to This Discussion

Hi Philipp,

 

it's not working because cnt is a local variable. It is declared inside your RunScript function and only exists while that function is active. If you want this variable to survive subsequent calls to the RunScript function you'll need to either make it a class-level variable or a static variable. To make it class level you'll need to declare it outside of any function blocks, meaning in the 'custom additional code' area below the RunScript function:

 

'<Custom additional code> 

Private m_count As Int32
'</Custom additional code>

 

Then you can use it inside RunScript and it won't get nuked:

 

If (C) Then

  m_count += 1

Else

  m_count = 0

End If

A = m_count

Print(m_count)

 

It is good practice to name class level variables different from local variables. I always prefix them with 'm_' in accordance with Hungarian Notation.

 

If you choose to make the variable static, then it can be declared inside the RunScript function, but it will retain its value when RunScript finishes. You use the 'Static' keyword instead of the 'Dim' keyword in this case (Note, C# does not have Static variables, this is a VB feature only).

 

Static s_count As Int32 = 0

If (C) Then

  s_count += 1

Else

  s_count = 0

End If

A = s_count

Print(s_count)

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

 

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service