Famine MoD LUA Programming 0 Basic Primer

Source: Internet
Author: User

Objective

The original posted in the Famine game bar, in order to make the article more targeted, the original cut and streamlined. This paste is mainly for programming 0 basic Modder to explain some of the basics of programming. As for the introduction of the famine framework, it will be explained in another article.

Programming 0 Basic people, want to learn to make mods, the difficulty is relatively large, because the lack of some basic programming concepts, only know how to copy someone else's code or a little change on their basis, encountered a slightly more complex code, it is helpless. For mod crashes or errors, there are few ways to handle it yourself. But I also do not recommend to learn a programming language before you learn the MoD code, which is not necessary. In fact, the basic programming knowledge used in Famine mods is relatively simple, and the Lua language used is much simpler than the strong-typed language such as C. Simply want to be a mod, just need to know some basic knowledge and concepts.

The following are all based on the Lua language.

Identifier

A name

Give the constant, variable, function, and class A name so that we can use it by name. It is generally named with the combination of letters, numbers, and underscores.
Recommended naming rules

    • Constants: Each letter is capitalized, and underlined between each word.
    • Variables and functions: The first letter is lowercase, the first letter of subsequent words is capitalized, the others are lowercase.
    • Class: Capitalize all words in the first letter.

This is only a personal recommendation of the naming rules, readers can according to their preferences to determine the naming rules.

Variable

The amount of the change can be assigned by the sign ' = '.

Typical representative: The person's hunger value. This value is constantly changing in the game almost every moment, so that we can set different effects according to different changes, such as Wolfgang different hunger values will have different forms, this is by detecting hunger value to achieve.

Constant

The amount of time that the program will not be changed while running

In fact, the LUA language cannot define constants on its own. However, for some quantities, we do not need to change in the course of running the game, and we need to refer to it. Lance attack, for example, does not need to be changed during the game, but all the weapons that are done by the authorities are a multiple of the spear's attack power, which needs to be calculated by reference to it. At this point, consider the spear attack as a constant and define it with a variable (spear_damage, which is defined in Tuning.lua).

Scope

The effective area of the variable.

In domains outside the scope, if you refer to this variable and do not have a variable with the same name in the domain, an error will occur.
There are only two options for the scope of a variable in LUA: local and Global. The default non-local modifier is the global (global) variable, and local is added locally. Global variables are scoped to the entire program. The scope of a local variable is in the defined domain. A selection control structure, within a function body, or within a file, is a domain.
The main value of this scope is to keep the system free of clutter. For example, in the attack, you need to calculate the damage, in order to facilitate multiple calculations, we set this value to a variable. However, the enemy's attack will change, its own anti-damage will also change, at this time we hope that the end of the calculation, the variable is referenced to the amount of blood changes, this variable can disappear, will not affect the next calculation of other damage. This is the important role of local variables. Of course, the other advantage of local variables is to read its data, more quickly than global variables, but to improve the performance of the MoD is not the focus of this tutorial, it is not in detail expanded. In summary, global variables, less than use, should be used as sparingly as possible.

In the above I have mentioned statements and references many times, and some people may not know the meaning of the two terms. I'm just passing the definition and assignment together.

Definition and Declaration

The definition is to tell the system what I set this variable/constant to be.
Declaration, that is, to tell the system, I set a variable/constant, you remember it for me.

Definitions and declarations are not the same, but they are often mixed together. If you just want to write a mod, it doesn't matter if you don't know the difference, just think it's enough.

Reference

The reference is to tell the system that I'm going to use this variable/constant to do something.

For example, for a calculation of an expression, the system will help you read the data stored in it.

Assign value

The assignment is to tell the system to deposit the data you give to this variable/Changshili.

Note that the data here, not just exponential words, can be any type allowed by the Lua language, such as a text (string), a Boolean value (True or false), and so on. In some languages, such as C, definitions and assignments can be separated. But in the script language of the Famine MoD Lua, the two are linked together. The first assignment to a variable is the definition of it.

Data type

Assigning a value to a variable is about writing data to it, which involves a problem with the data type. In the initial assignment to a variable, the data type of the given value is the data type of the variable. Assigning a value to this variable later must be given a value of the same data type, which would cause the system to crash (except nil) if the values were different. This is not a small talk, just for the Lua language, simply list the various types commonly used in Mods

    • Nil: Represents an invalid value that can be assigned to a variable of any data type. The actual effect is equivalent to deleting this variable
    • Boolean: Contains two values: false and True (FALSE and True)
    • String: strings, enclosed in a pair of double or single quotation marks
    • Function: Functions, this will be said below
    • Table: This concept is going to be said later
Function

This is a very important concept in programming.

The difference between a function and a variable can be made by analogy: A variable is like a property, you can give an object a property, let it be described, for example, attribute: can be burnt. And the function, is an operation method, you let an object has a function, is to let it have some kind of operation. For example, the method of operation: the specific steps and actions that are burnt down.

A function consists of a function name, a parameter table, and a function body. Functions, like variables, can be referenced and scoped. Unlike variables, however, functions need to be defined separately, and in different programming languages, functions are defined differently, but not all of the three basic components mentioned above. In Lua, functions can also be considered as variables and can be assigned values. In addition, a function can have a return value, which means that the result of the calculation is returned for use by another function or expression.

In Lua, the basic format of a function definition is as follows:

function 函数名(参数表)函数体end

If you want the scope of the function to be local, add the local before function. This way, you will not be able to call the function outside its scope.
How does a function work? First, you need to understand that defining a function does not make the function work. Only a function statement is executed to make it work. Take the computational damage as an example. You define the function of how to calculate the damage, the parameters are the attacker's attack power and the defender's armor. After the function is defined, it does not work immediately. This function is performed only if you set a series of processes that trigger the function to be triggered when the attack state occurs. When the function executes, two parameters are entered: The attacker's attack power and the defender's armor. In the function body, after a series of calculations, the result is obtained, which is returned by return and used by the variable to receive or add in various expressions. It is important to note that even functions without parameters must be written in such a way when executed: the function name (actual parameter table)

code example:

--定义了函数caldamage,但没有执行function caldamage(attack,armor)        return attack-armorendlocal damage = caldamage(10,8--这里执行了函数caldamage,并把计算的结果返回,赋值给damage

What does the function do? is to make your programming appear more logical, modular, and reduce the amount of code used. After defining the good one function, it is no longer necessary to control the detailed execution of the function (that is, what the function body writes), we just need to know the name of the function, the parameter table and the return value, and what this function does. Because in a famine mod, a large number of functions do not have a return value (that is, the return value is nil), the function is executed in order to use its function.
The function is the most important thing when making a famine mod. We make mods, the main goal is to modify or add functions to the game. It is important to understand when these functions will be triggered, what parameters are required, what functions are returned, and what the return value is.

We do the MoD on the basis of the original game, that is to say, there are many functions that have already been defined so that we can use them. The game of famine, for example, is like a car, and the function is the part of the car that allows the car to have certain functions: start, brake, etc. We now feel that the car can not meet our needs, then, it is clear, do the appropriate modification, to a new build a car easy. Being a mod is like making some modifications. Since it's a retrofit, it's important to understand what parts you need to retrofit and what you want. Some core parts are not to be clarified.

The structure of the Famine mod itself is very open now, but the official does not give the detailed documentation, when we want to implement a function, we do not know whether the official is given, how to do? My advice is to think about the various functions in the game and the mods that have been released by others, and whether they are similar to your needs, and refer to the corresponding code. Yining modification is also a good reference, but Yining modification is a direct modification of the game's core files, and MoD still have some differences, so to use, the premise is to understand its meaning.

Table

The table is not a necessary concept for a programming language, but this concept is used very frequently in famine mods. The entire frame of the game is also very dependent on the table. The important role of the table is also the same as the function, to make your programming seem logical and clear. For example, there are now 4 individuals, A,B,C,D, with multiple attribute descriptions: health, sanity, hunger, damage, armor, Attack_period, Walkspeed, Runspeed. These properties, for 4 individuals, some have, some do not, how do we organize them? It's a good idea to connect with more than one table. First of all, let's give the attributes a class, health, sanity, hunger is the three basic attributes of famine, each separate into a category, damage, armor, Attack_period is related to the fight, classified as combat, as for Walkspeed, Runspeed are related to the movement and are classified as locomotor. So, we have more than one table:

A summary table:

Individual attribute table

Properties a b c d
Health A's blood B's Blood C's Blood D's Blood
Sanity The Spirit of a The Spirit of B The Spirit of C The Spirit of D
Hunger A of hunger B's Hunger C's Hunger D's Hunger
Combat A's combat properties Battle Properties of B C's Combat properties D's Combat properties
Locomotor The Move property of a Move property of B C's Move property Move attribute of D


Each element of this table is a table, and now it's time to take A's combat property sheet, which is:

Individual attribute table

Damage Armor Attack_period
Numerical 20 50 3



So, what do we do when we want to quote A1 's damage? First find a in the first column of the general table, and then find combat in the vertical bar, so we get a hint: go to A1 's Combat property sheet. Then in the Battle attribute table, we found the damage in the bar, when there is only one item in the vertical bar, it is no longer necessary to find it. The a,combat,damage we look for in the search process is the so-called index. We look in the order of the first horizontal and vertical, A is a 1-level index, Combat is 2, and damage is level 3. The specific value (20) of the damage is finally found in order, which is called the value. This value is more than just a numeric value, such as the Combat attribute table of a found in the general table, which can also be referred to as values. Expand Some, if there is not only one item in the vertical table in the Battle attribute table, but there are two items: max,min, what if we want to check the damage maximum value at this time? Then add a Level four index max. When you want to reference A's damage maximum, you can write the call statement in the programminga.combat.damage.max

In conjunction with Famine MoD programming, we often see a statement like this

inst.components.sanity:DoDelta(-10)`

The meaning of this sentence is that the spirit of the current object is reduced by 10.
How does it work? First of all, in the game so many individuals, to find you in the vast sea of people, must have a name, this name is inst, and then, inst under a lot of attribute classes, we need the spirit value, classified as components, that is, component. The concept of components, a concept created by famine for logical clarity in programming, is explained in more detail in the article introducing the Famine programming framework. Then we continue to look for a table of functions containing mental values and operating spirit values in the Components list, that is, sanity. At this point you can see that the sanity is followed by a colon: instead of the previous number. This is because we want to execute this function. If you want to refer to this function for other operations, use the dot number. Of People who have studied C + + will understand the concept of class, and certainly not unfamiliar with it. The people who have not learned, see me in the following about the interpretation of the class.

Class and object-oriented programming

The concept of class is developed in the thought of object-oriented programming. Why use classes? It is because object-oriented programming seems logical structure is clear, easy to implement, interact and maintain. So, what is a class? In programming, it can be understood as a collection of variables and functions. This collection is encapsulated, with some variables and functions you can access and reference, called public variables and public functions, and others that you cannot access or use, which are private variables and private functions. It is important to note that the class itself is only a logical structure, not an entity. For example, a bicycle is a class concept with a lot of basic properties: color, material and so on, there are many things to do: ride, forward, brake and so on. A property is a variable, and an action is a function. The color, the material, is you can see, is the public variable, but the internal turntable's color you cannot see, is the private variable. And the operation of riding and braking, you can decide, is the public function. And the move forward, you can't go straight, you have to pedal repeatedly to let the bike forward, so the advance is a private function. The idea of a bicycle in mind is a class. And the thought of your bike is a concrete entity.

Combined into Famine mod, sanity This is a class, which has a lot of attributes: The current spirit value, the maximum spirit value and so on, there are many operations: Spirit increase/decrease, set the current spirit value, set the maximum spirit value and so on. and specifically to the sanity of a character, that is the entity of this class.
In fact, in Lua, there is only a table, without the concept of class. But the famine of the game-making writers for the sake of programming convenience, or by some means, on the basis of the table, the concept of class created. We just need to realize how to use a class.
A variable in a reference class is manipulated in the same way as an element in a reference table. If you want to invoke a function, you need to change the dot number. To a colon: and add it after the function name (the function parameter table).
For example, the current spiritual value of the character, the maximum mental value of the characters, etc., but also some operating functions, such as the above example Dodelta. As I said before, in Lua, functions can also be considered as variables. If you want to refer to this function, for example, to assign a value to a function, then the colon above will be changed to a dot, and the Back "(-10)" should be removed. If you want to execute this function, use a colon: and add the appropriate parameters.

These are all 0 basic introductory content.
Understanding this part of the content, you can go to the next article, Famine MoD framework outlined.

Famine MoD LUA Programming 0 Basic Primer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.