- Joined
- Jun 21, 2020
- Messages
- 2,036
- Solutions
- 1
- Reaction score
- 1,194
- Points
- 125
- Favorite Pinball Machine
- Indiana Jones
Thanks @ravarcade for providing me with the code I could use and explaining a couple of ways to solve my problem.
This is apparently meta-programming by the way.
The problem:
Similarly to what happens in VP I wanted to group some objects that had the same exact behaviour. Problem is, I didn't want to do it for 1 or 2 but 60 objects... That's a lot of repetitive code :)
So here are a couple of ways to do that. Lets start with the easy stuff.
Problem:
you have a lot of guides. Usually you just want them to guide the ball somewhere but maybe you want them to play a sound when hit. Here's a simple way to do that providing you number them sequentially, in this case I have 6 guides.
If you add 1 more guide, just change 6 to 7 and add another line: "Sub Guide7 ... : End Sub"
You can also use the method described here for droptargets. In this case you just maintain the array and you use the special sub "SetHitSub" that does what it says on the tin; it creates a new _hit sub that FP will recognize as such. Then you use that new _hit for watever you need it to do.
So using the same Guide1 to Guide6 example:
Easy to maintain, right? Things get tricky when the names are not at the end of the names you have dozens of triggers to deal with.
I add to use the above SetHitSub again but I already had it for other purposes. So I added a new var just for this case to run just once for this particular purpose.
"That's All Folks" (tm)
This is apparently meta-programming by the way.
The problem:
Similarly to what happens in VP I wanted to group some objects that had the same exact behaviour. Problem is, I didn't want to do it for 1 or 2 but 60 objects... That's a lot of repetitive code :)
So here are a couple of ways to do that. Lets start with the easy stuff.
Problem:
you have a lot of guides. Usually you just want them to guide the ball somewhere but maybe you want them to play a sound when hit. Here's a simple way to do that providing you number them sequentially, in this case I have 6 guides.
Code:
Sub Guide1to6_Hit()
Playsound "guide_sound"
End Sub
Sub guide1_hit() : Guide1to6_Hit : End Sub
Sub guide2_hit() : Guide1to6_Hit : End Sub
Sub guide3_hit() : Guide1to6_Hit : End Sub
Sub guide4_hit() : Guide1to6_Hit : End Sub
Sub guide5_hit() : Guide1to6_Hit : End Sub
Sub guide6_hit() : Guide1to6_Hit : End Sub
If you add 1 more guide, just change 6 to 7 and add another line: "Sub Guide7 ... : End Sub"
You can also use the method described here for droptargets. In this case you just maintain the array and you use the special sub "SetHitSub" that does what it says on the tin; it creates a new _hit sub that FP will recognize as such. Then you use that new _hit for watever you need it to do.
So using the same Guide1 to Guide6 example:
Code:
' special sub that allows to create a newer _hit sub
Sub SetHitSub(array, hitSubName)
addDebugText"SetHitSub"
Dim i
For i = 1 To UBound(array)
ExecuteGlobal "Sub " & array(i).name & "_hit(): " & hitSubName & "(" & i & "): End Sub"
' for every index of array aMetals it creates a Sub
' index 1 is guide1 so Guide1_hit will be index 1 in Sub Metals_Hit(idx)
' whenever guide1 is triggered, metals_hit plays the sound.
Next
End Sub
Dim aMetals(6) ' it allows for 7 objects - arrays start at index 0 (zero) but I don't use it here for the sake of readability
Set aMetals(1) = Guide1 : Set aMetals(2) = Guide2 : Set aMetals(3) = Guide3
Set aMetals(4) = Guide4 : Set aMetals(5) = Guide5 : Set aMetals(6) = Guide6
'Set "Metals_Hit" as the name of the Sub that processes metal hit events
SetHitSub aMetals, "Metals_Hit"
Sub Metals_Hit(idx)
Playsound "guide_sound"
End Sub
Easy to maintain, right? Things get tricky when the names are not at the end of the names you have dozens of triggers to deal with.
I add to use the above SetHitSub again but I already had it for other purposes. So I added a new var just for this case to run just once for this particular purpose.
Code:
Dim RunJustOnce ' just for this particular rubberHit triggers purpose.
Sub SetHitSub(array, hitSubName)
addDebugText"SetHitSub"
Dim i
For i = 1 To UBound(array)
ExecuteGlobal "Sub " & array(i).name & "_hit(): " & hitSubName & "(" & i & "): End Sub"
Next
' -> Relevant part for this example<-
' -> this is for rubber triggers <-
If RunJustOnce <> 1 Then
For i = 1 To 60
ExecuteGlobal "Sub DRubber" & i & "Hit_hit() : DRubberX_Hit() : End Sub"
' for DRubber1Hit to DRubber60Hit add "DRubber<number>Hit_hit() as index of Sub DRubberX_hit
' to add 1more trigger, change 60 to 61... a lot easier to maintain :)
Next
RunJustOnce = 1
End If
End Sub
' RubberHit sounds
Sub DRubberX_Hit()
playsound "rubber_sound"
End Sub
"That's All Folks" (tm)
Last edited: