Sunteți pe pagina 1din 7

The problem with every NPC scripting tutorial is that people give you instructio

ns. 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 Code:
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 d
o 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 wi
ll 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 Code:
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 ha
s an 'OK' button, and displays the entered text.
--cm.dispose() - This will end your conversation. If you don't add this, you wil
l 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 Code:
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 en
d of the if statement and the corresponding '}'
So, I put:
PHP Code:
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' stateme
nts 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, the
n do this.
So what if I put:
PHP Code:
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 alr
eady 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 nei
ther your 'if' statement or any of your 'else if' statements are true, do this.
In my example script...:
PHP Code:
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, tha
t if you are not level 50 and you are not above level 50, it will say "you are b
elow 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 sec
tion can be an if statement, en else if statement, or an else statement. These s
ections is anything and everything between a set of braces "{ }".
Looking at this, lets make an if statement in an if statement.
PHP Code:
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 "Yo
u 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 di
fferent kind of function, I'll explain it later.
__________________
Hmmm... but that if statement inside of another if statement was impractical. Wh
at 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 an
d 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 re
ally 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 seco
nd: Wouldn t it be annoying as hell to make an NPC that says your exact level usin
g just what s above, wouldn t it? It would take (depending on the server) 1 if stateme
nt, 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.g
etPlayer() opens MapleCharacter.java, a source file, and you can do something fr
om within there. Inside of MapleCharacter.java, there is a returning function ca
lled 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 ho
w 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 thi
ngs. For example, adding '#h' to your text will automatically make the NPC say t
he 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 Code:
== //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 is 'minus. It can be used to subtract dependant variables or funct
ions.
&& //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 ev
en 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 sc
ript.
Code:
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 cl
ick 'end chat'
cm.dispose();
return;
}
if (mode == 1)
status++;
else
status--;
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 st
atus, simply put '} else if (status == n) {', 'n' depending on which status it i
s.
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 e
nd 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 sta
rt() {'. This defines the variable for the whole length of a script. For example
:
PHP Code:
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' w
ill make that variable appear. Just make sure that it isn't inside of a set of q
uotation marks! To add this string into a sentence, simply do it like with the r
eturning function. Make sure you include spaces!
PHP Code:
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 Code:
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' an
d 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 play
er 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 t
his 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 Code:
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 us
ed 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 Code:
var status = -1;
function start() {
cm.sendYesNo("I hope I can make as much as yesterday... well, hello! Don't y
ou 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 a
ffect any of your other characters on your account. Do you want to extend your b
uddy 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 a
s I thought you would. Hahaha, just kidding! Anyway if you feel like changing yo
ur mind, please feel free to come back and we'll talk business. If you make a lo
t of friends, then you know ... hehe ..." : "I see... I don't think you don't ha
ve as many friends as I thought you would. If not, you just don't have 250,000 m
esos 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 r
elief... hehe ...");
cm.dispose();
return;
}
if (status == 0)
cm.sendYesNo("Alright, good call! It's not that expensive actually. #b25
0,000 mesos and I'll add 5 more slots to your buddy list#k. And no, I won't be s
elling 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 migh
t 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 lis
t, you know who to find. Of course, it isn't going to be for free ... well, so l
ong ...");
}
cm.dispose();
}
}
__________________________

S-ar putea să vă placă și