3 months ago

## 從零開始學習 NPC

Hello, and welcome to my attempt to teach everyone NPC scripts better. Regardless of the countless tutorials, and countless Help threads that have been posted and solved, people still seem to have issues with NPCs. Hopefully, with this tutorial, people will learn something. Before we start, let me tell you a bit about myself. I joined RageZone in October of 2008. I started just like most of you guys, not knowing a god damn thing. I've had my share of nub questions asked, but I've also learned from the help others have given me. I read tutorial after tutorial to learn more, so I didn't have to rely on others to answer my questions. Sure, there are still quite a few things I don't know, no one knows everything. Anyone who says otherwise is a damn fool. The main point I'm trying to make is, if you truly want to be good at something, it takes work. You can't sit back, have others do the work, and expect to become a pro at whatever it is you wish to do. You have to show initiative, and want to learn. I hope my tutorial teaches you something, even if it's just one thing.

## 第一級: 初心者

### 第二課: Types of NPCs

NPC 主要有兩種：需要status的，以及不需要status的。最簡單的區分方法就是...顯然地...status。編寫 NPC 時，時時刻刻捫心自問：我的 NPC 會需要兩個以上的對話窗嗎？如果答案為是，那你絕大多數時候會需要用到status；若答案為非，那status可能用不太到。請見以下範例：

### 第四課: 學習專門術語

NPC 顏色代碼、道具圖片、其他

cm.[指令]

dispose

sendNext

sendPrev

How to use: cm.sendPrev("[文本]");

sendNextPrev

sendOk

sendYesNo

sendAcceptDecline

sendSimple

sendStyle

warp

openShop

haveItem

gainItem

changeJob

getJob

startQuest

completeQuest

forfeitQuest

getMeso

gainMeso

gainExp

getLevel

teachSkill

get[狀態]

modifyNX
Gives/Takes the player nx
How to use: cm.gainNX([amount]);
Make it negative to make it take away.

// 外流端專屬
modifyCSPoint

Keep in mind that some repacks use a different Terminology than this, so it's best to browse your repack/source folder to get familiar with yours.

## 第二級: 中堅份子

Type = 0

Type = 0

Type = 1

Type = 12

Type = 3

Type = 4

Credits: BENG

### 第二課: 使用運算子

! 一元 Returns true if 右運算元 evaluates to false. Returns false If the 右運算元 is true.
&& 條件且 二元 If the operand on the left returns false, returns false without evaluating the operand on the right.
\ \ 條件或

Using this is a good way to limit players from saving up loads of an item to cash in at once. 接著，我們還有關係運算子，他們很常被用在條件當中。列舉其中一部分：

== 等於 Returns true if the expression on the left evaluates to the same value as the expression on the right.
< 小於 Returns true if the expression on the left evaluates to a value that is less than the value of the expression on the right.
<= 小於或等於 Returns true if the expression on the left evaluates to a value that is less than or equal to the expression on the right.
> 大於 Returns true if the expression on the left evaluates to a value that is greater than the value of the expression on the right.
>= 大於或等於 Returns true if the expression on the left evaluates to a value that is greater than or equal to the expression on the right.

As you can see from the example, the item id 4000313 is being replaced with the variable "gl". Even though it's only a minor difference, I shortend the script by 1 character. Now imagine having hundreds of places on a script where 4000313 was replaced by "gl". It adds up on how much space you save by using variables. That was an example on how to use a variable for a number. Here is one on how to use a variable for a string, or text sentence.

## 第三級: 高手過招

### 第一課: 學習陣列

As you can see, the item ids are placed in the order they are shown in the sendSimple. The [selection] in the cm.gainItem method, calls the Array to determine which item to give the player. If they chose the second selection, it would call the second number in the Array. By placing the items in an Array, it shortens the script by a lot. Here is what the script would look like without an Array.

There is a clear difference between the two in which is longer. One of the most common ways to use Arrays, is with the infamous for loop, which I will explain in a later lesson.

Alright, time to explain. This is the multi-dimentional array.

Each separete array is colored, so you can see the 3 different arrays within the multi-dimentional array. Next is the random part, or this little snippet.

Ok, at the top of the script, you see this line.

Think of this as if it were a dice. The number symbolizes the sides of a dice. So on this dice, there are 100 sides. Continue reading the random part as you read this. If the dice lands on side 1 - 50, give an item in the first array. If the dice lands on side 51 - 90, give an item in the second array. If it lands on any other side, give an item in the third array. This line here...

Basically follows through with the action. That is all I know about these types of arrays.

This is the infamous for loop. It simplifies almost anything it is used with. It was created to specifically deal with Arrays. Here is the entire part of the code, so I can explain what it means.

Ok so, i = 0. While 0 is less than the options array, do the code. After executing the code, do i++. i++ basically tells the program running the script to increase i until it reaches the length of the array. So since the options array has a length of 8, it will keep increasing until it displays all 8 options.

Contributions from other members
Spoiler:

Quote Originally Posted by .:LastBreath:. View Post

Insert In NpcConversatonManager:

How to use

CM command:
Code:
changeKeyBinding
allows you to place a skill anywhere in your key config.
How to use: cm.changeKeyBinding(selection, 1, SKILLID);
(THINKS THATS HOW TO USE?)
ALL CREDITS GOES TO "made4forum" FROM HIS RELEASE
....
Further Explanation of the For Loop
Quote Originally Posted by Rice

Final Words

This concludes the tutorial. There is still quite a bit I don't know, even with NPCs. If anyone has something they wish to add/contribute, feel free to post it and I'll include it with credits. I hope you enjoyed my wall of text and learned something valuable. Please leave credits if this is posted anywhere else. Lastly, if you need help with an NPC script or have trouble understanding something, feel free to ask HERE. Do not quote the entire tutorial. If you do, I will ask a Mod to remove your post, because it's an inconvenience for others.

Credits: