Friday, September 14, 2007

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);
      }
   }
}

4 comments:

Anonymous said...

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.

MH said...

This doesn't work for me.
The only error I receive is "The given key was not present in the dictionary"

I almost deleted all the lines and just wrote this code:

//C#
namespace SecondLife
{
}

same error.
However the following code was saved successfully:
//C#
public class MyClass
{
}

Unknown said...

Hi MH

This blog post is 3 years old, contains the phrase "untested example" and was more a conceptual drawing of what we were implementing at the time. There may be some changes in current code. Either way, you should have a look at a more recent place than this blog. :)

Unknown said...

Try this: http://opensimulator.org/wiki/Scripting_Documentation