So you want to learn the basics?
Okay! Here we go.
We will make a simple NPC and teach you on the way.
To begin with. You should download a JavaScript editor below at the Javascript editor downloads.
When you have done that, you are ready to start!
Open the program. Go to file and open and then go to your servers main folder and then you go to scripts folder--> NPC folder. If you dont know how to do that, then you should not have a private server at all because its just to go to the NPC folder inside scripts folder in your Server folder. Ok?
When you have got that far, choose a npc to edit.
Click open (remember we are still using the script editor!).
Now, (if you haven't made an new empty NPCID.js) it should get up some weird text.
(It may look weird to you if you are a beginner at this)
Do not panic.(Lol)
All you have to do is to delete the text. (Hotkey - CTRL + A and then delete)
Now you can start scripting!
To begin with, type this at the top:
- Code:
-
var status = 0;
Explanation:
- Spoiler:
This is the variable to the status that we will get into later. Ignore the = 0 part. It doesn't really matter. the function start will make it -1 anyways. If you want to add a new variable you always have to make a var [Name]; at the top or the variable might not work.
When you have added that script, your first variable is set. Under this variable, you can set more variables.
That depends on what you will use them for but we will go into that later.
Now, you have to add the string that actually makes your NPC work.
Code:
function start() {
status = -1;
action(1, 0, 0);
}
This makes the script actually work. You may notice that because of the function_start(). This has to be included in every npc script you are making.
Now you can go in game and talk to your NPC!... JOKES! The npc is not done yet..
Now you will have to add a string for the ACTIONS to work. Still, the above string is the base of the script. Did you notice that action(1, 0, 0); ? Well I did.
What do that variable do? We will come into that now.
Add this string to the script:
Code:
function action(mode, type, selection) {
This means that action is a function that has 3 variables. mode, type and selection. So the action(1, 0, 0) from before will be in the order as this one. So mode is 1, type is 0 and selection is 0.
Now we can add our modes.
Code:
if (mode == -1)
cm.sendOk("Goodbye then!");
cm.dispose();
else {
if (mode == 0 && status == 1) {
cm.dispose();
return;
}
The mode variable, actually takes care of if you have clicked End Chat or finished talking to a NPC. The mode == -1 checks if you have clicked a negative button like, No. The mode == 0 and status == 1 checks the status assigned for choices. The status == 1 should be changed if you have a question in another status. We will not talk about that cm.sendOK in this tut. Please check out Tutorial 3 if you want to knwo about it.
There is ONE more mode that we will add.. It is the one that checks if we did go backwards or forwards in the script.
Code:
if (mode == 1)
status++;
else
status--;
You should now knwo about what the modes do. Yes, they take care of the status's actions. Now lets explain what the status++; actually does. Did you remember the status == -1; variable under function_start()? Now you may have noticed what I mean? It is actually adding +1 when you are going to next window(in-game)/status(in-script). The status--; is subtracting -1 when you are going to previous window(in-game)/status(in-script).
Now are we done? The answer is both no and yes. If you want the npc to not say anything, yes. If you want the npc to actually do anything, no. The script will wonder "Hey! What to do with the statuses then? It's useless!". It isn't useless if you add this if statement:
- Code:
-
if (status == 0) {
This is a npc window(in-game)/status(in-script). If you are gonna add more statuses, You wil?l have to add +1 to the number all the time. For example: If you have status ==1, and want to add a new status, that status will have to be status == 2. But it won't do anything if you leave without the commands (Tutorial 3).
Now you have your first NPC window! Gratz! Now we will add a simple cm.sendNext(""); into it.
- Code:
-
cm.sendNext("Hello there! The Credits Go to henki. I want to teach you how to script NPCs!");
There will be an explanation in tut 3.
Now we will add a new status. This time it has to be "else" because if its not status 0 = if the mode added +1 to the variable "status" it will be 1.
- Code:
-
} else if (status == 1) {
You won't need a explanation for this. The explanation is in the status == 0 explanation.
Now we will make the player choose between two choices.
- Code:
-
cm.sendSimple("So, why are you talking to me? \r\n #L0##bI want to learn NPC scripting#k#l /r/n #L1##bNevermind#k#l");
Tutorial 3/4 for explanation.
Now we will add the 3rd status
- Code:
-
} else if (status == 2) {
Same as status == 0/1
Now I'm gonna explain some of the cm.sendSimple. The L0 and L1 you see, is selection text commands and for them to take you somewhere when you click one, we have to add selections.
- Code:
-
if (selection == 0) {
When you have clicked a selection at the sendSimple, you will be taken to this strings. the selections L0 = selection == 0, L1 = selection == 1 and so on..
Now when we click the first choice, we will be taken to selection 0. But now we will make that one do something.
- Code:
-
cm.sendOk("Then you have to read the tutorial. See ya!");
Tutorial 3 still.
Now we want to add a selection for the second choice.
Code:
} else if (selection == 1) {
and because the second choice says "Nevermind" we want the npc to quit the conversation.
Code:
cm.dispose();
Tutorial 3....
Now we will add some end branches and another dispose and were done!
This is the final result:
- Code:
-
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == -1) {
cm.dispose();
} else {
if (mode == 0 && status == 1) {
cm.dispose();
return;
}
if (mode == 1)
status++;
else
status--;
if (status == 0) {
cm.sendNext("Hello there! All credits to henki! I want to teach you how to script NPCs!");
} else if (status == 1) {
cm.sendSimple("So, why are you talking to me? \r\n #L0##bI want to learn NPC scripting#k#l \r\n #L1##bNevermind#k#l");
} else if (status == 2) {
if (selection == 0) {
cm.sendOk("Then you have to go read the tutorial. Cya!");
} else if (selection == 1) {
cm.dispose();
}
}
}
}
Now press file->Save and you can try out the new made npc!
Im gonna talk about the more/equals/less symbols.
If you want to check (for example) if a player has less ITEMID than AMMOUNT, you type:
Code:
if (cm.haveItem(ITEMID) < AMMOUNT) {
So. The < actually checks if ITEMID is less than AMMOUNT
If you want to check if a player has more ITEMID than AMMOUNT, you type:
Code:
if (cm.haveItem(ITEMID) > AMMOUNT) {
So. The > actually checks if ITEMID is more than AMMOUNT
If you want to check if a player has ITEMID equals AMMOUNT, you type:
Code:
if (cm.haveItem(ITEMID) = AMMOUNT) {
So. The = actually checks if ITEMID equals AMMOUNT
Wait! There is more..
If you want to check if a player has ITEMID less OR equals to AMMOUNT, you type:
Code:
if (cm.haveItem(ITEMID) <= AMMOUNT) {
So. The <= actually checks if ITEMID less OR equals to AMMOUNT
If you want to check if a player has ITEMID more OR equals to AMMOUNT, you type:
Code:
if (cm.haveItem(ITEMID) >= AMMOUNT) {
So. The >= actually checks if ITEMID more OR equals to AMMOUNT
Fast walktrough:
(ID) < (AMMOUNT) = less than
(ID) > (AMMOUNT) = more than
(ID) = (AMMOUNT) = equals
(ID) <= (AMMOUNT) = less OR equals
(ID) >= (AMMOUNT) = more OR equals
EXTRA NPC SCRIPT TEMPLATES:
Quote:
Npc that you cannot leave with no or end chat:
- Code:
-
var status = -1;
function start()
{
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == 1)
status++;
if (status == 0) {
}
}