1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

Python Scripting

From construct

Jump to: navigation, search

Construct allows you to use Python (http://www.python.org) scripts inline with your events, enabling complex algorithms and calculations that may be difficult to express in events. Scripts also integrate with all the objects in the layout editor.

Contents

Getting Started

Here are some tutorials on the python language itself: tutorialspoint python.org tutorial

A quick look on the usage of python in construct. (by David): Python - A quick look

Adding scripts

Python scripts can be added in Construct via right-clicking the event sheet editor and selecting Insert Script. If Enable Scripting is not enabled in application properties, you will be asked if you want to enable it - this option must be on for scripting to work. The script editor then appears for you to edit your Python script.

You can also run user-typed scripts or scripts from text variables by using the system object action Run script.

Note you can also drag script blocks in to subevents: you can run a block of script in a Start of Layout event, for example, or under more complex conditions. A script on its own (not as a subevent) runs on the equivalent of Always, or once every tick.

Exported EXEs with Python scripting

You should be using build .99.84 or later for proper Python support. Exported EXEs with Enable Scripting turned on must redistribute "Python26.dll" in the same directory as the EXE file for scripting to work. "Python26.dll" can be found in the Construct install directory, eg. C:\Program Files\Scirra\Construct.

If Enable Scripting is turned off, you won't need any extra DLLs with your exported EXE.

In order to bundle external python libraries in your game follow this Tutorial. (by scidave) Python External Libraries

Scripts and objects

All the objects in the layout editor are also object names available in script: the script editor lists them on the right, and as you type their name followed by a dot, they will list all their member variables and functions - try typing Sprite. for example. There is a function for every action, condition and expression.

Many object's expressions are modifiable as properties. For example, the Sprite object's X, Y, Width, Height and Angle are modifiable by script as such:

Sprite.X = 10

Sprite.Y += 1

Sprite.Width *= 2

You can also access instances of an object using array notation:

#access third Sprite instance (python is zero indexed)
Sprite[2].Height /= 2

#iterate through all instances of Sprite
for s in Sprite:
 s.X += 2

Actions and conditions can be called in all objects as well, with the same parameter format as their event counterparts. The information bar at the top of the script editor helps remind you the parameters you need.

Accessing Globals

You can access Global variables through the System object:

t = System.globalvar('timeRemaining')

Script-Event communication

Events can retrieve data from scripts via the Python system expression. For example, the expression:

Python("x")

will return the content of the variable x. Similarly:

Python("myfunc()")

will return the value returned from calling the function myfunc in script. Further, you can use the system condition Python Compare, to directly compare a python variable in an event, such as the variable "x" greater than 100.

Scripts can trigger events by using the Function Object. The Call routine of the Function object takes two parameters: the function name, as a string, and the forget/remember picked objects option, which can be left as 0 (for Forget). For example:

Function.Call("My function", 0)

FAQ

Q. Do I need to install python to use python scripting?

A. No, as long as you're not using any external python libraries. All you need is at least version 0.99.84 of construct installed.


Q. If I make a game that uses python scripting, will the end user need to install python to run it?

A. No, just bundle "python26.dll" with the "exe" of your game.


Q. A built-in method wants an Object as an argument (System.Create, for example). What syntax do I use?

A. Pass the object name in string format, for example:
System.Create("Sprite", 1, 0, 0)


Q. I created a Widget object using System.Create(), but it isn't in the Widget array. How do I access it?

A. You can access the newly created Widget via the SOL object:
S = SOL.Widget

Additional Resources

A useful tool to experiment with python in construct. (by Silent Cacophony):Interactive Python Shell

Plugin to access the SOL (selected object list) from events in python. (by Lucid): pyfix

Event structure
Event Sheets Event groups - Events - Sub-events - Python scripting
Events Conditions - Actions - Expressions - Parameters
Tips and tricks Using functions
Project structure
Applications Application properties - Event sheets - Layouts - Global variables - Resources - Transitions
Event Sheets Events - Event groups - Sub-events - Python scripting
Layouts Layout properties - Layers
Layers Layer properties - Objects
Objects Common properties - Attributes - Behaviors - Containers - Effects - Families - Private variables
Personal tools