Scripting

bigtal13

Pinball Wizard
Joined
May 16, 2011
Messages
4
Reaction score
1
Points
10
Favorite Pinball Machine
Flash Gordon
Is there an easy way to learn how to script a table? I mean, besides trial and error.
 
The first thing that you need to know is how to use Dim and add the score.
A Dim is VisualBasic Scripting's name for a Variable. Variables are memory placeholders that are used to store values that you wish to save. In other programming languages, a variable is known as a Dim, a variable, a var, and there may be others.

At the top of the editor click File > New Table.
Open the table's Script and at the top of the script type:

Dim Score

Now below that line, create this Sub. You can Copy/Paste the following into the script:

Code:
Sub AddScore(Points)
Score = Score + Points
ScoreText.Text = Score
End Sub

Now, when you place a Bumper on the Table, keep it Highlighted in blue or click on it to highlight it and go to the script. You can also go to the script and use the top left Pull-down to select the Bumper. Then from the Right-side Pull-down in the script window select Hit().
The script will automatically create the Sub Bumper1_Hit()
Inside of this Sub, between the Sub and End Sub statements, type this:

AddScore 100

This statement tells the program to go to the Sub AddScore and to execute the statements within the Sub, in this case, Add 100 points to the Dim Score that you created. The value of 100 is passed as the parameter of (Points) to the Sub AddScore(Points). Here, Points is operating as an Internal Dim (Points) and the value 100 is an assumed parameter value by the declaration in the Sub heading (Points)

You can use any unique name for the Dim, as long as the name is not the same as any other Dim or any command statement.
Now, when you play the table and hit the Bumper1, you should see 100 Points added to the ScoreText display.

Note: You can use any TextBox that you place on the Table >Backglass editor screen to display the score, or whatever data you choose to display. Just remember that if you rename ScoreText or a TextBox to another name that you create for the TextBox, say TextBox1, or Zots, etc., then to always use that name in script when calling it and to choose a Unique Name different from all other Objects and Command Statements.

Example:
Code:
Sub AddScore(Points)
Score = Score + Points
[b]Zots[/b].Text = Score
End Sub

Note that the statement Zots.Text = Score tells the program that the .Text to be displayed in the TextBox named Zots will be the value saved by the program in the Dim Score.
Any value that you save to a Dim, be it a number or a word (a Text String) will be displayed in the TextBox that you choose when you tell the program to do so using the .Text method.
 
Last edited:
Thank You sooooo much! My nickname at work is "sleepy" haha I can make fun of my narcolepsy right?
 
The best way I've found to learn is to take apart a game in the editor and see how it works. We all did that as kids, right? Click on an object such as a target, look in the script for the object and see what is written for the object to do and how it's written. The rest is all semantics. I learned to code and I learned to turn ON a computer 3 years ago, so it can't be that bad. Or you can start with a template and just add the code for the objects you add. There are many great templates here (including my own).:welcome1:
 
You want to know the keycodes for the keys?
Open a new Table (or any Table. In the script, find the Sub Table1_KeyDown(ByVal keycode) and note the statement structure for the If/Then Conditional Statements

Inside of this Sub add this:

ScoreText.Text = keycode

Now Play the Table. Note that the keycode for each key pressed will appear in the ScoreText window. Press the 5 key at the top of the keyboard and note the keycode is 6.
Press the top 1 key and note the keycode for this key is 2.

Be aware that the keycodes in Visual Pinball are different from the ASCII keycodes used by the Windows operating system and by other programs which use the ASCII codes for each key.

Exit and return to the script. Notice that there are two keycode Subs, one for keypresses (_Keydown) and one for releasing each key (_KeyUp). Take a look at the If/Then statements for the LeftFlipper and RightFlipper keycodes in both Subs and what the script says to do to the Flippers when you press or release the associated keys.

Okay, now you want more Dims, a couple more Objects on the Table, and a few Conditional Statements.

Click the Backbox button and place two TextBoxes. They should list by the names TextBox1 and TextBox2.

Now in script add these Dims at the top:

Dim Score, Game, Credits, Balls

Find Sub Plunger_Init() and change the name to Sub StartGame. Now go up to the
Sub Table1_KeyDown(ByVal keycode)and add these Conditional Statements inside of the Sub but outside of the existing If/Then statements found there:

Sub Table1_KeyDown(ByVal keycode)
If keycode = 6 Then
Credits = Credits + 1
TextBox1.Text = Credits
End If

If keycode = 2 Then
StartGame
End If


Now Play the Table. The Table does not automatically create the Ball.
Press the 1 key. The program creates the Ball because in script you told the program that when the 1 key is pressed, to then go to the StartGame sub and create the ball.
But now, everytime you press the 1 key, the program creates another Ball. So we might need to prevent that.
Use the left side script Pull-down and select Table 1, then from the right side Pull-down select Init:
The program automatically creates this Sub. Find it at the bottom of the script and inside add this:

Sub Table1_Init()
Game = False
End Sub

Go back to Sub Table1_KeyDown(ByVal keycode)
Add this to the If keycode = 2/Then statement:

If keycode = 2 And Game = False Then
Game = True
StartGame
End If

With Game = False in the keycode statement, when Game = True (Because you set the Dim Game to True) in a statement, then the keypress will exit the If/Then statement without executing any of the statements contained within (like creating more Balls)until the Dim Game is reset to False.

Note that Subs that include _Init() in the name are proocessed at the initialization of the Player (the initial creation of the Table) before anything else is run and that they only run one time up front unless for some reason you call the Sub again later in the script. When you call a Sub, you do not use the term Sub, only the name of the Sub and whatever values you pass in the same line following the name or in parenthesis if indicated. If the name of the Sub includes empty parenthesis, then include the parenthesis in the call plus whatever values that may be assigned within the parenthesis when available or when assigned specifically to the parenthesis as a Dim. When a Dim is explicitly assigned to a Sub, then only include the desired value within the parenthesis. See the Sub AddScore(Points) for clarification.

Okay.
Now we want to get fancy.
Go to the Sub Table1_KeyDown(ByVal keycode) and in the If keycode = 2 Then statement add:

If keycode = 2 And Credits > 0 And Game = False Then
Game = True
Credits = Credits - 1
TextBox1.Text = Credits

StartGame
End If
...
End Sub

Now find the Sub StartGame and add:

Sub StartGame

Score = 0
ScoreText.Text = Score
Balls = 1
TextBox2.Text = Balls

PlaySound "Plunger"
Plunger.CreateBall

End Sub

Sub Drain_Hit()
If Balls > 3 Then
Game = False
Exit Sub
End If

Drain.DestroyBall
Plunger.CreateBall
PlaySound "Plunger"
Balls = Balls + 1
TextBox2.Text = Balls



End Sub

Note that the Dim Game does not control the end of the game, but only sets the If/Then condition based on what you tell the program to look for.
 
Last edited:
cool! thanks for the breakdown, i kind of wondered exactly how each routine was called up havng adhd and being a little ocd i have to know the why's and not just the hows of the routines it actually helps me learn it quicker once I know the technical aspects of the coding.
 
Yep. The rote answers do nothing for me too.
The "Why and How it works" helps a lot in creating new ideas.
 
Yep. The rote answers do nothing for me too.
The "Why and How it works" helps a lot in creating new ideas.
 
Heh...double post.
 
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.
  • ChatGPT @ ChatGPT:
    @Free Bally!, Pirates are often depicted with eye patches in popular culture, but the reasons for this vary. Here are a few possible explanations:

    1. **Injury**: The most straightforward reason a pirate might wear an eye patch is that they lost an eye due to injury. Pirate battles, sword fights, and the general hazards of seafaring life could easily lead to eye injuries, necessitating an eye patch.

    2. **Myth and Stereotype**: The image of a pirate with an eye patch has become a stereotype perpetuated by movies, books, and other forms of media. It's part of the romanticized pirate image that includes peg legs, hooks for hands, and parrots on shoulders.

    3. **Preserving Night Vision**: A more interesting and practical theory suggests that pirates wore eye patches to keep one eye adapted to the dark. When moving between the bright deck and the dark below decks, a pirate could switch the patch from one eye to the other. The eye that was covered
    Quote
  • Free Bally! @ Free Bally!:
    nope... it was because a seagul shit in his eye the day after he got the new hook fitted
    • Haha
    Reactions: JonPurpleHaze
    Quote
  • JonPurpleHaze @ JonPurpleHaze:
    He couldn't scratch his eye, or tried and had to get the patch because of the hook...:pirat:🏴‍☠️🪝
    • Like
    Reactions: Free Bally!
    Quote
  • Free Bally! @ Free Bally!:
    ahh well, back to work tomorrow
    • Like
    Reactions: JonPurpleHaze
    Quote
  • Free Bally! @ Free Bally!:
    Looking at Tuesday for a real day off, ahhh
    Quote
  • JonPurpleHaze @ JonPurpleHaze:
    Hope you find your keys!
    Quote
  • Free Bally! @ Free Bally!:
    But working long every darned day has (Finally!) cleared up pain in my hip that has plagued me since April
    Quote
  • Free Bally! @ Free Bally!:
    haha, naw the keys are long gone. HAVE to get a new pair of pants... Without A Hole In The Pocket!
    • Haha
    Reactions: JonPurpleHaze
    Quote
  • JonPurpleHaze @ JonPurpleHaze:
    Get AC 4! you :pirat:
    • Like
    Reactions: Free Bally!
    Quote
  • Free Bally! @ Free Bally!:
    Got AC, weather-liars say i'll need an umbrella all weekend
    • Haha
    Reactions: JonPurpleHaze
    Quote
  • Free Bally! @ Free Bally!:
    ohh oops
    Quote
  • Free Bally! @ Free Bally!:
    you mean the pirate game!
    • Like
    Reactions: JonPurpleHaze
    Quote
  • Quote
  • JonPurpleHaze @ JonPurpleHaze:
    AC = Assasin's Creed...4...Black Flag.
    Quote
  • Free Bally! @ Free Bally!:
    heh, they all look like pirate queens, instead of a gig on the beach, the Law Of The Sea says they have to fight until there's only one
    • Like
    Reactions: JonPurpleHaze
    Quote
  • Quote
  • Free Bally! @ Free Bally!:
    ahoy, have to go make some salad of fish chunks flavored to taste like crab, have a salty Friday ya scallawag!
    Quote
  • Chat Bot Mibs Chat Bot Mibs:
    Free Bally! has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    yirmiahu has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    Guenther has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    alain.rev has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    eduguitar72 has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    mickcarmone has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    No World Order has left the room.
  • Chat Bot Mibs Chat Bot Mibs:
    ususio1011 has left the room.
      Chat Bot Mibs Chat Bot Mibs: ususio1011 has left the room.
      Back
      Top