Jump to content

LSL vs C# simple examples


Beowulf Zessinthal
 Share

You are about to reply to a thread that has been inactive for 2589 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

When i first learnt to script (in LSL), i thought i had fallen into heaven :) 

So logical, so powerful .. ahh.. but soon in Sansar the scripting language will be C#.

Im excited for the chance to see some comparative LSL vs C# examples.

I will post a basic LSL script here.. would someone show me a reasonable C# version?

Then maybe folk who already know both can post a few other examples?

Title what the script does: 

LSL: Switch alpha values of object using a timer


float alpha;

default
{
    state_entry(){
    alpha= 1.0;
    llSetAlpha(alpha, ALL_SIDES);
    llSetTimerEvent(2);
    }

    timer(){
        if(alpha == 1.0){
        alpha= 0.4;
        }
        else{
        alpha= 1.0; 
        }
    llSetAlpha(alpha, ALL_SIDES);
    }
}

 

Link to comment
Share on other sites

It would look like something like this in C#:

float alpha= 1.0f;

public void default_event_state_entry()
{
    this.alpha = 1.0;
    this.llSetAlpha(alpha,ALL_SIDES);
    this.llSetTimerEvent(2);
}

public void default_event_timer()
{
    switch( alpha )
    {
        case 1.0:
            alpha = 0.4;
        break;
        default:
            alpha = 1.0;
        break;
    }
    this.llSetAlpha( alpha,ALL_SIDES);
}

For larger project C# will be superior and easier to code and control. The most notable difference in the above sample is the definition of default_event_state_entry, where you have to create event handlers for the basic events touch_start etc. Second the switch statement makes handling if-statements much easier.

Compared to LSL there will be a little more code to write, but IMHO this is a small price to pay for much better programming control and logic.

Link to comment
Share on other sites

I wonder if you'll find that the LSL/C# language differences will be dwarfed by the differences in simulation and scripting architecture between SL and Sansar. Most of the time you spent learning LSL was not in learning the language, but rather how the simulator, event system, physics, communications, UI, camera and other SL bits operate, and how the LSL function library lets you work them.

  • Like 1
Link to comment
Share on other sites

Thats true Madelaine ..  but as i cannot get in yet i am getting better acquainted with the things i will need to know.

So later i will have less frustration^^

C# seems very doable after LSL .. so far only 1.5 days spent, but already im optimistic^^

Over the next few days i will watch a few youtube tutorials too. 

Bring it on baby!

Edited by Beowulf Zessinthal
Link to comment
Share on other sites

  • 1 month later...
  • Lindens

C# in sansar will be a little more complicated to write, but also a bit easier to use. For example instead of configuration notecards, public variables are shown in the object's properties - right next to the rest of the object properties.  The current API is pretty limited, and of note for your example there is no interface yet to the visual components of an object. So these examples are somewhat theoretical at this time. However except for VisualObject and its methods the below is how your script could look in Sansar.

First is probably the closest to how LSL lays things out:

// You have to include the libraries you want to use in C#
using LindenLab.ServerScript;
using System;

// All scripts are a class that extends one of a selection of base classes.
// The base classes decide what APIs are available and where the script could work.
// This one works on an object. Some might be agent or scene(region) specific.
public class AlphaChanger : ObjectScript
{
	// Public fields in the base class get shown in the UI when editing
	public float HighAlpha = 1.0;
	public float LowAlpha = 0.4;

	public override void Init()
	{
		// Init is the place to set up callbacks and events.
		// There is a timer class, so here is one quick option.
		// Note that ChangeAlpha could be any method we create that takes a TimerData
		// Thus multiple timers are easy
          Timer.Create(TimeSpan.FromSeconds(2), ChangeAlpha);
	}

	public void ChangeAlpha(TimerData data)
	{
		// Hypothetical VisualObject is an interface to visual object APIs
		// This one has a hypothetical property to get and set the alpha
		if (VisualObject.Alpha == HighAlpha)
		{
			VisualObject.Alpha = LowAlpha;
		}
		else
		{
			VisualObject.Alpha = HighAlpha;
		}
	}
}

This is how I would probably actually write it in C#, but it is more dense and a little harder to read.

// You have to include the libraries you want to use in C#
using LindenLab.ServerScript;
using System;

// All scripts are a class that extends one of a selection of base classes.
// The base classes decide what APIs are available and where the script could work.
// This one works on an object. Some might be agent or scene(region) specific.
public class AlphaChanger : ObjectScript
{
	// Public fields in the base class get shown in the UI when editing
	public float HighAlpha = 1.0;
	public float LowAlpha = 0.4;

	public override void Init()
	{
		// Init is the place to set up callbacks and events.
		// This is a bit more dense, but C# supports anonymous methods inline so for simple actions on events this might be “easier”.
          Timer.Create(TimeSpan.FromSeconds(2), data => VisualObject.Alpha = (VisualObject.Alpha == HighAlpha) ? LowAlpha : HighAlpha);
	}
}

Lastly here is an overly fancy way to do it, that is overkill but some example of the power in Sansar's C#:

// You have to include the libraries you want to use in C#
using LindenLab.ServerScript;
using System;

// All scripts are a class that extends one of a selection of base classes.
// The base classes decide what APIs are available and where the script could work.
// This one works on an object. Some might be agent or scene(region) specific.
public class AlphaChanger : ObjectScript
{
	// Public fields in the base class get shown in the UI when editing
	public float HighAlpha = 1.0;
	public float LowAlpha = 0.4;

	public override void Init()
	{
		// Init is the place to set up callbacks and events.
		// We support coroutines, which are like mini scripts all themselves which can
		// be paused or wait for events independently of the rest of the script.
		// If you are doing something more complex than a toggle these can be a lot easier to read and write.
		StartCoroutine(ChangeAlpha);
	}


	public void ChangeAlpha()
	{
		// <side note here that this is overkill for this example, but is cool so I wanted to show it>
		while (true)
		{
			// Hypothetical VisualObject is an interface to visual object APIs
			// This one has a hypothetical property to get and set the alpha
			if (VisualObject.Alpha == HighAlpha)
			{
				VisualObject.Alpha = LowAlpha;
			}
			else
			{
				VisualObject.Alpha = HighAlpha;
			}
			
			// This will wait 2 seconds before continuing this loop. The rest of the script is not stopped.
			// For events there is also WaitFor(<event>) which would pause just this method until the event happens.
			Wait(TimeSpan.FromSeconds(2));
		}
	}
}

 

  • Like 7
Link to comment
Share on other sites

Yes, and notice the usage of unity subroutines. Although this is SL/LSL, looking forward to a lift of the curtain to much more!

using LindenLab.ServerScript.Secrets&Things2Come.FromKelly:D

Link to comment
Share on other sites

  • 3 weeks later...
Quote
On 4/29/2017 at 5:51 AM, Kelly Linden said:

... For example instead of configuration notecards, public variables are shown in the object's properties - right next to the rest of the object properties.  The current API is pretty limited, and of note for your example there is no interface yet to the visual components of an object.

That's seriously cool

Even more cool when/if there will be a interface to visual components where class object public properties can be set at design time by the object owner. I can see an explosion of user -created components taking Sansar by storm should this be the case

 

 

 

  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2589 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...