This site is in no way affiliated with or endorsed by Linden Labs
Second Life® and Linden Lab® are trademarks or registered trademarks of Linden Research, Inc. All rights reserved. No infringement is intended.
Site Map
Messages in LSL
Copyright © 2007 Ian Monstre · All Rights reserved
All of the "If (gap_position == xx)" statements in the finding_gap state are figuring out which tiles need to move based on the position of the gap.
Before we explain how the script works, take a look at the script that is contained in every tile
Why have two scripts?
Both of these scripts communicate with each other by passing messages back and forth. You might wonder why we chose this way of doing things - after all, wouldn't it make sense just to have one big script that's stored in the frame (for example), and do everything from that?
Well actually there's a good reason why you can't do that: In Second Life scripting, you can't make one object move another. For example, the script that's running in the frame couldn't move any of the tiles. It could move itself (which isn't very useful) but it can't move any of the tiles. Instead, it has to send a message to the tile it wants to move. When the tile receives the message it can act upon it by issuing an llSetPos command.
We do this by passing a value in one of the parameters when we make the call - in this case we've defined a constant called SET_POS in both scripts to do this. When this parameter is received, the tile script uses another parameter to figure out where to move itself to. There are only 9 possible positions we need to move any tile to, so we simply hard-code where the tile needs to move itself to for each of these 9 positions and issue an llSetPos command appropriately.
Back to the Frame script
Let's take another look for the script for the frame. As mentioned previously it makes use of LSL's "state" facility. For example, the script may be in a state where it's waiting for a tile to tell it that it has been touched and needs to move.
The script starts by waiting for a tile to report that it has been touched. As soon as that happens the script sends a message to the first tile (ie the tile with the number "1" on it) and moves into a state where it's waiting for every tile to "check in" by reporting its current position.
If you take a look at the tile script you'll see that when it has reported its position it sends another "REPORT_POSITION" command to the next tile. For example, as soon as Tile 1 has reported its position to the frame, it sends a "REPORT_POSITION" command to Tile 2 and so on.
One all 8 tiles have reported in with their positions, the script moves into a "finding_gap" state. In this state it calculates the position of the gap given that it now knows exactly where all of the tiles are. And based on that information it can work out which tiles need to move to which positions and sends SET_POS commands to the appropriate tiles accordingly.
And that's it!
It's as simple as that. The rest of the script is really just implementation detail. Feel free to make improvements to this, after all it was my first dabble in the use of messaging in LSL from when I first joined Second Life. I'm sure there are other ways to achieve the same effect. I'm also keen to see how more complicated games could be constructed using a similar technique - for example a checkers game (or draughts game if you prefer), or even Chess if you're feeling ambitious.
Copyright © 2007 Ian Monstre · All Rights reserved
A number of people have asked me about the "message passing" feature in the SL scripting language (LSL). I find it's one of those things you need to play around with to get the hang of, which is exactly what I did when I started scripting in Second Life. I built a sliding puzzle - of the type generally known as a "15 puzzle" - that presents a number of tiles in a frame which you can slide around in order to show a picture or series of numbers.
In this case, I used just 8 tiles in a 9-tile frame, that ended up looking a bit like this:
As you click on any tile it slides towards the gap.
So I thought it worth sharing the code from this object as a way of showing how you might use messaging in Second Life. I may well create a video that explores messaging in more depth if people are interested.
In the interim though, here is the code for my sliding puzzle.
How the object is constructed
The actual object is pretty simple. It consists of 9 prims:
The frame contains the script that actually controls the whole object. It sends messages to the tiles telling them to move, and asking for a position update. The script within each tile is identical ie it's one script placed into each of the eight tiles (but different to the script on the frame obviously).
The Scripts
Let's take a look at what we need to do in the controlling frame. The script that controls the object will always be in one of three states:
Take a look at each of these states listed in the code:
| · |
One large cube with the middle hollowed out. This serves as a frame that contains the tiles |
| · |
Eight flattened cubes that represent the tiles. Each has a texture on the front face showing the numbers 1 to 8
|
| 1. |
Waiting for a tile to send a message to it saying "I have been touched, I need to move" (State_Entry) |
| 2. |
Waiting for all of the tiles to send back a report of their current position (Awaiting_checkin) |
| 3. |
Figuring out which tiles need to move and sending a message to them appropriately requesting them to move (Finding_gap) |