Lua was born to be game-related; Since Warcraft launched it, more and more gaming companies have used it.
Access to the NPC is closely related to the game logic. A transaction box, create box, and task dialog box are displayed, which are basically implemented by Lua;
In this article, I want to write down the Lua Implementation of game tasks based on my recent experiences;
In last December, the new game project was established. The first task assigned to me was to compile the game task. At that time, I still did not know what Lua was doing, it took a day to get familiar with the Lua syntax (mainly about the table structure), call interfaces between C and Lua, and then explore the road.
I personally prefer the top-down learning method, so here I will first give a rough outline of the access task NPC:
1. When a player accesses the NPC, the client sends the click message with the npcid to the server;
2. The server looks for the task menu (actually a Lua table) of this NPC to determine whether the player meets the conditions (whether the level is appropriate, whether the pre-task is completed, and whether the player has done it .. Etc)
3. if yes, the server fills in the form information and assigns an ID index starting from 0 to each task button (because there may be more than one task at one NPC, register a callback function for each button, which is indexed by the ID. The form information is sent to the client and executed by the client to display the appropriate dialog interface;
4. When a player clicks a task button, the client sends the corresponding ID information to the server and the server performs the corresponding callback. (For example, when receiving a task, this callback is probably accepttask (taskid) and so on. When submitting a task, this callback is probably committask (taskid ))
There are many other real situations. because the task is the simplest, you can choose to explain it.
To compile a program, you must first determine the data structure. The final definition of a struct task includes the following members: unique task id, type, pre-task, task reward item, experience, money, task start NPC (from here ), task termination NPC (submit here), task description, unfinished description, completion description, task element, and other fields. If they are not the focus here, they will not be mentioned. It took me two weeks to make sure that the structure was intermittent... I have never played any online games. I started several online game clients in the first few days. I registered a number to go in and play it. I simply understood what the task was like.
I want to explain in detail the task elements:
In my initial understanding, the task is to let you do something, and then you can complete this task and get a reward. Therefore, it is abstracted as a task element. The name may be a bit strange, and it seems a little cool, so I decided.
Task elements can be of different types. The two most common types are monsters and items. Many tasks require how many monsters are killed and how many items are collected. This is also the simplest and most basic.
Later, due to the continuous submission of planning requirements, I added some types. For example, if a task requires you to search for some NPCs, the type is visit, that is, access.
Some tasks enable you to answer questions, and have the question type.
When the game starts, task data is imported to Lua to construct a Lua table for each task-related NPC. The table name suffix is the corresponding npc id for easy search.
The start of the process for receiving a task has been briefly described. Here we will briefly describe the completion of the task.
Each time a player receives a new task, the task element of the task is added to the task manager of the player structure (class player. The task manager uses a map to manage multiple tasks.
When a player accesses the termination NPC of the task, it determines whether the task element is complete. if the task is completed, a dialog box for task submission is constructed. The Player clicks finish to obtain the task reward.
If the task is not completed, the end of the NPC will be a "unfinished description" of the corresponding task ". For example, you may say, "You can't even clean up those monkeys. You really disappoint me ".
The preceding describes the simplest and most common task types. It is difficult to implement the actual game task in a unified framework. However, due to the flexibility of Lua (I personally think that the table structure is flexible and omnipotent), some unimaginable demands for planning (they use the Warcraft task to describe the requirements to me, but I have never touched Warcraft. So some gaming experience is quite good.) In many cases, I use Lua table to configure tables and write several functional functions. However, I feel that some of my ideas may be "willful". At present, it is enough, and it will be hard to say in the future. After all, I cannot imagine what kind of tasks are there.
Well, if I go back to college, I must seriously play an online game ~