Iron Python is a powerful tool included with TCAdmin, but seems to get very little use. However, taking the time to learn how to leverage it gives you access to many things you cannot accomplish with batch scripts.
Here I’ll cover some of the information you need to get started. This does expect you understand how to use Python.
The version of Iron Python included with TCAdmin is crippled. It does not come with the standard library. Due to this many common modules, such as OS, are not available. However, this limitation is easy to workaround since you have access to most .NET APIs.
The MSDN page outlining the .NET classes is very useful, especially if you have little experience with .NET
Also note that TC Admin uses .NET 2.0. Due to this features added after 2.0 are not available.
Lets get started.
First, create a Custom Script for any game package, set the Event to Custom Icon and Script Engine to Iron Python.
Writing To The Console
The most basic thing you can do is output to the web console when the button is pressed.
Accessing TCAdmin Variables
There are 2 ways to access the value of variables depending on the type. Both are accessed via the ThisService object
Example: IP Address and Game Port
To write the assigned IP Address to the console you can do
Script.WriteToConsole('IP Address: ' + ThisService.IpAddress)
Example: A custom variable you create on a game package
In this case you replace VariableName with the name of the actual variable you are trying to access
Set Custom Variables Value
You also have the ability to set custom variable values directly from your script.
ThisService.Variables['VariableName'] = 'Some Value' ThisService.Save()
In this example we the variable’s value to Some Value. We then call Save() on ThisService which saves any changes we have made.
Using .NET Classes In Your Script
Much of the power of using Iron Python in TCAdmin is the ability to directly access .NET classes.
To do this you must import the class just as you would a Python Module. Once you do this you use it just as you would any other class in Python.
For this example I’ll use the File class which is part of the .NET System.IO namespace. I will check if a file exists and create it if it does not
from System.IO import File if not File.Exists('test.txt'): File.Create('test.txt')
The way you import can also be helpful. You can import the entire System.IO namespace. However, doing so creates much more type for you. To access file you would need to use
By importing only the classes you need it becomes much easier to manage.
Dealing With The Working Directory
When you create batch scripts, they execute from the service’s root directory. This is not the case for Iron Python scripts. They execute from the Monitor’s root directory. This means you need to use the full path to reach anything in the game service’s directory.
This is a bit of a pain when using Iron Python and creates a fair bit of extra typing.
While you can change the current working directory via your script, DO NOT DO IT. This will also change the CWD for the Monitor causing it to crash.
I normally do the following to cut down on the boiler plate needed for accessing files in the game service’s directory.
from System.IO import Path, File rootdir = ThisService.WorkingDirectory ex_file = Path.Combine(rootdir, 'test.txt') if not File.Exists(ex_file): File.Create(ex_file)
Here I assign ThisService.WorkingDirectory to a shorter variable name. This will cut down on a lot of typing in large scripts. I then use Path.Combine() to create an absolute file path using the root directory and the name of the file.
In the previous example, it would have created test.txt in the Monitor folder. In this example it will create it in the game service’s directory.