Leeched from RZ(RageZone) Created by Get31720
This Guide may seem very long but it's all spacing.
Take your time!
This guide will give you a basic walk through of scripting your NPCs. The guide will be broken into Levels each one bringing you to a more advance part of scripting an NPC.
I'm creating this script as I create the thread.
Level 1
Basic Scripting
I'm not going to launch into a full understand of the beginning of a script because this is basics. I don't want you getting confused on what it means in the beginning.
Most people start their scripts with a comment or 2 on top. A very easy comment I like to you is something like this:
[code=php]// get31720 of Ragezone
// Example of an NPC script [/code]the // creates a comment LINE. Only for 1 line though. If you hit enter and start on a new line and you want to put a comment put //
Now to start your script. I personally like to start my scripts like this:
[code=php]var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) { [/code] Now if you want a full explanation of this part I suggest you go to the [Guide] How to Analyze NPC scripts but if you're just here to figure out how to script something here we go.
See where it says Goodbye? That text there is what shows when someone hits Exit Chat or No in a conversation. If you don't want it to say anything just remove the cm.sendOk line and leave the cm.dispose.
Did I lose you?
So you lost track of where I was trying to head with all this? Knowing maplestory you know cm.sendOk must be some kind of conversation. cm.dispose(); ends a conversation. More questions? Post them.
Did I confuse you with the "just leave the cm.dispose();" thing? What I mean is currently in what I showed you if you hit Exit Chat it would say Goodbye. But you can remove the line that says sendOk("Goodbye"); and when you click Exit Chat it'll just dispose the conversation (end it)
Okay now after the status we'll code the NPC to say something to us then end. This is very simple, but it's okay to get confused. Let's add this to the script.
[code=php] cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
} [/code]cm.sendOk is pretty much bringing up a conversation with an OK button at the bottom of it. Simple right? cm.dispose(); is there to end it.
If you don't understand the { } do yourself a favor and look below.
{ and } and basically beginning and ending a block of info. Let's color the script so we can see the big picture
var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) {
cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
}
Colorful enough for you? Now depending on their color I've shown you how the { and } and effecting the blocks of info. If you don't get this post on the thread. If you notice there are 2 { that are not colored and 2 near the bottom that are not colored. This shows the (mode, type, selection) and mode == -1 continue through the script. When ever you use this on the top of your script just remember to stick 2 } at the bottom
You've passed Level 1
Level 2
Continuing the conversation with Next
Let's bring back the script we did in Level 1.
[code=php]var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) {
cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
} [/code]But now let's use cm.sendNext to continue the conversation in 2 windows.
We're going to be concentrating in this area:
[code=php] if (status == 0) {
cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
} [/code]First let's change the cm.sendOk to cm.sendNext and add another cm.sendNext.
[code=php] if (status == 0) {
cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
} [/code] The red area is pointing out a very common mistake. By having both in the same status what the script is trying to do is bring up 2 conversation windows at the same time. This will ruin your script so we have to add another status.
[code=php] if (status == 0) {
cm.sendNext("Hello, I'm Angel's (get31720) NPC!");
}
else if (status == 1) {
cm.sendNext("I am helping people learn scripting!");
cm.dispose();
}
}
} [/code]And TaDa you've changed your NPC to have cm.sendNext!
Now if you click the NPC it'll say: Hello, I'm Angel's (get31720) NPC!
Then if you click Next it'll say: I am helping people learn scripting
If you click Exit Chat it'll say: Goodbye
To see the finished script look below.
var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) {
cm.sendNext("Hello, I'm Angel's (get31720) NPC!");
}
else if (status == 1) {
cm.sendNext("I am helping people learn scripting!");
cm.dispose();
}
}
}
Do you need me to show you how the { and } work when you edited this? Look below if you need an explanation.
if (status == 0) {
cm.sendNext("Hello, I'm Angel's (get31720) NPC!");
}
else if (status == 1) {
cm.sendNext("I am helping people learn scripting!");
cm.dispose();
}
}
}
The { and } are beginnings and endings of blocks of info. I'm showing you the blocks they begin/end with colors. The last 2 uncolored ones the bottom are affected from the top of the script. See the 2nd spoiler if you want the colored { and } of the Level 1 script to help you understand.
You've passed Level 2
Level 3
Learning How to use if and else
Let's say you wanted to check if someone had a power elixir and if they did it would say "Good for you" and if they didn't it would say "Here have some" and give them 10.
Let's get the level 2 script but edit the text inside to fit our situation.
We'll be concentration on the same area again. The top part is important to the script but this is what we're editing.
[code=php] if (status == 0) {
cm.sendNext("If you need power elixirs I'll give you 10. If you already have some you don't need them.");
}
else if (status == 1) {
cm.sendNext("Good for you. You already have a power elixir");
cm.dispose();
}
}
} [/code]So far all I did was change the text the NPC is going to say. Now let's add the if(cm.haveItem(2000005)) which is checking to see if they have the power elixir. (power elixir's ID is 2000005).
[code=php] if (status == 0) {
cm.sendNext("If you need power elixirs I'll give you 10. If you already have some you don't need them");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendNext("Good for you. You already have a power elixir.");
cm.dispose();
}
}
} [/code]But wait there's more! Now we need to use } else { because if the player does NOT have a power elixir we need to code what happens when the if(cm.haveItem(2000005); is false (not true)
[code=php] if (status == 0) {
cm.sendNext("If you need power elixirs I'll give you 10. If you already have some you don't need them");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendNext("Good for you. You already have a power elixir.");
cm.dispose();
}
}
} [/code]Tada You've finished! Now the NPC will check for the power elixir. If the player has one it will say good for you and end the conversation. But if they don't have a power elixir the NPC will give the player 10.
if you want it to check for more than, less than, or equal to (>, <, and =) you can do a combination. Such as
[code=php]if(cm.getMeso() >= 2000) [/code] ONLY USE > , <, = for cm.getMeso NOT for cm.haveItem
Which basically translates into if the character has more than or equal to 5 power elixirs.
The { in status ==1 doesn't end in the beginning of the script so we had to put a } at the end of the script.
Remeber if at any point you get confused post what level you got confused on and what you were having trouble understanding!
You've passed Level 3
Level 4
Learning selections
First let's learn about writing a selections and what it means. selections are the different choices you can see when you open your NPC script.
When you want an NPC to say text with a selection you need this
Code: \r\n#L0#Can I have some anyway?#l\r\n jumps down 1 line
#L0# begins a selection This also means that this selection is 0'; #L1# is selection 1 and so on
Can I have some anyway is the selection text
#l (this is an L not an i) ends the selection.
Now let's take the same script we just made but change the text, and lets have 2 selections.
if (status == 0) {
cm.sendNext("Hello!");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendSimple("You have power elixirs.\r\n#L0#Can I have some anyway?#l\r\n#L1#I'll see you later#l");
}
}
else if (status == 2) {
if (selection == 0) {
} else if (selection == 1) {
}
else {
cm.sendNext("You don't have a power elixir? Here I'll give you 10!");
cm.gainItem(2000005, 10);
}
}
}
}
Under the selections we code what we want the NPC to do. What I changed was:
used cm.sendSimple for the text with selections.
added 2 selections.
Added another status because sendSimple increases the status
Moved the }else{ area higher because it has to be under the if(cm.haveItem
Now under selection 0 we're going to code for the NPC to say "Okay here you go" and give them 10 power elixirs. Under selection 1 we're going to make the NPC say See you later and dispose. And just for fun I'm going to change the cm.sendNext to cm.sendOk.
if (status == 0) {
cm.sendNext("Hello!");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendSimple("You have power elixirs.\r\n#L0#Can I have some more please?#l\r\n#L1#Nah, I don't want any.#l");
}
else {
cm.sendOk("You don't have a power elixir? Here I'll give you 10!");
cm.gainItem(2000005, 10);
cm.dispose();
}
}
else if (status == 2) {
if (selection == 0) {
cm.sendOk("Okay here you go");
cm.gainItem(2000005);
cm.dispose();
}
else if (selection == 1) {
cm.sendOk("See you later");
cm.dispose();
}
}
}
}
and TaDa we're done!
If you want to see the whole finished script look below:
var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) {
cm.sendNext("Hello there!");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendSimple("You have power elixirs.\r\n#L0#Can I have some more please?#l\r\n#L1#Nah, I don't want any.#l");
}
else {
cm.sendOk("You don't have a power elixir? Here I'll give you 10!");
cm.gainItem(2000005, 10);
cm.dispose();
}
}
else if (status == 2) {
if (selection == 0) {
cm.sendOk("Okay here you go");
cm.gainItem(2000005);
cm.dispose();
}
else if (selection == 1) {
cm.sendOk("See you later");
cm.dispose();
}
}
}
}
You've passed Level 4
Level 5
Revising and Checking your script
Are all your { } correct?
Did you put ; everywhere it needed?
Did you make sure all your text in the conversation has " in the beginning and end?
Did you type any misspellings?
Test your new skills in the root section where people are looking for help with their scripts. If you can read them and correct them congratulations!
When you pass Level 5 is your own opinion
This Guide may seem very long but it's all spacing.
Take your time!
This guide will give you a basic walk through of scripting your NPCs. The guide will be broken into Levels each one bringing you to a more advance part of scripting an NPC.
I'm creating this script as I create the thread.
Level 1
Basic Scripting
I'm not going to launch into a full understand of the beginning of a script because this is basics. I don't want you getting confused on what it means in the beginning.
Most people start their scripts with a comment or 2 on top. A very easy comment I like to you is something like this:
[code=php]// get31720 of Ragezone
// Example of an NPC script [/code]the // creates a comment LINE. Only for 1 line though. If you hit enter and start on a new line and you want to put a comment put //
Now to start your script. I personally like to start my scripts like this:
[code=php]var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) { [/code] Now if you want a full explanation of this part I suggest you go to the [Guide] How to Analyze NPC scripts but if you're just here to figure out how to script something here we go.
See where it says Goodbye? That text there is what shows when someone hits Exit Chat or No in a conversation. If you don't want it to say anything just remove the cm.sendOk line and leave the cm.dispose.
Did I lose you?
So you lost track of where I was trying to head with all this? Knowing maplestory you know cm.sendOk must be some kind of conversation. cm.dispose(); ends a conversation. More questions? Post them.
Did I confuse you with the "just leave the cm.dispose();" thing? What I mean is currently in what I showed you if you hit Exit Chat it would say Goodbye. But you can remove the line that says sendOk("Goodbye"); and when you click Exit Chat it'll just dispose the conversation (end it)
Okay now after the status we'll code the NPC to say something to us then end. This is very simple, but it's okay to get confused. Let's add this to the script.
[code=php] cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
} [/code]cm.sendOk is pretty much bringing up a conversation with an OK button at the bottom of it. Simple right? cm.dispose(); is there to end it.
If you don't understand the { } do yourself a favor and look below.
{ and } and basically beginning and ending a block of info. Let's color the script so we can see the big picture
var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) {
cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
}
Colorful enough for you? Now depending on their color I've shown you how the { and } and effecting the blocks of info. If you don't get this post on the thread. If you notice there are 2 { that are not colored and 2 near the bottom that are not colored. This shows the (mode, type, selection) and mode == -1 continue through the script. When ever you use this on the top of your script just remember to stick 2 } at the bottom
You've passed Level 1
Level 2
Continuing the conversation with Next
Let's bring back the script we did in Level 1.
[code=php]var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) {
cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
} [/code]But now let's use cm.sendNext to continue the conversation in 2 windows.
We're going to be concentrating in this area:
[code=php] if (status == 0) {
cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
} [/code]First let's change the cm.sendOk to cm.sendNext and add another cm.sendNext.
[code=php] if (status == 0) {
cm.sendOk("Hello, I'm Angel's (get31720) NPC!");
cm.dispose();
}
}
} [/code] The red area is pointing out a very common mistake. By having both in the same status what the script is trying to do is bring up 2 conversation windows at the same time. This will ruin your script so we have to add another status.
[code=php] if (status == 0) {
cm.sendNext("Hello, I'm Angel's (get31720) NPC!");
}
else if (status == 1) {
cm.sendNext("I am helping people learn scripting!");
cm.dispose();
}
}
} [/code]And TaDa you've changed your NPC to have cm.sendNext!
Now if you click the NPC it'll say: Hello, I'm Angel's (get31720) NPC!
Then if you click Next it'll say: I am helping people learn scripting
If you click Exit Chat it'll say: Goodbye
To see the finished script look below.
var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) {
cm.sendNext("Hello, I'm Angel's (get31720) NPC!");
}
else if (status == 1) {
cm.sendNext("I am helping people learn scripting!");
cm.dispose();
}
}
}
Do you need me to show you how the { and } work when you edited this? Look below if you need an explanation.
if (status == 0) {
cm.sendNext("Hello, I'm Angel's (get31720) NPC!");
}
else if (status == 1) {
cm.sendNext("I am helping people learn scripting!");
cm.dispose();
}
}
}
The { and } are beginnings and endings of blocks of info. I'm showing you the blocks they begin/end with colors. The last 2 uncolored ones the bottom are affected from the top of the script. See the 2nd spoiler if you want the colored { and } of the Level 1 script to help you understand.
You've passed Level 2
Level 3
Learning How to use if and else
Let's say you wanted to check if someone had a power elixir and if they did it would say "Good for you" and if they didn't it would say "Here have some" and give them 10.
Let's get the level 2 script but edit the text inside to fit our situation.
We'll be concentration on the same area again. The top part is important to the script but this is what we're editing.
[code=php] if (status == 0) {
cm.sendNext("If you need power elixirs I'll give you 10. If you already have some you don't need them.");
}
else if (status == 1) {
cm.sendNext("Good for you. You already have a power elixir");
cm.dispose();
}
}
} [/code]So far all I did was change the text the NPC is going to say. Now let's add the if(cm.haveItem(2000005)) which is checking to see if they have the power elixir. (power elixir's ID is 2000005).
[code=php] if (status == 0) {
cm.sendNext("If you need power elixirs I'll give you 10. If you already have some you don't need them");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendNext("Good for you. You already have a power elixir.");
cm.dispose();
}
}
} [/code]But wait there's more! Now we need to use } else { because if the player does NOT have a power elixir we need to code what happens when the if(cm.haveItem(2000005); is false (not true)
[code=php] if (status == 0) {
cm.sendNext("If you need power elixirs I'll give you 10. If you already have some you don't need them");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendNext("Good for you. You already have a power elixir.");
cm.dispose();
}
}
} [/code]Tada You've finished! Now the NPC will check for the power elixir. If the player has one it will say good for you and end the conversation. But if they don't have a power elixir the NPC will give the player 10.
if you want it to check for more than, less than, or equal to (>, <, and =) you can do a combination. Such as
[code=php]if(cm.getMeso() >= 2000) [/code] ONLY USE > , <, = for cm.getMeso NOT for cm.haveItem
Which basically translates into if the character has more than or equal to 5 power elixirs.
The { in status ==1 doesn't end in the beginning of the script so we had to put a } at the end of the script.
Remeber if at any point you get confused post what level you got confused on and what you were having trouble understanding!
You've passed Level 3
Level 4
Learning selections
First let's learn about writing a selections and what it means. selections are the different choices you can see when you open your NPC script.
When you want an NPC to say text with a selection you need this
Code: \r\n#L0#Can I have some anyway?#l\r\n jumps down 1 line
#L0# begins a selection This also means that this selection is 0'; #L1# is selection 1 and so on
Can I have some anyway is the selection text
#l (this is an L not an i) ends the selection.
Now let's take the same script we just made but change the text, and lets have 2 selections.
if (status == 0) {
cm.sendNext("Hello!");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendSimple("You have power elixirs.\r\n#L0#Can I have some anyway?#l\r\n#L1#I'll see you later#l");
}
}
else if (status == 2) {
if (selection == 0) {
} else if (selection == 1) {
}
else {
cm.sendNext("You don't have a power elixir? Here I'll give you 10!");
cm.gainItem(2000005, 10);
}
}
}
}
Under the selections we code what we want the NPC to do. What I changed was:
used cm.sendSimple for the text with selections.
added 2 selections.
Added another status because sendSimple increases the status
Moved the }else{ area higher because it has to be under the if(cm.haveItem
Now under selection 0 we're going to code for the NPC to say "Okay here you go" and give them 10 power elixirs. Under selection 1 we're going to make the NPC say See you later and dispose. And just for fun I'm going to change the cm.sendNext to cm.sendOk.
if (status == 0) {
cm.sendNext("Hello!");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendSimple("You have power elixirs.\r\n#L0#Can I have some more please?#l\r\n#L1#Nah, I don't want any.#l");
}
else {
cm.sendOk("You don't have a power elixir? Here I'll give you 10!");
cm.gainItem(2000005, 10);
cm.dispose();
}
}
else if (status == 2) {
if (selection == 0) {
cm.sendOk("Okay here you go");
cm.gainItem(2000005);
cm.dispose();
}
else if (selection == 1) {
cm.sendOk("See you later");
cm.dispose();
}
}
}
}
and TaDa we're done!
If you want to see the whole finished script look below:
var status = 0;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
}
else {
if (status >= 2 && mode == 0) {
cm.sendOk("Goodbye");
cm.dispose();
return;
}
if (mode == 1) {
status++;
}
else {
status--;
}
if (status == 0) {
cm.sendNext("Hello there!");
}
else if (status == 1) {
if(cm.haveItem(2000005)) {
cm.sendSimple("You have power elixirs.\r\n#L0#Can I have some more please?#l\r\n#L1#Nah, I don't want any.#l");
}
else {
cm.sendOk("You don't have a power elixir? Here I'll give you 10!");
cm.gainItem(2000005, 10);
cm.dispose();
}
}
else if (status == 2) {
if (selection == 0) {
cm.sendOk("Okay here you go");
cm.gainItem(2000005);
cm.dispose();
}
else if (selection == 1) {
cm.sendOk("See you later");
cm.dispose();
}
}
}
}
You've passed Level 4
Level 5
Revising and Checking your script
Are all your { } correct?
Did you put ; everywhere it needed?
Did you make sure all your text in the conversation has " in the beginning and end?
Did you type any misspellings?
Test your new skills in the root section where people are looking for help with their scripts. If you can read them and correct them congratulations!
When you pass Level 5 is your own opinion