# Script functions of WvsProj
[TOC]
In the version [69817dc](https://github.com/tnsc4502/mnwvs196/commit/69817dcbf650dd84dbba33740806fd307006908e), I modified the lua files so that it can support the sytax "member->MemberFunction()" to get close to C++ style.
In the descriptions, \* indicates that it would probably be removed, \<\> means the parameter is unnecessary required, ... represents the parameters are variadic.
System
--
In the script the word "System" denotes class Script iteself, so far I haven't implemented functions in there, but in the future I may put some common helper like GetSystemTime or some stuffs in.
It will set a variable userID as the ID of User who starts the conversation.
Npc
--
The word "Npc" in the script represents ScriptNPCConversation instance, for every Npc script, it will create only one unique pointer by calling Npc.get().
We provide functions as follows:
* ***static*** get()
```cpp=
//Returning a unique pointer of current ScriptNpcConversation.
//In the same script, it return a same value wherever you call it.
self = Npc.get()
```
* say(sText, \<nParam\>)
```cpp=
//Showing a simple conversation that has only one "Ok" button.
self->say("Hello")
```
* sayNext(sText, \<nParam\>)
```cpp=
//Showing a conversation that has only one next/prev button.
//To create conversations with page effect, put at least two sayNext:
self->sayNext("Page 1") //with Next button only.
self->sayNext("Page 2") //with Prev/Next buttons, if Prev is clicked, return to the previous conversation.
self->say("Can't go back") //Page conversation end here, can't roll back.
```
* askYesNo(sText, \<nParam\>)
```cpp=
//Showing a conversation with Yes/No buttons, returns a integer as user's response
if(self->askYesNo("You sure?")) then
...Yes is clicked
else
...No is clicked
end
```
* askMenu(sText, \<nParam\>)
```cpp=
//Showing a conversation with multiple options, returns a integer as user's selection.
opt = self->askMenu("#L0#Opt 1#l\r\n#L1#Opt 2#l")
```
* askNumber(sText, nDefaultValue, nMinValue, nMaxValue)
```cpp=
//Showing a text box to ask user input an number, then returns that number.
//nDefaultValue = the default number shows in the text box.
//nMinValue = The minimum value that is allowed.
//nMaxValue = The maximum value that is allowed.
opt = self->askNumber("Plz input an number:", 100, 0, 1000)
```
* askText(sText, sDefaultText, nMinSize, nMaxSize)
```cpp=
//Showing a text box to ask user input a text, then returns that text.
//sDefaultText = the default text shows in the text box.
//nMinSize = The minimum length of text.
//nMaxSize = The maximum length of text.
opt = self->askText("Plz input a text:", "Example", 1, 32)
```
* askAvatar(sText, nTicketID, aIDs)
```cpp=
//Showing a conversation that allow user to select faces/hairs.
//nTicketID = The required item.
//aIDs = Array of items.
//Once the user has made the choice, it affect the user instantly.
self->askAvatar("Choose one:", 0, {30000, 30001})
//or
opts = {30000, 30001}
self->askAvatar("Choose one:", 0, opts)
```
* \* debug(sDbgText)
```cpp=
//Printing something to the console.
//For debuging purposes.
self->debug("Console test.")
```
It also provide some conversation parameters:
* style_playerTalk
```cpp=
//Making the user as the speaker in the conversation.
self->say("To Npc.", style_playerTalk)
```
* style_playerRespond
```cpp=
//Making the user as the responder in the conversation.
self->say("To Npc.", style_playerRespond)
```
* style_fullScreen
```cpp=
//Making the conversation being full screen.
self->say("To Npc.", style_fullScreen | style_playerRespond)
```
* style_inverseDirection
```cpp=
//Making the image of the speaker reversed.
self->say("To Npc.", style_inverseDirection)
```
* style_noESC
```cpp=
//Removing the ESC button in the conversation.
self->say("To Npc.", style_noESC | style_playerTalk)
```
User
--
In the script, we use "User" as ScriptTarget, which is a wrapper of User class.
To get a user pointer, calling User.get(nUserID) will return a ScriptTarget instance, you can test the result to check it's validity.
The functions available for User are:
* ***static*** get(nUserID)
```cpp=
//Returning the user instance of specific nUserID.
target = User.get(userID)
```
* noticeMsg(nType, sText)
```cpp=
//Poping a notice box, nType seems have no effecct.
target->noticeMsg(0, "Warning!")
```
* chatMsg(nType, sText)
```cpp=
//Sending some messages in chatting box, nType = Color
target->chatMsg(4, "Warning!") //4 = Purple
```
* inventory()
```cpp=
//Return inventory instance of the user
inv = target->inventory()
inv->exchange(0, 1432011, 1)
```
* questRecord()
```cpp=
//Return QuestRecord instance of the user
qr = target->questRecord()
qr->setState(2001, 2)
```
Inventory
--
The "Inventory" in scripts is ScriptInventory.
You can obtain the inventory instance by two ways, by calling Inventory.get(nUserID) or by calling inventory() of a user pointer.
The following are supported methods:
* ***static*** get(nUserID)
```cpp=
//Returning inventory instance of the user
inv = Inventory.get(userID)
```
* exchange(nMeso, <nItemID, nCount>...)
```cpp=
//Exchanging items and meso.
//nCount < 0 means get item from user; otherwise, it gives the specific item to user.
//It returns a value indicate the result of exchanging:
//0 = Success
//1 = Insufficient Meso
//2 = No Available Slot
//3 = Insufficient Item In The Slot
ret = inv->exchange(0, 1432011, 1)
ret = inv->exchange(0, 1432011, 1, 1432012, 1)
ret = inv->exchange(0, 1432011, 1, 1432012, -1)
```
QuestRecord
--
11/05/2018 ScriptQuestRecord has added, below are the functions it provides. Like inventory, it has to two ways to access player's questRecord, by calling QuestRecord(nUserID) or through pScriptUser->questRecord()
* ***static*** get(nUserID)
```cpp=
//Returning QuestRecord instance of the user
qr = QuestRecord.get(usreID)
```
* getState(nQuestKey)
```cpp=
//Get the state of specified quest by nQuestKey(= QuestID).
state1 = qr->getState(2001)
```
* setState(nQuestKey, nState <,sInfo>)
```cpp=
//Set the state to nState of specified quest by nQuestKey.
//sInfo set only when nState = 1
//When nState :
//0 = Remove the quest record.
//1 = Start the quest.
//2 = Complete the quest.
qr->setState(2001, 2)
```
FieldSet
--
The scripts used in fieldSet are different from Npc which won't set a variable userID, and is unable to perform any Npc conversation(since it bind non of Npcs).
* ***static*** enter(sFieldSetName)
```cpp=
FieldSet.enter("testFieldSet")
```
\* Packet
--
We also provide "Packet" for testing purpose, it supports following functions:
* ***static*** new()
* encode1(nVal)
* encode2(nVal)
* encode4(nVal)
* encode8(nVal)
* encodeStr(sText)
* encodeHex(sText)
* clear()
* send(pScriptUser)
* \* print()
```cpp=
target = User.get(userID)
pt = Packet.new()
pt->encode2(0x2EF)
pt->encodeStr("hi")
pt->encode1(4)
pt->send(target)
pt->clear()
pt->encode2(0x2F0)
pt->encode2(4)
pt->encodeStr("hi")
pt->send(target)
```