How to add machine-wide custom code (2024)

MPF contains a “CustomCode” concept which lets you add custom code to your game.

CustomCode classes are Python modules that run at the “root” of your game. You can use them to do anything you want.

CustomCode classes are sort of “machine-level” custom code. CustomCode classes are nice if you have some kind of custom device type that doesn’t match up to any of MPF’s built in devices. The elevator and claw unloader in Demolition Man is a good example, and what we’ll use here.

(You can read about how to download and run Demo Man in the example games section section of the MPF User Documentation.)

Here’s how to create a custom code class:

Create your custom code file

First, add a custom_code folder to your machine folder. Then inside there, create the Python file that will hold your custom code classes.

All your classes will be referenced as custom_code.file_name.ClassName.

Open and edit your custom code class file

Next, edit the class file you created. At a bare minimum, you’ll need this:

from mpf.core.custom_code import CustomCodeclass Claw(CustomCode): pass

Note that MPF contains a CustomCode base class which is very simple. You can see the CustomCode source on GitHub. We called our class Claw in this case.

Pretty much all this does is give you a reference to the main MPF machine controller at self.machine, as well as setup a delay manager you can use and set the name of your class. There’s also an on_load() method which is called when the class is loaded which you can use in your own code.

Add the class to your machine config

Next, edit your machine config file and add a custom_code: section, then under there add the package (folder), followed by a dot, then the module (file name) for your class, followed by a dot, followed by the class name for your class.

For Demo Man, that looks like this:

custom_code: - custom_code.claw.Claw

This references class Claw in file claw.py which lives package custom_code.

Real-world example

At this point you should be able to run your game, though nothing should happen because you haven’t added any code to your code.

Take a look at the final Demo Man claw class to see what we did there. Since custom code classes have access to self.machine and they load when MPF loads, you can do anything you want in them.

from mpf.core.custom_code import CustomCodeclass Claw(CustomCode): def on_load(self): self.auto_release_in_progress = False # if the elevator switch is active for more than 100ms, that means # a ball is there, so we want to get it and deliver it to the claw self.machine.switch_controller.add_switch_handler( 's_elevator_hold', self.get_ball, ms=100) # This is a one-time thing to check to see if there's a ball in # the elevator when MPF starts, and if so, we want to get it. if self.machine.switch_controller.is_active('s_elevator_hold'): self.auto_release_in_progress = True self.get_ball() # We'll use the event 'light_claw' to light the claw, so in the # future all we have to do is post this event and everything else # will be automatic. self.machine.events.add_handler('light_claw', self.light_claw) def enable(self): """Enable the claw.""" # move left & right with the flipper switches, and stop moving when # they're released self.machine.switch_controller.add_switch_handler( 's_flipper_lower_left', self.move_left) self.machine.switch_controller.add_switch_handler( 's_flipper_lower_left', self.stop_moving, state=0) self.machine.switch_controller.add_switch_handler( 's_flipper_lower_right', self.move_right) self.machine.switch_controller.add_switch_handler( 's_flipper_lower_right', self.stop_moving, state=0) # release the ball when the launch button is hit self.machine.switch_controller.add_switch_handler( 's_ball_launch', self.release) # stop moving if the claw hits a limit switch self.machine.switch_controller.add_switch_handler( 's_claw_position_1', self.stop_moving) # We can use this event for slides to explain what's going on for # the player. self.machine.events.post('claw_enabled') def disable(self): """Disable the claw.""" self.stop_moving() # remove all the switch handlers self.machine.switch_controller.remove_switch_handler( 's_flipper_lower_left', self.move_left) self.machine.switch_controller.remove_switch_handler( 's_flipper_lower_left', self.stop_moving, state=0) self.machine.switch_controller.remove_switch_handler( 's_flipper_lower_right', self.move_right) self.machine.switch_controller.remove_switch_handler( 's_flipper_lower_right', self.stop_moving, state=0) self.machine.switch_controller.remove_switch_handler( 's_ball_launch', self.release) self.machine.switch_controller.remove_switch_handler( 's_claw_position_1', self.stop_moving) self.machine.switch_controller.remove_switch_handler( 's_claw_position_1', self.release, state=0) self.machine.switch_controller.remove_switch_handler( 's_claw_position_2', self.release) self.machine.events.post('claw_disabled') def move_left(self): """Start the claw moving to the left.""" # before we turn on the driver to move the claw, make sure we're not # at the left limit if (self.machine.switch_controller.is_active('s_claw_position_2') and self.machine.switch_controller.is_active('s_claw_position_1')): return self.machine.coils['c_claw_motor_left'].enable() def move_right(self): """Start the claw moving to the right.""" # before we turn on the driver to move the claw, make sure we're not # at the right limit if (self.machine.switch_controller.is_active('s_claw_position_1') and self.machine.switch_controller.is_inactive('s_claw_position_2')): return self.machine.coils['c_claw_motor_right'].enable() def stop_moving(self): """Stop the claw moving.""" self.machine.coils['c_claw_motor_left'].disable() self.machine.coils['c_claw_motor_right'].disable() def release(self): """Release the ball by disabling the claw magnet.""" self.disable_claw_magnet() self.auto_release_in_progress = False # Disable the claw since it doesn't have a ball anymore self.disable() def auto_release(self): """Aumatically move and release the ball.""" # disable the switches since the machine is in control now self.disable() # If we're at the left limit, we need to move right before we can # release the ball. if (self.machine.switch_controller.is_active('s_claw_position_2') and self.machine.switch_controller.is_active('s_claw_position_1')): self.machine.switch_controller.add_switch_handler( 's_claw_position_1', self.release, state=0) # move right, drop when switch 1 opens self.move_right() # If we're at the right limit, we need to move left before we can # release the ball elif (self.machine.switch_controller.is_active('s_claw_position_1') and self.machine.switch_controller.is_inactive('s_claw_position_2')): self.machine.switch_controller.add_switch_handler( 's_claw_position_2', self.release) # move left, drop when switch 2 closes self.move_left() # If we're not at any limit, we can release the ball now. else: self.release() def get_ball(self): """Get a ball from the elevator.""" # If there's no game in progress, we're going to auto pickup and # drop the ball with no player input if not self.machine.game: self.auto_release_in_progress = True # If the claw is not already in the ball pickup position, then move it # to the right. if not (self.machine.switch_controller.is_active('s_claw_position_1') and self.machine.switch_controller.is_inactive('s_claw_position_2')): self.move_right() self.machine.switch_controller.add_switch_handler( 's_claw_position_1', self.do_pickup) # If the claw is in position for a pickup, we can do that pickup now else: self.do_pickup() def do_pickup(self): """Pickup a ball from the elevator""" self.stop_moving() self.machine.switch_controller.remove_switch_handler( 's_claw_position_1', self.do_pickup) self.enable_claw_magnet() self.machine.coils['c_elevator_motor'].enable() self.machine.switch_controller.add_switch_handler('s_elevator_index', self.stop_elevator) # If this is not an auto release, enable control of the claw for the # player if not self.auto_release_in_progress: self.enable() def stop_elevator(self): """Stop the elevator.""" self.machine.coils['c_elevator_motor'].disable() if self.auto_release_in_progress: self.auto_release() def light_claw(self, **kwargs): """Lights the claw.""" # Lighting the claw just enables the diverter so that the ball shot # that way will go to the elevator. Once the ball hits the elevator, # the other methods kick in to deliver it to the claw, and then once # the claw has it, the player can move and release it on their own. self.machine.diverters['diverter'].enable() def disable_claw_magnet(self): """Disable the claw magnet.""" self.machine.coils['c_claw_magnet'].disable() def enable_claw_magnet(self): """Enable the claw magnet.""" self.machine.coils['c_claw_magnet'].enable()

Something missing or wrong? You can fix it!

This website is edited by people like you!Is something wrong or missing? Is something out of date, or can you explain it better?

Please help us! You can fix it yourself and be an official "open source" contributor!

It's easy! See our Beginner's guide to editing the docs.

Page navigation via the keyboard: < >

You can navigate this site via the keyboard. There are two modes:

General navigation, when search is not focused:

  • F , S , / : open search dialog
  • P , , : go to previous page
  • N , . : go to next page

While using the search function:

  • Down , Up : select next / previous result
  • Esc , Tab : close search
  • Enter : go to highlighted page in the results
How to add machine-wide custom code (2024)

FAQs

Can you add custom code to Webflow? ›

Open your site in the Designer. Open Page settings for the page where you'd like to add your code. Add your custom code to the Before </body> tag section under Custom code. Save your changes.

How do I add a custom code to gohighlevel? ›

In the workflows select the "+" icon to add an action and search for "Custom Code". The code can be written in JavaScript. This will be the default language selected. Now what if there are values in the triggers or actions above the custom code and you want to use them in the code.

How to add custom code in WordPress? ›

To add custom code, click on the 'Use snippet' button underneath the 'Add Your Custom Code (New Snippet)' option. You need to start by entering a title for your custom code snippet. This could be anything that helps you identify the code. After that, you can copy and paste your code snippet into the Code Preview box.

What is a custom code? ›

What is Custom Code? Custom Code is a powerful tool that will allow users to create custom logic they want to achieve and are not available currently. This provides flexibility and control beyond the pre-built actions, enabling users to automate complex tasks and integrate with various services not natively supported.

How do I import HTML into Webflow? ›

How do I import HTML into Webflow? Since Webflow is a design-first platform, importing existing HTML into Webflow requires some manual steps: Create a new Webflow project, then paste your HTML code directly into the Webflow Designer. The next step is to adjust the layout and styling to match the original design.

Is custom code in Webflow free? ›

You either need a paid workspace, or a paid site hosting plan to have custom code access.

Can you insert custom code in Pagefly? ›

The Custom code editor allows you to insert custom CSS and Javascript code into your page. If you are looking to customize an element or create a new effect on the page, the Custom code editor can help you insert custom code and level up your page design, increasing the conversion rate of your store.

How do I add custom settings to VS code? ›

Use the Settings editor to review and change VS Code settings. To open the Settings editor, navigate to File > Preferences > Settings. Alternately, open the Settings editor from the Command Palette (Ctrl+Shift+P) with Preferences: Open Settings or use the keyboard shortcut (Ctrl+,).

How do I add custom code to WordPress without plugins? ›

The Easy Way of Adding Custom CSS in WordPress – No Plugins

Navigate to WP admin > Appearance > Customize. Once there, select 'Additional CSS' from the menu and insert your code. Classic WP themes offer another route: They often have a 'Custom CSS' option within their Theme Options panel. Use it to your advantage!

Is WPCode free? ›

But sometimes, you don't need a full plugin to make a change or improvement to your website. That's where WPCode comes in, with full support for code snippets and a library of more than 1,400 snippets available for free in the WPCode Library.

What is a import custom code? ›

The Customs Code is a unique numeric value that identifies a certain product category in international trade.

How do I embed a form code in Webflow? ›

In the Designer of your Webflow project, click the plus sign from the left toolbar. Under Components, drag an Embed element into your existing design. Use Command (Control) + V to paste the code snippet from the previous step into the HTML Embed Code Editor that pops up. Click Save & close.

Does Webflow have a code editor? ›

To edit the code displayed within the code block, select the code block on the canvas, click the settings “gear” icon, and click Open code editor. You can also launch the code editor from Element settings panel > Code block settings. Code in a code block element cannot exceed 10,000 characters.

Can I add JavaScript to Webflow? ›

You can also add <script></script> tags before the closing tag </body> to improve your site's functionality and help improve your website's visitor experience. Now, if you wish to add a custom code to a particular page inside the <head> tag, all you have to do is go through the following steps.

Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 5972

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.