2

The Basics of Using Iron Python With TCAdmin

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 Basics

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.

Script.WriteToConsole('Hello World')

Accessing TCAdmin Variables

There are 2 ways to access the value of variables depending on the type. Both are accessed via the ThisService object

Service Variables

Example: IP Address and Game Port

ThisService.IPAddress

To write the assigned IP Address to the console you can do

Script.WriteToConsole('IP Address: ' + ThisService.IpAddress)

Custom Variables

Example: A custom variable you create on a game package

ThisService.Variables['VariableName']

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

System.IO.File.Exists('test.txt')

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.

matt

2 Comments

  1. I am looking to find a way for me to import my sql file into my arma 3 database that is created via ironpython. I have no experience with python, just a little bit with php and java. I have the script from http://help.tcadmin.com/Automatically_Create_a_MySQL_Database which i already added to the game setup.
    i do not quite understand what variables are with python. when you have
    ThisService.WorkingDirectory where are these types of variables located for tcadmin?
    like for example: If i wanted to import an sql file located at:
    C:\TCAFiles\Games\arma3\TCA.Mods\@ExileServer-0.9.6\MySQL
    how would i put this into the python script?
    exileimport=Path.Combine(ThisService.RootDirectory, “exile.sql”);

    and is it possible that the sql file would import when a mod is added? If so how would one accomplish this? I have a feeling i would need to set the script to External Download Script option based on the info from this page. http://help.tcadmin.com/Custom_Scripts.

    here is how i would like the scenario to go:
    I have 1 game folder called arma3 with
    TCA.Mods folder with all my arma 3 mods zipped.
    then i have different game service templates setup, 1 for epoch and 1 for exile
    then i add the different mods. when the service is installed it will also install those selected mods and execute the script to import the sql file before starting up the server.

    I see there is a custom scripts icon under each mod which i can place the script there to import the database when its enabled. I just do not know how to go about actually importing the sql file into said users database.

    any pointers you can share with me to accomplish this?

    thanks for your time.

  2. To your variable question, those are built-in TCA variables, I’m just showing how to access them. You can find most of the default variables here: http://help.tcadmin.com/ThisService

    Another issue you will face, when you select the “installed by default option” for a mod, none of it’s scripts are executed when the service is created. This means if you have Epoch set to be installed by default with a custom script to setup the database, when the new service is created the database setup script is never used. The custom scripts are only executed if a user clicks Install on a mod for an existing service.

    I’m not sure I understand what you want the end result to be. Are you saying that you want to have separate game packages for Epoch and Exile that use a 3rd package called Arma 3 as the template?

Leave a Reply

Your email address will not be published. Required fields are marked *