Code/Example BAM Reducing repetitive code

Coding and examples for future Pinball and BAM

AnonTet

Pinball Master
Site Supporters
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.

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:
Ah finally. Been wondering about this for 2 years, was getting to the point was going to ask rav about this. This means, using FOR/NEXT loops for the SET commands, and a "count" variable that the script will do when you start a table, you can have automatic recognition of objects by just dragging and dropping a object in the editor, something I have been wanting to add to fpxEngine for years. Add a proper built in menu system so the user can set scoring through the menu, and you have a very basic pinball construction set type system. This is also great for adding some of the new stuff coming out like fleep etc, all built into one set of code.
 
Fleep Sounds code I've been porting to FP was what it started this for me. You can find the code here and actually, I just changed it again... will post later or tomorrow.

If only I could merge it with smoke's rollingballsounds for at least paning everything... But I think that's beyond my ability.

I'm now glad I shared this :)
 
I'm not much of a coder but it looks interesting.
 
General chit-chat
Help Users
You can interact with the ChatGPT Bot in any Chat Room and there is a dedicated room. The command is /ai followed by a space and then your ? or inquiry.
ie: /ai What is a EM Pinball Machine?
  • No one is chatting at the moment.
  • Quote
  • xenonph @ xenonph:
    I sent you message with pic showing where to make visible adjustment.
    Quote
  • Chat Bot Mibs Chat Bot Mibs:
    Pinped has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    daleks12 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Stephen has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    docdoc has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Exnihilo_Mundus has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    angmarg52 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    tully619 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    smorndb has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Tech49 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Topsi Klaus has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Forsaken43 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    duduky72 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Gerge has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    nunolilo has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    DavidT2025 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Gary-7 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Thunderbird has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    alug has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    bluebird has left the room.
  • HZR @ HZR:
    It’s nice to go all see a place I can play actual machines!!!!
    Quote
  • Chat Bot Mibs Chat Bot Mibs:
    Sunrise74 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Rai has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    liebowa has left the room.
      Chat Bot Mibs Chat Bot Mibs: liebowa has left the room.
      Back
      Top