Ah! You want to write some Python? Well, look no further. Here you will learn the basics of Python-scripting in Golly™. Note that, while this won't function as a complete tutorial for Python, you should be able to still follow a bit even if you have no Python knowledge.
Things to Install
Wait! You need to prepare some stuff first. You need:
- Python 2.7+ (But not Python 3, since Golly doesn't support Python 3 as of the time of writing)
- Mouse (Optional)
If you are on a Mac, Python should already be installed (But alas, the author of this article doesn't have a "Mac", so the author is relying on information from others.). Once you have packed your stuff and prepared you can start your Pythoning adventure!
What are we Going to Make?
For this tutorial we are going to make a very simple script that detects the period of the object on screen. Golly already has a built-in script for this exact thing (See oscar.py)but we are going to make it anyway because why not. We will start by detecting the period of an oscillator, then move on to making it able to detect the period of spaceships.
The idea of this script is to take a the pattern and run it until it either repeats or crosses the set border of time. You'll understand later (hopefully) once we get into making the script. Let's draw an outline of our program in a flow chart (Note: The author has no idea how to properly draw a flow chart):
So there, we have the flow our program is going to go in. Now we just need to translate that to Python. To start, open a new Python IDLE window, or any text editor, in fact.
Let's get scripting!
Step 1: Setting things up
Let's set things up.
Believe it or not, Golly has it's own Python module (A python module is basically a set of functions)! It is used to make scripting easier and contains other useful non-Golly-related features such as easy dialog boxes without the need of the bulky tkinter commands. To import the Golly module, simply type
import golly as g
The 'as g' is not needed, but will make typing out the commands (Which are usually used a lot in Golly scripts) less tedious. Commands in that are packed into the Golly module can be found in Golly's help files under Help -> Python Scripting.
You could also do this if you want to omit "g." and make the commands even shorter, but beware of function name collisions:
from golly import *
We'll also need a variable for the period of the pattern, so put this in as well:
period = 0
Step 2: Is there a pattern?
There is a very simple way to test if the pattern is empty in Golly. If you read the Golly help files, you probably have found it already. If you haven't, it's
The g.empty() command returns a simple True or False. It will return True if the pattern is empty and False otherwise. So, we first want to check if there even is a pattern to check the period of. Let's do that by using the Python if statement:
if g.empty(): g.warn("No pattern!") g.exit()
This also shows us two other Golly commands:
- g.warn() opens a dialog box with the classing yellow triangle warning symbol with the specified message.
- g.exit() stops the script.