[Tutorial] NPC scripting for dummies Ch1

Junior Spellweaver
Joined
Oct 6, 2009
Messages
150
Reaction score
35
The problem with every NPC scripting tutorial is that people give you instructions. It tells you how to make that particular NPC, but doesn't help you make your OWN npc. Let's get started shall we?

Let's start off with a no-status NPC.
Every npc script, status or no-status, will have this line:
PHP:
function start() {
This is basically telling the NPC script to commence.

In a no-status NPC, your whole script will take place under that line. You can do anything there.

So, you may be wondering, what is a status?
A status is a part of an NPC conversation. Pressing the next button on an NPC will take you to the next status, pressing back will take you to the previous one... usually.

So back to our no-status NPC.
Here's a basic npc function:
PHP:
function start() {
 cm.sendOk("Hi there!");
 cm.dispose();
}
So let's look at this step by step.
-The NPC script starts
--cm.sendOk("Text") - This is a basic function which sends a chat window that has an 'OK' button, and displays the entered text.
--cm.dispose() - This will end your conversation. If you don't add this, you will get stuck. However, if this were a status NPC, it would simply take you to the next status.
-} - This closes the section, completing everything between 'start() {' and '}'

Oh, don't forget to put a ';' after every function.
This is a really basic no-status NPC. Scroll down to see other basic functions.
____________________________
Now let's make this a little more interesting.
I'm going to teach you to use an 'if' and an 'else' function.

PHP:
function start() {
	if (cm.getLevel > 50) {//This is an ‘If’ statement.
		cm.sendOk("You are above level 50.");//This is a function.
		cm.dispose();
	} else if (cm.getLevel == 50) {//This is an ‘else’ if statement.
		cm.sendOk("You are level 50.");
		cm.dispose();
	} else {//this is an ‘else’ statement.
		cm.sendOk("you are below level 50.");
		cm.dispose();
	}
}
_____________________
So let's look at an IF statement.
An if statement is a statement saying that 'if (this is true) { Do something'
That 'do something' means it will do anything written between the '{' and the end of the if statement and the corresponding '}'
So, I put:
PHP:
if (cm.getLevel > 50) {
		cm.sendOk("You are above level 50.");
		cm.dispose();
	}
This means that If you are above level 50, it well say "You are above level 50" and then dispose the chat.
______________________
Now let's look at an else if statement.
As you probably noticed, after the closing '}' for the if statement, I put 'else if'. What does this mean???
Basically, it's another 'if' statement. However, you can't have two 'if' statements in the same part of the conversation, so we have 'else if', which basically tells you 'If the first if statement isn't true, but this statement is true, then do this.
So what if I put:
PHP:
	if (cm.getLevel > 50) {//This is an If statement.
		cm.sendOk("You are above level 50.");//This is a function.
		cm.dispose();
	} else if (cm.getLevel == 50) {//This is an else if statement.
		cm.sendOk("You are level 50.");
		cm.dispose();
	}
Look down at the else if statement. What it's saying is that:
If (you are not above level 50, but are level 50 exactly) { do stuff.
}
Of course, you don't need to write all that, because the first statement has already been made. So else serves to say that none of the other statements are true. BEWARE: Overlapping statements can cause problems! (i.e. if (cm.getLevel() == 50) { and } else if (cm.getLevel() == 50) {)

So my script is saying, if you aren't above level 50 and you are level 50, say "You are level 50." and dispose the text.

There can be hundreds of else if statements in one part of the script.
______________________
Now let's look at an else statement.
An else statement is pretty much your all-solving statement. It says that if neither your 'if' statement or any of your 'else if' statements are true, do this.
In my example script...:
PHP:
function start() {
	if (cm.getLevel > 50) {//This is an If statement.
		cm.sendOk("You are above level 50.");
		cm.dispose();
	} else if (cm.getLevel == 50) {
		cm.sendOk("You are level 50.");
		cm.dispose();
	} else {//this is an else statement.
		cm.sendOk("you are below level 50.");
		cm.dispose();
	}
}
looking down at the else statement, you realize that it means, in this case, that if you are not level 50 and you are not above level 50, it will say "you are below level 50". If you want to, you could add it as another 'else if' statement: } else if (cm.getLevel() < 50) {
In some cases, else statements just save a little bit of text.
There can only be one else statement in each section of your script.
______________________
Have you been wondering what I mean by 'section' of your script? Actually, a section can be an if statement, en else if statement, or an else statement. These sections is anything and everything between a set of braces "{ }".
Looking at this, lets make an if statement in an if statement.
PHP:
function start() {
	if (cm.getLevel > 50) {
		if (cm.getPlayer().getMapId() == 100000000) {
			cm.sendOk("You are above level 50 and are in Henesys.");
			cm.dispose();
		} else {
			cm.sendOk("You are above level 50.");
			cm.dispose();
		}
	} else if (cm.getLevel == 50) {
		cm.sendOk("You are level 50.");
		cm.dispose();
	} else {
		cm.sendOk("you are below level 50.");
		cm.dispose();
	}
}
Look inside of my 'if (cm.getLevel() > 50) {' statement.
Now there's another if statement! How could you!
Stfu. Now this is an if statement that happens ONLY if you are above level 50.
This one says that if you're above lvl 50 and you're in henesys, it will say "You are above level 50 and are in Henesys.". However, if you're not in henesys, it will just say "You are above level 50."
Now you may be wondering, what is this?: cm.getPlayer().getMapId(); This is a different kind of function, I'll explain it later.
__________________
Hmmm... but that if statement inside of another if statement was impractical. What else can we do?
Code:
function start() {
	if (cm.getLevel > 50 && cm.getPlayer().getMapId() == 100000000) {
			cm.sendOk("You are above level 50 and are in Henesys.");
			cm.dispose();
	} else if (cm.getLevel > 50 && cm.getPlayer().getMapId() != 100000000) {
			cm.sendOk("You are above level 50.");
			cm.dispose();
	} else if (cm.getLevel == 50) {
		cm.sendOk("You are level 50.");
		cm.dispose();
	} else {
		cm.sendOk("you are below level 50.");
		cm.dispose();
	}
}
Well that was different.
This time we're using 'and'(&&). It's saying that if you are above level fifty &&(and) you are in Henesys (Map ID: 100000000), it will say “You are above level 50 and are in Henesys.". But the second one says that if you are above level 50 and are not in henesys it will say "You are above level 50.". So where does it say “are not in henesys”?
For some reason, ‘!’ means ‘not’. Therefore saying that if the mapid != 100000000, it’s really saying: If the map ID does not(!) equal(=) 100000000.

Well that was difficult. Not…
Now let’s look at using Dependant expressions in a text. Think about it for a second: Wouldn’t it be annoying as hell to make an NPC that says your exact level using just what’s above, wouldn’t it? It would take (depending on the server) 1 ‘if’ statement, 253 ‘else if’ statements, and 1 ‘else’ statement. I can do this in one line.
Code:
cm.sendOk(“You are currently level “ + cm.getLevel + “.”);
Why does this work you ask?
This is a ‘returning function’ (will be explained in more detail later!). This means that if you stick this in with the script, it will return a property, anything from an Integer, to a string of text, to opening another source file to execute a function inside of that. This can be done with many things. Like earlier, cm.getPlayer() opens MapleCharacter.java, a source file, and you can do something from within there. Inside of MapleCharacter.java, there is a returning function called ‘getMapId()’. This returns an integer, in this case the ID of the map you’re in. So if you go into MapleCharacter using cm.getPlayer(), you can access any of the functions inside of MapleCharacter. I’m sorry if it's not clear, I'll tell you how to locate a function in the source later.

So back to our script. cm.getLevel() is a returning property which returns your level. I believe you should understand what this means by now. But that’s not all! Let’s look at a function that returns a text string.
Code:
cm.sendOk(“You are level “ + cm.getLevel + “and your name is “ + getPlayer().getName() + “.”);

cm.getPlayer().getName() returns the string of our character's name. But that's actually not necessary. There are text commands that allow you to do certain things. For example, adding '#h' to your text will automatically make the NPC say the player name.
Code:
cm.sendOk(“You are level “ + cm.getLevel + “and your name is #h.”);

Now let's get REALLY abstract. Try adding
Code:
cm.sendOk("Your pet's name is " + cm.getPlayer().getPets().getName() + ".");
_________________________
Right. Now that you can see the beauty of fuctions, let's review our operators.
PHP:
==     //This means 'is equal to'. This can not be used to set values.
=      //This sets a variable to be equal to 'x'. x can be an integer, a string of text, or a function.
>      //This means 'is greater than'
<      //This means 'is less that'
>=      //This means 'is greater than or equal to'
<=      //This means 'is less than or equal to'
+      //This is 'plus'. It can be used to add dependant variables or functions.
++     //This operator does the same thing as doing "+1". This is generally used at the start of status NPCs.
-      //This is 'minus. It can be used to subtract dependant variables or functions.
--     //This operator does the same thing as doing "-1". This is generally used at the start of status NPCs.
&&      //This is 'and'. See earlier example
||      //This is 'or'. This is used to make an 'if' or 'else if' statement that can be activated in two or maore ways.
!      //This means 'not'
!=      //This is 'is not equal to'
Every one of these is vital to making diverse NPCs.
____________________________
You may be wondering, why have I gone on so long about no-status NPCs and not even touched status NPCs? Well, the simple reason is, every status is like its own no-status NPC. So now, I'll show you how to make a Status NPC and that'll be it for now.

The first part you need is a new beginning. Put this at the beginning of your script.
PHP:
var status = 0;
 
function start() {
    status = -1;
    action(1, 0, 0);
}
 
function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
    } else {
        if (mode == 0) {
            cm.sendOk("Okay, come again!");//This is the message shown if you click 'end chat'
            cm.dispose();
            return;
        }
		
    if (mode == 1)
        status++;// see '++' operator in operator section
    else
        status--;// see '--' operator in operator section
  if (status == 0) {//This is where your first status starts.
That 'if (status == 0) {' is where that status starts. To move on to the next status, simply put '} else if (status == n) {', 'n' depending on which status it is.
Everything that goes on in between 'if (status == 0) {' and '} else if (status == 1) {' is the first status, which in itself is like its own no-status NPC. To end your script, complete your final status with a simple '}' instead of another 'else if' statement.
________________________________
Now, final lesson, let's just teach you about a variable.

A variable can be anything from a number to a function.
Most variables are put at the very beginning of the script, before 'function start() {'. This defines the variable for the whole length of a script. For example:
PHP:
var text = "Mah name ish Jon!";

function start() {
	cm.sendOk(text);
	cm.dispose();
}
This will make 'text' mean "Mah name ish Jon!". That way, simply typing 'text' will make that variable appear. Just make sure that it isn't inside of a set of quotation marks! To add this string into a sentence, simply do it like with the returning function. Make sure you include spaces!
PHP:
var text = "Mah name ish Jon!";

function start() {
	cm.sendOk("lolol, i'm bored... " + text + " What's your name?");
	cm.dispose();
}

Or, you could use it to define a number:
PHP:
var number = 412;

function start() {
	cm.sendOk("What is " + (number * 782) + " divided by 782?");
	cm.dispose();
}
______________________________
Oh, right, I almost forgot... I need to teach you how to find functions.

First, open up your 'src' directory, find the folder 'scripting', go to 'npc' and open 'NPCConversationManager.java' with NetBeans. Everything below
Code:
public class NPCConversationManager extends AbstractPlayerInteraction {
that starts with 'public' is a function. For example:
Code:
public void dispose() {
. Here, the function is dispose(), which can be used by adding 'cm.' inf ront of it. There are many different functions. The main kinds are public voids, public ints, and public strings. Public ints return integers, public voids will change/set a property, and public strings will return a string of text, such as a player name.
Look at:
Code:
public void sendNext(String text) {
This is saying that you need to enter a string which will be called 'text'. In this example, you don't need to know that it will be called 'text', all you need to know is that it's a string, so it could be
Code:
public void sendNext(String) {
for all you care. All strings must be in between quotation marks.

Now you can locate a function.
Here are some of the basic functions:
PHP:
cm.sendNext("text"); // sends a chat with a 'next' button.
cm.sendPrev("text"); // sends a chat with a 'back' button.
cm.sendNextPrev("text"); // sends a chat with a 'next' and a 'back' button.
cm.sendSimple("text"); // This sends a chat with no buttons. It is only to be used with selections(will be discussed in chapter 2)
cm.sendYesNo("text"); // sends a chat with a 'yes' and a 'no' button.
______________________________
Well, that's all for today. Sorry if something's not clear, I'm actually really tired. Just post if there's a problem and I'll clear it up.



[IMPORTANT]Make sure you have all the corresponding '{'s and '}'s.

Here's a script for you to study:
PHP:
var status = -1;

function start() {
    cm.sendYesNo("I hope I can make as much as yesterday... well, hello! Don't you want to extend your buddy list? You look like someone who'd have a whole lot of friends... well, what do you think? With some money I can make it happen for you. Remember, though, it only applies to one character at a time, so it won't affect any of your other characters on your account. Do you want to extend your buddy list?");
}

function action(mode, type, selection) {
    status++;
    if (mode == -1) {
        if(mode == 0)
            cm.sendNext(status == 0 ? "I see... you don't have as many friends as I thought you would. Hahaha, just kidding! Anyway if you feel like changing your mind, please feel free to come back and we'll talk business. If you make a lot of friends, then you know ... hehe ..." : "I see... I don't think you don't have as many friends as I thought you would. If not, you just don't have 250,000 mesos with you right this minute? Anyway, if you ever change your mind, come back and we'll talk business. That is, of course, once you have get some financial relief... hehe ...");
        cm.dispose();
        return;
    }
    if (status == 0)          
        cm.sendYesNo("Alright, good call! It's not that expensive actually. #b250,000 mesos and I'll add 5 more slots to your buddy list#k. And no, I won't be selling them individually. Once you buy it, it's going to be permanently on your buddy list. So if you're one of those that needs more space there, then you might as well do it. What do you think? Will you spend 250,000 mesos for it?");
    else if (status == 1) {
        var capacity = cm.getPlayer().getBuddylist().getCapacity();
        if (capacity >= 50 || cm.getMeso() < 250000) {
            cm.sendNext("Hey... are you sure you have #b250,000 mesos#k? If so, then check and see if you have extended your buddy list to the max. Even if you pay up, the most you can have on your buddy list is #b50#k.");
        } else {
            cm.gainMeso(-250000);
            cm.getPlayer().setBuddyCapacity(capacity + 5);
            cm.sendOk("Alright! Your buddy list will have 5 extra slots by now. Check and see for it yourself. And if you still need more room on your buddy list, you know who to find. Of course, it isn't going to be for free ... well, so long ...");
        }
        cm.dispose();
    }
}
__________________________
Chapter 2 is now available!

http://forum.ragezone.com/f428/tutorial-npc-scripting-dummies-ch2-702948/

__________________________

Have a nice day~
 
Last edited:
Re: [Tutorial] NPC scripting for dummies Chapter 1

Lol... It's not so much noob friendly as 'instructive'. The problem I find in most guides is that they lack the instructive capability which gives people the comprehension of coding. If you give a guide to a certain format, people can't do everything, only what that guide says, and they still have no idea why you need to put a '{' after every if statement, or a '}' before an else/else if statement. That's just my opinion though.

Oh, by the way, even after hours of searching for a good tutorial, I still had to figure this all out myself. Ouch.
 
Re: [Tutorial] NPC scripting for dummies Chapter 1

Lol... It's not so much noob friendly as 'instructive'. The problem I find in most guides is that they lack the instructive capability which gives people the comprehension of coding. If you give a guide to a certain format, people can't do everything, only what that guide says, and they still have no idea why you need to put a '{' after every if statement, or a '}' before an else/else if statement. That's just my opinion though.

Oh, by the way, even after hours of searching for a good tutorial, I still had to figure this all out myself. Ouch.

Lol it's tough but in the end it's worth it, because you don't have to relay on others to help you script and NPC. I like it you got my thx.
 
Yeah I do. I run a server but can't say the name since Nexon is looking after the MapleStory Section.

That's kind of a pity, isn't it... I run a server too. I code with this guy named Brian. I swear he's a genius, he taught me everything I know about source coding, and I dare say I'm pretty good. Have you ever coded source? If not I could teach you a thing or two. The things I do are:
GFX
Wz edits(I'm really good at this xD)
npc scripting(this is what I'm best at)
source coding(my favorite, I learn source particularly fast.)

That's about it.
Buddy me :)
well, I have to go for tonight(French time >.<)
Talk to ya later!

Oh yeah, I have an MSN, if you have one pm me(if you feel like it).
Cya!~
:)
 
Last edited:
That's kind of a pity, isn't it... I run a server too. I code with this guy named Brian. I swear he's a genius, he taught me everything I know about source coding, and I dare say I'm pretty good. Have you ever coded source? If not I could teach you a thing or two. The things I do are:
GFX
Wz edits(I'm really good at this xD)
npc scripting(this is what I'm best at)
source coding(my favorite, I learn source particularly fast.)

That's about it.
Buddy me :)
well, I have to go for tonight(French time >.<)
Talk to ya later!

Oh yeah, I have an MSN, if you have one pm me(if you feel like it).
Cya!~
:)

Yeah I code and I script Npc's. Been into java since my first year of high school taking java classes and I was also on OdinMS when it first opened up. I was even on LocalMS Lol Good times!
 
Wow really? That's pretty impressive. I started coding about a year ago, I needed to teach myself everything i've leanred about NPCs. As for source, I mostly learn by myself, with brian's help.

It's tough to learn NPC scripting yourself, but it goes really fast that way.

I just entered High school (grade 8), and I've always been a geek in my own world. My friends still think it's freaky that I do complicated(to them!) scripting for fun...

:D
 
I like your "newb-friendly" guide. I had a little experience with java codes so it was a little bit better for me...
*Thank your thread*
 
xD thanks for the compliments! I hope this helps a lot of people!!!

By the way, people, chapter two won't be released for at least another week, probably more.
 
LOLWUT? My tutorial just got necrobumped by someone o.o
Dammm... But come to think of it, I should probably continue writing this sometime...

@soccer - Not everyone is good at scripting. I think the guide I wrote is pretty good for noobs to understand the way things work, and make their own NPCs. At least simple NPCs. So, I think a mod should decide whether or not to close this o.o.

If I do continue this guide, I'll just recreate this particular topic if this one's already been closed.
 
Good game. Nice bump :O:.

As well as that, I'm not trying to be an ignorant flamer, but would this guide be suitable for closure? *hints at moderators*

Or would people still be able to refer to a NPC scripting guide like this?

Why would it be closed? It's still a decent guide that people can learn from. If people continue to necro bump, they get infracted for it. Simple as that :) Although, I would love to see this updated more.
 
Back