[Binwood Lua column] basics 02: some special functions, lua Column
I did not expect that it has been more than a year since the previous basic supplement. I have recently prepared to return Lua and complete all the basics. Let's talk about the Lua function today ~
0. Environment
The main reason I suddenly became interested in Lua was that Cocos Code IDE began to surface, which is an official Cocos2d-x IDE dedicated to Cocos2d-x + Lua or JS, I tried to use it. Although it cannot be said to be perfect, it is worth looking forward.
Therefore, you can select the Lua editor used in this article ~
Stupid wood and flowers contribute, huh? Flowers? No, it's your heart ~
Reprinted please note, original address: http://www.benmutou.com/archives/1702
Source: dummies and Game Development
1. Literacy-Lua Function
Lua is easy to create and call functions, such as code:
- Function mutou ()
- Print ("this is definitely not an advertisement: www.benmutou.com ");
- End
This creates a function and calls the function very easily:
- Mutou ();
Then run the command to see the output. print is also a function.
2. Literacy-function parameters
For the mutou function, modify the parameters, such as the Code:
- Function mutou (name, age)
- Print (name );
- Print (age );
- End
We add two parameters to mutou: name, age, and age.
By the way, there is no variable type in Lua, and variables will display different types according to the given values.
Let's try to call the mutou function like this:
- Mutou ("Wood", 25 );
Then run the command and the output is as follows:
[LUA-print] Wood
[LUA-print] 25
It's normal. I like it ~
3. Massive Cores-adaptive to the number of function parameters
Lua functions have a broad mind and can tolerate all the parameters we pass in. However, Lua functions are filtered according to its rules.
Continue with the mutou function. Let's try to call it like this:
- Mutou ("Wood ");
A parameter is missing, but this is different from the C ++ or Java language. It does not cause compilation errors and can still run normally. The output is as follows:
[LUA-print] Wood
[LUA-print] nil
The missing parameter is automatically replaced by nil.
Then, since you can give less parameters, you can also give more parameters. Try to call it like this:
- Mutou ("Wood", 25, "narration", 45 );
(Xiao RuO: You are only 45 years old! All your cells are 45 years old !)
The output is as follows:
[LUA-print] Wood
[LUA-print] 25
The result is that the extra parameters are removed.
As for whether this is a good thing or a bad thing, it depends on who used it ~
4. Multiple return values
That's right, Lua seems to think of all the things we want to be lazy and implement them.
The Lua function can return multiple values and modify the mutou function as follows:
- Function mutou (name, age)
- Return name, age;
- End
Return the two parameters that are passed in again. Try to call them like this:
- Local name = mutou ("Wood", 25 );
- Print (name );
Call the mutou function and use a variable to save the returned value. The output result is:
[LUA-print] Wood
Because we only use one variable to save the return value, even if the function returns two values, we can only get one. To get two values, it is very simple, as shown below:
- Local name, age = mutou ("Wood", 25 );
- Print (name .. ":" .. age );
The input result is as follows:
[LUA-print] WOOD: 25
By the way, you can use the symbol "..." to connect to a string. I will not talk about it here ~
The return value is the same as the parameter. If we try to use three variables to obtain the function return value, it will not cause compilation errors, as shown below:
- Local name, age, hehe = mutou ("Wood", 25 );
- Print (name .. ":" .. age );
- Print (hehe );
Enter the following information:
[LUA-print] WOOD: 25
[LUA-print] nil
The nil value is assigned to the extra variables. Although this does not cause compilation errors, it may also cause runtime errors.
For example, if you use the ".." symbol to connect to the nil value, an error is returned.
5. Features of multiple return values 1 -- must be the last element of the expression
For a function with multiple return values, multiple values may not be returned in all cases.
Let's look at this special situation, which is still the mutou function. We call it like this:
- Local a, B, c = "first", mutou ("Wood", 25 );
- Print ();
- Print (B );
- Print (c );
The output result is as follows:
[LUA-print] first
[LUA-print] Wood
[LUA-print] 25
That's right. Lua's value assignment statement supports comma expressions (that is, assigning values to multiple variables). The values at the beginning naturally give priority to the variables, therefore, the value of variable a is "first ".
However, this is not the point ~ (Xiao RuO: You can also say a wool !)
The point is that we change the order as follows:
- Local a, B, c = mutou ("Wood", 25), "first ";
- Print ();
- Print (B );
- Print (c );
The output is unexpected:
[LUA-print] Wood
[LUA-print] first
[LUA-print] nil
Yes, the mutou function returns only one value, which is one of the features of the function: If the function call is not the last element of the expression, only one value is returned.
What? Isn't it clear enough? Let's look at a particle (Xiao RuO: particle, you head !)
So we call the function... Well, let's continue with the next one. (Xiao RuO: If you can't give an example, you just got excited !)
6. Features of multiple return values 2 -- forcibly return a value
In some cases, we may get bored with a function. That's right. Why do you always return multiple values ?! (Xiao RuO: Isn't it a function you wrote yourself !)
If you want a function to forcibly return only one value, you can do this:
- Local a, B = (mutou ("Wood", 25 ));
- Print ();
- Print (B );
The input result is as follows:
[LUA-print] Wood
[LUA-print] nil
We only need to put the function call in brackets when calling the function.
7. Name parameters/named real parameters/named arguments
Finally, let's take a look at a more practical function parameter-name parameter.
In fact, "Name Parameters" are my own name. In the second edition of "Lua programming", they are called "named real parameters". But after I see the English words and usage, I prefer to call it "name parameter ".
Let's first ask you a question. We have just been discussing the mutou function. Don't go back and check the number of parameters. Answer it in 1 second ~ (Minor: 2 !)
The second question is, Which of the following is the front and back of the age and name parameters of the mutou function? Answer in 0.001 seconds! (Xiao RuO: answer your question !)
Yes, sometimes we don't remember the Parameter order of a function. Is the reverse parameter the first parameter, or is name the second parameter? (Look like something is wrong ...)
Therefore, the named real parameters come in handy.
Let's modify the mutou function:
- Function mutou (args)
- Return args. name, args. age;
- End
Now the mutou function parameter is changed to a table, which requires that the table contains two values: name and age.
Therefore, the method for calling the mutou function becomes as follows:
- Local args = {name = "Wood", age = 25 };
- Print (mutou (args ));
Yes, you can pass a table variable to the mutou function. Because the function obtains the parameter value based on the key value of the table, the parameter order is not required, this solves the sequence problem we just mentioned.
8. End
Well, there are so many basic features about the function. Of course, there is another variable length parameter I didn't mention, because it seems there is nothing to mention ~
Recently I have been planning to stick to one thing: Don't write games at night, even if you encounter exciting bugs.
I want to charge all the night's time. My basic skills are not enough and I feel a little creden.
I plan to repeat the Lua Program Design book recently, hoping to stick to it ~
The plan is to spend one hour reading a book every night and one hour organizing the tutorials.
Today is the first day. I am very tired. I am much tired of writing tutorials than writing games.
What's the saying? Never put yourself in the comfort zone. Okay, stick to it...
(Xiao RuO: use an ellipsis for wool! Use an exclamation point! This means that you have the determination ...)
I said, Xiao ruo, Why did you use the ellipsis at last...