Grasshopper

algorithmic modeling for Rhino

Hey Everyone,


i've been trying to do something very simple but I keep running into the wall. I have several buttons, and I want to create an array of integers whose length is equal to the number of buttons. If a button is pressed for more than a certain amount of time, the corresponding integer should increase by 1.

I'm new to arrays in VB - here's my problem:

When I declare the array I need to be able to access the list length of the input "indices" So I placed it within Private Sub Runscript. But then when I try to increment the integers it briefly increments but then resets everything back to 0 (i guess because it's constantly re-diming the array). I tried Dim-ing the array outside of Runscript but then the list length of "indices" isn't accessible. 

So how can I declare and initialize the array only once while still having access to the list length of an input?

The code is below.

Private Sub RunScript(ByVal current As Object, ByVal selTime As Integer, ByVal indices As List(Of Integer), ByRef A As Object, ByRef B As Object, ByRef C As Object, ByRef D As Object)

delay = selTime
Dim boolList(indices.Count-1) As Integer

If (saveCur = True) Then
curButton = current
saveCur = False
curCount = 0
End If

If (curButton <> current) Then
curButton = current
saveCur = True
curCount = 0
Else
curCount += 1
End If

If (curCount > selTime) Then
itemVar = boolList(current - 1)
boolList(current - 1) = itemVar + 1
curCount = 0
End If

A = boolList
B = indices.Count
C = curCount
'D = modInt

Dim document As GH_Document = owner.OnPingDocument()
If (document Is Nothing) Then Return

document.ScheduleSolution(delay, New GH_Document.GH_ScheduleDelegate(AddressOf ScheduleCallBack))


End Sub

'<Custom additional code>

Dim initList As Boolean
Dim delay As Int32
Dim saveCur As Boolean = True
Dim curButton As Int32
Dim curCount As Int32 = 0
'Dim modInt As Int32 = 0
Dim itemVar As Int32

Private Sub ScheduleCallBack(ByVal document As GH_Document)
owner.ExpireSolution(False)
End Sub

Views: 600

Replies to This Discussion

I figured it out - probably not the best way to do it, but it seems to work. I just declare the array outside of runscript and then redim it inside runscript only the first time the script is run (in this case when initList = true)

Private Sub RunScript(ByVal current As Object, ByVal selTime As Integer, ByVal indices As List(Of Integer), ByRef A As Object, ByRef B As Object, ByRef C As Object, ByRef D As Object)

delay = selTime

If (initList = True) Then
ReDim boolList(indices.Count - 1)
initList = False
End If

If (saveCur = True) Then
curButton = current
saveCur = False
curCount = 0
End If

If (curButton <> current) Then
curButton = current
saveCur = True
curCount = 0
Else
curCount += 1
End If

If (curCount > selTime) Then
boolList(current - 1) += 1
curCount = 0

End If

A = boolList
B = indices.Count
C = curCount
'D = modInt

Dim document As GH_Document = owner.OnPingDocument()
If (document Is Nothing) Then Return

document.ScheduleSolution(delay, New GH_Document.GH_ScheduleDelegate(AddressOf ScheduleCallBack))


End Sub

'<Custom additional code>

Dim boolList() As Integer
Dim initList As Boolean = True
Dim delay As Int32
Dim saveCur As Boolean = True
Dim curButton As Int32
Dim curCount As Int32 = 0
'Dim modInt As Int32 = 0
Dim itemVar As Int32

Private Sub ScheduleCallBack(ByVal document As GH_Document)
owner.ExpireSolution(False)
End Sub

Any variable only exists within the scope of it's creation (or usage). If you exit the sub, the array created inside will loose scope and be destroyed.

Outside the RunScript sub is the scope of the component on the GH canvas. So the initList will exist as long as the component lives on the canvas.

You might want to check array dimensions and redim if neccessary for cases when array length of indices changes.

Hey Hannes, thanks for the reply.

Yes, right after I posted I added another conditional which would detect if indices.Count changes and sets initList to True (which would redim the array).

-Brian

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