16 June 2012

CMT3313 - Week 14 Post - Second Life - Building and Scripting a Door

Hello there and yes unfortunately its about Second Life again but at least today is about the part of second life which interested me, so here goes.

I decided that I wanted to create a door which an avatar could open and close buy touching it, so I set off on building a wall with a door in the middle.

First step to building a door

My next step was to create a hinging point for my door as by default the rotation point of any object is the middle and this is not how a door actually works is it?

Adding the hinging point
Now to join these two objects together in Second Life, you must 'Link' them together by holding down the shift button and selecting the two (or more if you like) objects and finally click Ctrl + L to link them.
It's important to keep in mind that the last object you select will be the root object, therefore I selected the hinging point as the root.

So now I've got a wall with a door in the middle set up with a hinging point, true it's not the nicest looking thing you've ever seen but I decided to leave the looks for the end.
To test if I've done this correctly I first tried changing the rotation of the door via the Building Tools window, by setting a -90 degree value in the 'Z' parameter of the rotation fields and to my surprise...it actually worked!!
No, I'm lying, I had to try it about 5 times until I got it right...which is quite frustrating. But eventually it worked and this is how it looked like:

Manually opened door
But now I wanted to create a script which does this every time a user touches the door, so after doing some research I learnt that you must do this by using the rotation variables of an object.

Every object has 4 rotation values; X, Y, Z and S but to make a door open all I needed to use were the Z and S values.
So I started out by retrieving it's Z and S rotation positions. I did this by writing a script which would get these values and output them on screen every time the object is touched.



The above script is what I'm taking about, it get these values by using the method 'llGetLocalRot()', this method returns an object of type "rotation", which contains all 4 values which I have previously mentioned, then it uses the method 'llSay()' to output the values I require on the screen using channel 0 (zero).

Back to getting those all important rotation values, I close the door once again and touched it to output it's Z and S values like so:

Rotation Variables
I also did this for when the door is opened, so that I know how much I should open it and wrote these values done for now.
Then I re opened the object's script, removed the entry state as I don't need it and left the touch_start state. in here I used an IF statement which would check if the door is closed and open it. If the door is opened instead it closes it by setting the object's rotation variables to the ones I had written down by using the method 'llSetLocalRot()' and passing it the rotation variable which I modify.

Open-Close Door Script
After playing around with some textures this is the final result:

Finished opening & closing door
Conclusion
I must admit I don't quite think the LSL scripting language is one of the best scripts out there but at least with a few lines of code you can visually see things move around or for my last post's case, send an email with by simply using 'llEmail' this fact forces me to admit that I kind of enjoyed doing this today.

2 June 2012

CMT3313 - Week 13 Post - Second Life...Not necessarily better than the first one

In my previous blog I skimmed over what Second Life is, today I am dedicating this post on the subject, what I like and what I don't like about it.



Fist thing that came to mind when I heard about Second Life was...WHY?! What's wrong with my first life? Why would I ever want to take part in a community of a bunch of complete losers and what's with the hole role playing?
Then I learnt that people actually sell land, clothes, accessories and appearances with real money!!! This further pushed me away from Second Life and the people who hang out there.
But I must admit, I already hated Second Life before I had even laid eyes on it and this blinded me from seeing what an accomplishment Second Life really is and that's from a technical point of view only!

I mean the the developers of this thing managed to create a whole virtual world in which subscribers can roam around and build things.
Yes, they enabled any user to build and program things to do anything they very well please and this is probably the only thing I liked about Second Life.

Scripting
So off to the interesting part from now on. To code your own objects you must use the Linden Scripting Language or LSL in short. This scripting language was created specifically for Second Life, it's based on Java and C programming languages and is a server side scripting language.

Now one must keep in mind that LSL is very state and event driven, meaning you do not create objects which have attributes and methods, instead you use states which contain one or more events.
States resemble real life scenarios, as in an engine can be on or off and the same goes for Second Life. By default every object you create has one state and that's "default", but you can create more states and change from one to the other.
Events on the other hand are just like event handlers in other programming languages. These are pre-defined and are triggered by a specific action, such as on touching the particular object, the "touch_start" event is triggered.



But enough about the theory and lets move to my first attempt at building an object and scripting it.

My go at Second Life Scripting
After I spent a few minutes having a go at scripting a twisty box, I managed to make my object send me an email when ever it got touched and this got me excited just like a little boy in a candy shop!


So here's the script I used, nothing fancy and I haven't gotten into using multiple states yet, but it's a start.


default
{
    state_entry()
    {
        llSay(0, "Hello, Avatar!");
    }

    touch_start(integer total_number)
    {
        llEmail("grechClifford@gmail.com","Box Object Touched","Object has been touched");
        llSay(0, "You Touched ME! - Sent Email to Owner!.");
        
    }   
}


As you can see, the "touch_start" event first uses the "llEmail" function which sends me an email telling me that my object has been touched and then uses the "llSay" function to display a message to the user.


As for the result, I immediately receive the email displayed below:


Conclusion
So, my final verdict on Second Life...I still hate it as it is still filled with creeps posing as attractive women, but at least there is something which sparks my interest. Although I would also like to say that I am not planning to play this game very often apart for my degree purposes.