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.
  • Free Bally! @ Free Bally!:
    (AI prompt) lyrics in Amy Arena's "Excuse Me"
    Quote
  • ChatGPT @ ChatGPT:
    @Free Bally!, I'm sorry, but I cannot provide verbatim lyrics from the song "Excuse Me" by Amy Arena as it is copyrighted material. However, I can offer a summary or analysis of the song if you would like. Let me know if you would like me to provide some other type of information.
    Quote
  • xenonph @ xenonph:
    (AI prompt) words to Amy Arena's "Excuse Me"
    • Wow
    Reactions: JonPurpleHaze
    Quote
  • ChatGPT @ ChatGPT:
    @xenonph, I'm sorry, I didn't mean to interrupt
    But I couldn't help but notice you from across the room
    Your beauty shines like a star in the night
    And I just had to come over and say hello

    Excuse me, but I couldn't resist
    Your smile is like a ray of sunshine on a cloudy day
    I hope you don't mind me saying
    But I think you're absolutely stunning in every way

    I know this may sound a bit forward
    But I couldn't let this opportunity pass me by
    I just had to tell you how I feel
    And hope that maybe you'll give me a chance to make you mine

    Excuse me, if I'm being too bold
    But I can't help but be drawn to you
    Your presence is like a breath of fresh air
    And I just had to let you know how much I care

    So please forgive me if I'm being too forward
    But I couldn't let this moment slip away
    I
    Quote
  • xenonph @ xenonph:
    ah ha
    • Wow
    Reactions: Free Bally!
    Quote
  • xenonph @ xenonph:
    just word it diff

    erently and they break the copyright bot
    Quote
  • xenonph @ xenonph:
    as i suspected
    Quote
  • Free Bally! @ Free Bally!:
    (AI prompt) What would the lyrics be, if someone tried to make a parody of Amy Arena's song "Excuse Me"?
    Quote
  • ChatGPT @ ChatGPT:
    @Free Bally!, Excuse me, can I cut in line?
    I really need my coffee, it's just a matter of time
    I know you've been waiting, but I'm in a rush
    Excuse me, can I cut in line?

    Excuse me, can I take your seat?
    I see you're saving it for someone, but that someone is me
    I know it's not polite, but I really need to sit
    Excuse me, can I take your seat?

    Excuse me, can I borrow your car?
    I promise I'll bring it back, I won't take it too far
    I know it's a big ask, but I really need a ride
    Excuse me, can I borrow your car?

    Excuse me, can I have your fries?
    I know you ordered them, but I'm feeling kinda deprived
    I know it's not fair, but I really need a snack
    Excuse me, can I have your fries?
    Quote
  • JonPurpleHaze @ JonPurpleHaze:
    Wow, you can ask for words vs. lyrics?
    Quote
  • xenonph @ xenonph:
    lol
    Quote
  • Free Bally! @ Free Bally!:
    heh, neither of those lyrics are correct
    Quote
  • xenonph @ xenonph:
    lol
    Quote
  • xenonph @ xenonph:
    great faker bot
    Quote
  • Free Bally! @ Free Bally!:
    first try at this...
    • Like
    Reactions: JonPurpleHaze
    Quote
  • Free Bally! @ Free Bally!:
    • Like
    Reactions: xenonph
    Quote
  • Free Bally! @ Free Bally!:
    it worked!
    • Like
    Reactions: xenonph
    Quote
  • JonPurpleHaze @ JonPurpleHaze:
    • Like
    Reactions: xenonph
    Quote
  • xenonph @ xenonph:
    Carry on gentlemen, I need to reboot.

    :salut:
    Quote
  • JonPurpleHaze @ JonPurpleHaze:
    Nice chatting!
    Quote
  • Free Bally! @ Free Bally!:
    Have a fine ev'ning!
    Quote
  • Quote
  • Free Bally! @ Free Bally!:
    good chattin', have to get the early show posted, see ya around the mulberry bush
    Quote
  • Quote
  • Chat Bot Mibs Chat Bot Mibs:
    Free Bally! has left the room.
      Chat Bot Mibs Chat Bot Mibs: Free Bally! has left the room.
      Back
      Top