Pure C# support

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 <STATE>_event_<EVENTNAME>.
    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);
      }
   }
}

1 thought on “Pure C# support”

  1. This is excellent! Your C# code looks almost exactly the same as the CIL we generate for LSL scripts, down to the name mangling of event handlers and library call interface. I was planning to keep the library call interface, but implement something closer to a normal C#/.NET interface for event handling via virtual methods or delegates for C# scripts. We should definitely work on maintaining compatibility here.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: