← Back to articles

Pure C# support

The ScriptEngine now mixes LSL and C# in the same script — and if a script starts with //C#, the LSL preprocessor is skipped entirely for 100% pure C# support. A few naming rules apply for events.

Tedd

ScriptEngine supports both LSL and C# within the same script. Mainly this means that you can write LSL, but when you want you can use C# code. For example instead of using llToLower(mystring) you can use mystring.ToLower(), instead of llStringLength(mystring) you can use mystring.Length().

But to make sure that we support pure C# code, I’ve added support for forcing the compiler to not use LSL preprocessor. This means skipping directly to compile without going through LSL to C# converter.

Any script that starts with the 4 letters “//C#” or //c#" will be compiled as C# directly. This ensures that we have 100% C# support.

There are a few rules you need to follow. Mainly:

  • Any LSL event needs to be renamed to event.
    For example “start_entry()” in default state will be “public void default_event_state_entry()”
  • LSL events needs to be public, of course.
  • Script needs to start with “//C#” without the quotes and without space in front of it. These 4 characters needs to be the first thing inside the script!
  • You still have to use ll-commands for stuff that native C# doesn’t support. But don’t worry - the ll-commands are just the name, you are actually writing in C#. ;)

If you put a LSL script into an object in OpenSim, the C# code of that script will be debug-logged to the "bin\\ScriptEngine\"-folder. So if you have any trouble, just write up a LSL code, put it into an object and you have a working C# script that does the same thing.

Untested example:

//C#

namespace SecondLife
{

public class Script : OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass
{

public Script() { }

// Your script goes here!
int touch_count = 0;
int tick_count = 0;
int objnum = 1;

public void default_event_state_entry()
{
llSay(0, “Hello, Avatar! Starting timer…”);
llSetTimerEvent(10);
}

public void default_event_touch_start(int total_number)
{
touch_count++;
llSay(0, "Object was touched. Touch count: " + touch_count);
}

public void default_event_timer()
{
tick_count++;
llSay(0, "Timer count: " + tick_count);
}
}
}

Written by Tedd · Oslo, Norway

Follow the journal via RSS