The difference between calling a function in Lua and using a dot number and a colon _lua

Source: Internet
Author: User
Tags lua

This paper is an object-oriented preheating chapter, explaining the difference between the two ways of calling, beginners are more likely to be pits.

1. Beginner's most confusing top1--call a function with a dot or a colon?

Let's take a look at the following two lines of code:

Copy Code code as follows:

Msprite.setposition (100, 20);
Msprite:setposition (100, 20);

This is a nightmare for first-time friends with Lua, why are there two forms of function invocation that we can just pick?

There is a difference between the two forms, but there is only one.
However, for the time being, I will explain later.

2. The simplest class

Let's take a look at the simple, to create a "class" to try, the following code:

Copy Code code as follows:

Tsprite = {
x = 0,
y = 0,
}
function tsprite.setposition (x, y)
tsprite.x = x;
Tsprite.y = y;
End

Tsprite.setposition (1, 2);
Print (Tsprite coordinates (...). Tsprite.x.. "," .. Tsprite.y.. ")");

It's actually creating a table and adding some fields to the table.
The output results are as follows:

Copy Code code as follows:

[Lua-print] Tsprite coordinates (1,2)

Let's take a look at the SetPosition function, in which the X and Y fields are called by Tsprite.
And, the way we use setposition is to use the dot number, which is the authentic function call way, remember.

3. Is it okay to use a real name? The role of--self

If you are more sensitive, you will find that the example in question is very problematic if we call it this way:

Copy Code code as follows:

Local who = Tsprite;
Tsprite = nil;

Who.setposition (1, 2);

This will certainly be an error, although the Who can really successfully invoke the SetPosition function, but the function needs to use Tsprite, and at this time Tsprite has been nil.

So the smart thing we can do is:

Copy Code code as follows:

Tsprite = {
x = 0,
y = 0,
}
function Tsprite.setposition (self, x, y)
self.x = x;
Self.y = y;
End

Local who = Tsprite;
Tsprite = nil;

Who.setposition (who, 1, 2);
Print (Tsprite coordinates (...). Who.x.. "," .. Who.y.. ")");

The output is still:

Copy Code code as follows:

[Lua-print] Tsprite coordinates (1,2)

Notice the first parameter of SetPosition, we force a parameter to be passed in, which is the tsprite itself.

Thus, when the SetPosition function is invoked, the content of the incoming who,who is the content of the tsprite, so setposition can perform normally.

4. Play the traditional virtues of laziness-the default self parameter, and the default pass self parameter

If you let a high (Chao) intelligence (JI) quotient (LAN) ape to process self every time you create a function and call a function, then he would say, "Come here, I promise I won't kill you."

So, LUA provides a new way to use it, yes, that's the colon.

Look, I'm talking about calling a function with a colon.
The following code:

Copy Code code as follows:

Tsprite = {
x = 0,
y = 0,
}
function tsprite:setposition (x, y)
self.x = x;
Self.y = y;
End
Local who = Tsprite;
Tsprite = nil;

Who:setposition (1, 2);

First, pay attention to the definition of setposition function, using a colon;
Second, pay attention to the call to the SetPosition function, using a colon.

The function of a colon is that when the function is defined, the first argument that is hidden to the function is self, and when the function is invoked, the current caller is passed in as the first argument by default.

After using a colon, it's the same as when we just used the dot number, but we no longer need to explicitly define the self parameter and actively pass the WHO parameter.

OK, so that's the difference between the dot and the colon, and it can be said that the colon was born to get us lazy.
If developed using Cocos2d-x Lua, the colon is used in most cases.
The reason is simple, because in most cases we have to use the self parameter, just like the This keyword in C + +.

5. End

The next article formally into the object-oriented content, I hope that we have not forgotten the meta table and meta method, such as the basis of object-oriented use.

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.