-
Notifications
You must be signed in to change notification settings - Fork 6
Running scripts
Scripts are run by IronPython 2.7.5 engine, which is an implementation of Python 2.7.
The mod can run scripts embedded in machine save file or external scripts in .py files. You can choose between script sources through Script Options window (Ctrl+U).
-
Script file
Use this input field to select which script file you want to run. You can enter absolute or relative path. Extension can be omitted. The checkmark shows you if the script file has been found. -
Script source
Use these toggle buttons to choose between running code from machine save file (.bsg) or external script file (.py) -
Import
Toggle this to embed the script file in the machine file. Code gets saved next time when you save the machine. -
Export
Use this to save embedded code to a script file for editing. Checkmark shows you if machine currently contains embedded code.
The scripts starts executing at the start of simulation and ends at the end of it. If you do not want scripts executing, turn off the toggle in the settings menu.
At the start of the simulation, the script gets compiled and executed.
Following functions are later called by the mod:
-
Update
Gets called at every frame of the simulation. Use this for reading input and manipulating blocks. -
FixedUpdate
Gets called at every frame of the simulation at a fixed rate. Use this if you need your program to run independent of in-game timescale slider or framerate.
Example script:
wheel = Besiege.GetBlock("WHEEL 1")
direction = 1
wheel.SetToggleMode("AUTOMATIC", True)
def Update():
""" updates the speed on every frame """
global direction # direction variable is defined in global scope
# read input
if Input.GetKey(KeyCode.U):
speed = 1 # set speed to 1 if U is pressed down
else:
speed = 0
if Input.GetKeyDown(KeyCode.I):
direction *= -1 # invert direction when I is pressed
wheel.SetSliderValue("SPEED", speed * direction)In the main script scope the mod automatically imports the following modules:
import clr # Common Language Runtime to call .NET members
clr.AddReference("System")
clr.AddReference("UnityEngine")
from UnityEngine import Vector2, Vector3, Vector4, Mathf, Time, Input, KeyCode, Color"
clr.AddReference("LenchScripterMod")
from LenchScripter import Functions as BesiegeYou can import additional modules from Common Language Runtime or Python built-in or external modules yourself, but note that the above modules will not be loaded in the imported scopes. If you want to access them from other modules imported by the script, you will have to import them yourself.
I recommend using built-in Python modules like math instead of CLR modules like UnityEngine.Mathf as you might have problem with method overloads.
Tutorials
Guides to scripting from basic to advanced.
Installation
Required file structure.
Running scripts
Get started; how to run scripts and how to import / export them.
Block identifiers
How to select blocks.
Block handlers
Manipulating blocks.
Functions
All available functions.
Property identifiers
How to select block properties.
Watchlist
How to debug your script.
API
How to integrate with your mod.
Console commands
Mod configuration and Python console commands.
Fueled by coffee


