Alife Virtual School - Class 10: NPC and Pathfinding with LSL
NPC and Pathfinding with LSL — Free class in Alife Virtual School
What You Will Learn
This class will give you the practical skills to create intelligent, moving characters. Upon completion, you will be able to:- Understand the core concept of Pathfinding and the Navmesh in Alife Virtual.
- Create a basic Non-Player Character (NPC) from a prim object.
- Use the
llNavigateTo()function to command an NPC to a specific destination. - Use the
llPatrol()function to create a multi-point patrol route for guards or guides. - Use the
llWander()function to make characters roam naturally within a defined area. - Handle pathfinding events to control character state and animations.
- Troubleshoot common pathfinding issues.
Prerequisites
This is an advanced class. Before you begin, you should be comfortable with the following:- A solid understanding of LSL fundamentals (variables, functions, events, and states), as covered in our earlier classes.
- Basic building skills: creating, editing, and linking prims.
- A registered Alife Virtual account. If you don't have one, you can register for free and get started in minutes.
- Access to land where you have script permissions. The best place to practice is your own **free 65,536 sqm private island**, which you can claim after registration.
Pathfinding in Alife Virtual: A Step-by-Step Guide
Pathfinding is the system that allows an object to intelligently find a path from point A to point B, avoiding obstacles along the way. It doesn't just move in a straight line; it "thinks." This is made possible by an invisible map called a **Navmesh** (Navigation Mesh) that the Alife Virtual server generates for all land.Pro Tip: Visualizing the NavmeshNow, let's get our hands dirty and build our first NPC.
You can see the Navmesh in-world! Using the **Firestorm Viewer** (the same powerful viewer used for Second Life), press Ctrl+Alt+Shift+P. This toggles the Pathfinding visualization layer. You'll see the walkable areas covered in a colorful mesh. This is an invaluable tool for debugging why your NPC might be getting stuck!
Step 1: Creating Your First Pathfinding Character
Before an object can use pathfinding functions, you must tell the server to treat it as a "character."- Rez Your NPC: Go to a clear spot on your island. Right-click the ground and choose Create. Click to rez a simple Cube prim.
- Name Your NPC: In the Edit window, under the General tab, name your cube "Wanderer."
- Create the Script: Go to the Content tab and click New Script. A new script named "New Script" will appear. Double-click it to open the LSL editor.
- Add the Character Code: Delete the default code and replace it with this:
default { state_entry() { // This is the most important line! // It tells the server to treat this object as a pathfinding character. llSetCharacterType(CHARACTER_TYPE_NONE); } } - Save the Script: Click Save. Your prim is now officially a pathfinding character! It won't do anything yet, but the foundation is laid.
Step 2: The Single Destination - llNavigateTo()
The simplest pathfinding command is llNavigateTo(). It tells your character to go to one specific spot. This is perfect for having a character walk to a user who clicks them, or move to a designated workstation.
Let's modify our script to make the cube move 10 meters in front of its starting position when touched.
- Open Your Script: Open the script inside your "Wanderer" cube.
- Modify the Code: Update the script to look like this:
// Store the target position in a global variable vector gTargetPos; default { state_entry() { llSetCharacterType(CHARACTER_TYPE_NONE); llSay(0, "Ready! Touch me to start navigating."); } touch_start(integer total_number) { // Calculate a target 10 meters in front of our current position gTargetPos = llGetPos() + <10.0, 0.0, 0.0> * llGetRot(); llSay(0, "On my way to " + (string)gTargetPos); // The main command! Options are an empty list for now. llNavigateTo(gTargetPos, []); } path_update(integer type, list info) { // This event fires with status updates about our journey if (type == PATH_TARGET_REACHED) { llSay(0, "I have arrived at my destination!"); } else if (type == PATH_TARGET_UNREACHABLE) { llSay(0, "I can't get there from here."); } } } - Save and Test: Save the script. Now, touch your cube. You should see it smoothly travel 10 meters forward, navigating around any obstacles you place in its path.
path_update event is crucial. It's how your script knows if the character has arrived, gotten stuck, or if a new path needs to be calculated.
Step 3: Creating a Patrol Route - llPatrol()
What if you want a character to follow a set route, like a guard patrolling a castle wall or a docent moving between exhibits? For this, we use llPatrol().
This function takes a list of coordinates (vectors) and cycles through them.
- Set Up Markers: Create 3 or 4 small, simple prims (like small spheres) and place them around your land where you want your patrol points to be. Name them "Marker 1", "Marker 2", etc., for clarity.
- Create a New Script: Create a new script in your "Wanderer" NPC. This script will be more complex.
list gPatrolPoints; // A list to hold our patrol coordinates default { state_entry() { llSetCharacterType(CHARACTER_TYPE_NONE); // Scan for our markers within 96 meters // NOTE: You must name your markers "Patrol Marker" for this to work. llSensor("Patrol Marker", "", ACTIVE | PASSIVE, 96.0, PI); } sensor(integer num_detected) { // We found our markers! Let's get their positions. llSay(0, "Found " + (string)num_detected + " patrol markers. Building route..."); // Loop through all detected markers and add their position to our list integer i; for (i = 0; i < num_detected; i++) { gPatrolPoints += llDetectedPos(i); } // Start the patrol! // The option "PATROL_FLAG_CYCLE" makes it loop forever. llPatrol(gPatrolPoints, [PATROL_FLAG_CYCLE]); } no_sensor() { llSay(0, "Error: Could not find any prims named 'Patrol Marker'."); } path_update(integer type, list info) { if (type == PATH_TARGET_REACHED) { // The 'info' list contains the index of the point we just reached integer point_index = llList2Integer(info, 0); llSay(0, "Arrived at patrol point #" + (string)(point_index + 1)); } } } - Prepare and Run: Before saving, go back to your marker prims and rename them all to exactly "Patrol Marker". Now, save the script in your NPC. It will automatically find the markers and begin its patrol.
A World of Possibilities: Think about what you can do with this on your **free 65,536 sqm island**! Create a tour guide that stops at points of interest, guards that patrol the perimeter of your base, or delivery bots that move between stations. Since Alife Virtual offers **free mesh and texture uploads**, you can make these characters look incredibly detailed without worrying about upload fees like you would in Second Life.
Step 4: Wandering Freely - llWander()
Sometimes, you don't want a character on a strict path. You just want them to meander around an area to create ambiance, like animals in a forest or guests at a party. This is where llWander() is perfect.
This is the simplest pathfinding function to use.
- Create a New Script: Let's use a fresh script in our "Wanderer" for this example.
- Add the Wander Code:
default { state_entry() { llSetCharacterType(CHARACTER_TYPE_NONE); // Start wandering within a 20-meter radius of our start point. // We'll use an empty list for options. llWander(20.0, []); llSay(0, "Time to wander around..."); } // path_update is not strictly necessary for simple wandering, // but it's good practice to include it for debugging. path_update(integer type, list info) { if (type == PATH_TARGET_UNREACHABLE) { // If it gets stuck, tell it to try wandering again. llSay(0, "Hmm, got stuck. Finding a new spot."); llWander(20.0, []); } } } - Save and Observe: Save the script. Your NPC will now move to random points within a 20-meter circle around its starting location, creating a very natural, lifelike effect.
Common Mistakes and How to Avoid Them
Pathfinding can be tricky. Here are some common problems and their solutions:- "My NPC won't move at all!"
- Did you forget
llSetCharacterType()? This is the most common error. This function MUST be called in thestate_entryevent. - Is the ground traversable? If your land is set to no-access or covered in phantom prims, the NPC will have nowhere to go. Check your parcel flags.
- Is the destination valid? Trying to navigate to a point high in the sky or under the terrain will fail.
- Did you forget
- "My NPC walks through walls or objects!"
- This usually means the object is not part of the Navmesh. Make sure the object is set to "Prim" physics shape type (in the Features tab of the edit window), not "Convex Hull."
- The Navmesh may need to be updated. This happens automatically, but on rare occasions, you may need to force it. Moving a large object on the parcel and then moving it back can trigger a rebake.
- "I get a script error: 'path_update not found in state default'."
- Your script is receiving pathfinding updates from the server, but you haven't written a
path_update()event to handle them. You must always include at least a basicpath_update(integer type, list info) {}block in any script that uses navigation functions.
- Your script is receiving pathfinding updates from the server, but you haven't written a
Advanced Tips and Tricks
Mastered the basics? Try these advanced techniques.- Animated Characters: The real magic happens when you combine pathfinding with animations. Use
llStartAnimation()andllStopAnimation(). A common pattern is to start a "walk" animation when you call a movement function, and stop it (or start an "idle" animation) in thepath_updateevent when you receivePATH_TARGET_REACHED. With **free animation uploads** in Alife, you can build a whole library of custom movements. - Dynamic Following: Use
llSensor()to detect a nearby avatar, get their position withllDetectedPos(), and feed that position intollNavigateTo(). By placing this in atimerevent, you can make an NPC that follows you around. - Complex Behavior with States: For a truly intelligent NPC, use LSL states. You could have an
idlestate, apatrollingstate, and afollowing_playerstate. The NPC can switch between these states based on sensor detections or chat commands. - Refining Movement with Options: The second argument in the pathfinding functions is a list of options. For example, using
[PURSUIT_OFFSET, <1.0, 0.0, 0.0>]will make the NPC try to maintain a 1-meter offset from its target—perfect for a companion who walks beside you instead of on top of you.
Practice Exercise: Your Island Tour Guide
It's time to put your new skills to the test! Your mission is to create a simple automated tour guide for your free private island.- On your island, identify 3-4 points of interest. It could be a cool building, a nice view, or just a spot you like.
- At each spot, rez a small, invisible prim. Name them all "Tour Stop".
- Create your tour guide NPC. You can use a simple prim, or get creative and use the **free full-body mesh avatar** and outfits provided to all Alife Virtual members!
- Write a script for your NPC that uses the
llSensor()andllPatrol()method we learned in Step 3 to find the "Tour Stop" markers and move between them in a cycle. - Bonus Challenge: In the
path_updateevent, when your NPC reaches a tour stop (PATH_TARGET_REACHED), usellSay()to have it announce something about that location!
Frequently Asked Questions (FAQ)
- Can I use a full mesh avatar as an NPC instead of just a prim?
- Absolutely! The standard method is to build your avatar, link all the parts with the root prim being the main body, and place the pathfinding script inside that root prim. The entire linked object set will then move as one character.
- How much do pathfinding scripts impact server performance (lag)?
- Pathfinding is a server-intensive process, but the LSL functions are highly optimized. A few NPCs on a region are perfectly fine. Performance issues can arise if you have dozens of characters all pathfinding simultaneously in a small area. Your **10,000 prim** allowance is generous, but always be mindful of performance. Start small and scale up.
- Why won't my NPC walk across water?
- The Navmesh is generated on terrain and solid, non-phantom objects. It does not generate over open water. To have an NPC cross water, you must build a physical path for it, such as a bridge or a series of invisible prims for it to walk on.
- Are the pathfinding functions in Alife Virtual different from Second Life?
- No, they are 100% identical. Alife Virtual is committed to **full LSL scripting support**, meaning every function, event, and constant you learn here is directly transferable. The incredible advantage you have in Alife Virtual is the freedom to learn and build without the financial pressure of land fees or upload costs that you'd face elsewhere.
Summary and Next Steps
Congratulations! You've just unlocked a powerful new dimension of creation in Alife Virtual. You've learned how to turn static objects into living characters using the three core pathfinding functions:llNavigateTo()for single destinations.llPatrol()for repeating routes.llWander()for natural, ambient movement.
Ready to Build Your World?
You've just learned an advanced skill that professional virtual world developers use every day. You have the knowledge, and in Alife Virtual, you have the tools and the space to bring your vision to life. Join our vibrant community of over **1,148,000+ members** who are building, scripting, and exploring every day on a platform built for creators. It's time to stop dreaming and start building. Register your free account today and claim your very own, permanent **65,536 sqm private island—absolutely free, forever.** No monthly fees, no hidden charges.Register for Alife Virtual for FREE
Claim Your FREE Private Island Now!
We can't wait to see what you create. Happy scripting🎓 Ready to Practice In-World?
Get your FREE island and practice everything you just learned — no credit card, no monthly fees.
Claim Your Free Island Now →No credit card required · Takes 2 minutes · Your island is FREE forever
Published: 2026-03-15 · Difficulty: Advanced · Category: Scripting
| Questions? Contact us
| ← Back to School