Python DLL

Step 1 (Install)
To build a Python Extension for Game Maker, you will need the following tools (install in this order):


 * Python: Download Python 2.5
 * Cython: Download Cython for Python 2.5
 * MinGW: Download MinGW installer
 * Extension Builder: Download Extension Builder

When you install MinGw, it will ask what you would like to install. Make sure you place a check next to these two boxes, and any others you would like:


 * g++
 * Make

Step 2 (Environment Variables)
After you have installed MinGw, you will need to add a PATH Environment variable, to do so, follow these steps:


 * Right click "My Computer" ("Computer" for Vista users)
 * Click properties
 * Click the "Advanced" tab
 * Click "Environment Variables"

In the dialog box that appears, at the bottom scroll down until you see "Path" and double click on it, then add this to the beginning of your path list:

C:\MinGW\bin;C:\Python25;

Press "OK" on the three windows you opened to save the information.

Step 3 (Create a File)
Last step! Then we can make our fist python dll!

Create (or edit): C:\Python25\Lib\distutils\distutils.cfg and add the following 2 lines:

[build] compiler=mingw32

Writing Our First Extension
To write a python extension for game maker, you need to create a new folder to store your extension. It is recommended that you create one in your root directory (C:). Call it "example":

C:\example

In this directory, we need to create a python file. Call it example.py. We will also need to create a file called setup.py. Let's start with setup.py. In this file, we need to add the following code:

setup.py
from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup(   cmdclass = {'build_ext': build_ext},    ext_modules = [Extension("example", ["example.py"])] )

If you look on the second to last line, you should notice that this is where we name our input file (example.py) and output (example) file name. If you named your example.py file something else, you should change that part of the code to match the name you called it.

example.py
Now to write our function!

We will do the basic addition function, which would looks something like this: cdef extern double add(double a, double b): return a + b

Save that file, and now lets compile it.

Compile Extension
To compile your extension, you must open up the CMD Window:
 * Click: "Start"
 * Click: "Run..."
 * Type: "CMD"
 * Press: "Enter"

In the command prompt window, you will need to switch to your file's directory. To do so, type:

cd C:\example

This will take you to the directory of your file, then just type in:

python setup.py build_ext --inplace

This will compile your python file into a dll or pyd. If the file compiles to a pyd, don't worry, you just need to change the file extension to a dll, and it will work fine!

Build Game Maker Extension
To build the extension for game maker, open up Extension_Maker.exe which is found in the last download from the beginning of this tutorial Download. Once this application has opened, fill out the top part with a Name (Required). Choose something that tells what the function is. For example: Add(num1,num2).

Next, click on the green button on the bottom left that says "Add DLL" and locate the dll that we just made and double click on it.

Near the bottom of the window, you will find a button that says "Add". Click on that to add our function.


 * Name: The function name you want to use in Game Maker
 * External Name: The function name found in the dll
 * Help Line: The hints that are shown in the bottom of the script window

Fill it in something like this:
 * add
 * add
 * add(num1, num2)

Next change Arguments From 0 to 2 and make sure they are both set to "Real" under "Argument Types". For our Result Type and Calling Convention, leave those as is. They are set to what you want.

Save the extension. This is an editable file and should be kept in case you want to make any changes. After saving, click:


 * File
 * Build Package...

Save your file somewhere. It is highly recommended you save it in the extensions folder of your Game Maker directory. It will have .gex as an extension.

Install Your Game Maker Extension
To install the GEX file in Game Maker, open up game maker, and click on the green plus sign to the right on the toolbar. Then:


 * Click "Install"
 * Click "Install" again
 * Find the file C:\Program Files\Game Maker 8\extensions\add.gex (This may differ with operating systems)
 * Click "Open"
 * Click "OK"

From here you should still have a window open, this window displays all of your installed packages. Choose the one we just create on the right hand side, then click on the blue arrow to move it over to the left hand side.


 * Click "OK"

That should install the package, and now it is ready to test!

Test Your Function
Your almost done!

Create a new room, and in the room toolbar select the tab that says Settings. Click the button that says Creation Code. When the script window comes up, insert this code into it:

show_message(string(add(1,3)));

You can modify the numbers if you want. Click the Check mark, and run your game. Using the above code you should get a message box that says: 4 (or some other number if you used two different numbers).

Conclusion
Well, those are the basics to create a python extension for Game Maker. Feel free to experiment, and try some of your own ideas!

Note: Python is tab sensitive, if you don't tab your functions you will probably get an error.

Resources
Python