Some common method records about Unity and XLua

I didn’t expect to start writing Lua again after two years. After another two years of experience, I read the pits recorded before, and I have a new understanding. Let’s improve it now.
The original record when writing lua

Let's first complete some of the previously recorded problems.

Question 1: I obviously wrote a lua script but couldn't find it, or couldn't find a method of a certain lua script, and it displayed nil or didn't exist when calling it, etc.

Reason: This is mainly the usage of require, which is actually an implementation of code segmentation. If you want to find a method that refers to a certain script, you must first find the location of the script. require is the way to find this.

Question 3: The lua script can be found, but the lua method cannot be called.

Reason: There are two ways to call methods in lua, one is to directly point out the method by pointing.
One is to call the method through:

This previous explanation is actually problematic. It should be said that for lua to call the C# method, the static method can be directly clicked. When an object refers to a member variable (that is, when a member method is called), this will be called implicitly, which is equivalent to self in lua. At this time, you need to use: to call.
The same is true for lua. When using:, it actually adds a hidden first parameter self to the function; when calling, the current object is passed in as the first parameter by default; in this way, the member variable of self can be used.

Question 4: I have a data defined in the same script, but when called in this script, there is a problem or it is nil.
Reason: Probably related to where you define your data. Try defining the data at the calling location.

Lua is an interpreted language. It uses a parser to execute code at runtime, so it distinguishes the sequence of codes, that is, it cannot be called first and then defined.

The following are some newly added points that need attention

1: The number of data obtained by #table can only obtain the number of consecutive indexes. It is not suitable for irregular tables.
2: for index, value in ipairs(myTable) do -- body end
This method is the same as above, it can only traverse the regular table.

3: Xlua calls C# static method rules: CS.namespace.class name.member variable
Xlua instantiates C# class rules (which can be understood as calling the corresponding class constructor): CS.namespace.class name() Xlua call enumeration rules: CS.namespace.enumeration name.enumeration value 4: Class extension needs to add [LuaCallCSharp] to the static class written by the extension method, otherwise it cannot be
called
.

simple summary

1: If you only use xlua to write code logic, you only need to pay attention to the mutual calls between Xlua and C#, and the lua syntax.
2: If you need to build a game framework through lua, you need to have a deep understanding of the concept of table in lua. That is, the data structure of lua is table.
3: Write lua and choose a suitable plug-in to get twice the result with half the effort. For example, the plug-in can provide some code completion, highlighting and simple jump. Although not as comprehensive as C#, it can also meet basic needs. (I am using the Vscode+Emmylua plug-in now, but it seems that I can only jump the method, not the attribute.)

that's all.

Guess you like

Origin blog.csdn.net/qq_39860954/article/details/119703239