# FAQs ## How to use xLua distribution package? xLua is currently released as a zip package and can be extracted to the project directory. ## Can xLua be placed in another directory? Yes, but the generated code directory needs to be configured (by default, it is in the Assets\XLua\Gen directory). For details, see the GenPath configuration in XLua Configuration.doc. The important thing to note about changing directories is that the generated code and xLua core code must be in the same assembly. If you want to use the hotfix function, the xLua core code must be in the Assembly-CSharp assembly. ## Does Lua source code only use the txt extension? It can use any extension. If you want to add TextAsset to an installation package (for example, to the Resources directory), Unity does not identify the Lua extension. This is Unity's rule. If you do not add it to the installation package, there is no limit to the extension. For example, in case that you download it to a directory (this is also practicable in hotfix mode), and then read this directory with CustomLoader or by setting package.path. Why does the Lua source code (including examples) of xLua use the txt extension? Because xLua itself is a library, it doesn't provide download functionality, and it's inconvenience to download code from somewhere else during runtime. TextAsset is a simpler solution. ## The editor (or non-il2cpp for Android) runs normally, but when iOS calls a function, "attempt to call a nil value" is reported. By default, il2cpp will strip code, such as engine code, C# system APIs, and third-party dlls. In simple terms, functions in these places will not be compiled into your final release package if your C# code does not access them. Solution: Add a reference (for example, configuring it to LuaCallCSharp, or adding access to that function to your C# code), or use the link.xml configuration (When ReflectionUse is configured, xLua will automatically configure it for you in link.xml) to tell il2cpp not to strip a certain type of code. ## Where can I find Plugins source code and how can I use it? Plugins source code is in the xLua_Project_Root/build. The source code compilation relies on CMake. After installing CMake, execute make_xxxx_yyyy.zz. xxxx stands for the platform, such as iOS or Android; yyyy is the virtual machine to be integrated, including Lua 5.3 and LuaJIT. The file extension is zz. The extension is bat for Windows and sh for other platforms. Windows compilation relies on Visual Studio 2015. Android compilation uses Linux, relies on NDK, and needs to point ANDROID_NDK in the script to the installation directory of NDK. iOS and OS X need to be compiled on a Mac. ## How do I solve the "xlua.access, no field __Hitfix0_Update" error? Follow the [Hotfix Operation Guide](hotfix.md). ## How do I solve the "please install the Tools" error? Do not install Tools to the same level directory as Assets. You can find Tools in the installation package, or in the master directory. ## How do I solve the "This delegate/interface must add to CSharpCallLua: XXX" error? In the editor, xLua can run even without generating code. This prompt appears either because CSharpCallLua was not to the type, or because the code was generated before adding, but no generation was executed again. Solution: After confirming that CSharpCallLua has been added to XXX (type name), clear the code and run it again. If there is no problem with the editor, the error will be reported to the mobile phone. This means that you did not generate the code (execute “XLua/Generate Code”) before release. ## What do I do if executing "XLua/Hotfix Inject In Editor" menu on Unity 5.5 or later versions produces the following prompt: "WARNING: The runtime version supported by this application is unavailable." This is because the injection tool was compiled with .NET 3.5. The Unity 5.5 warning means that MonoBleedingEdge's mono environment does not support .NET 3.5. However, due to backward compatibility, no real problems related to this warning have been found so far. You may find that defining INJECT_WITHOUT_TOOL in nested mode will not produce this warning. However, the problem is that this mode is used for debugging and is not recommended because it may cause some library conflicts. ## How do I trigger an event in hotfix? Firstly, enable private member access using xlua.private_accessible. Then, call delegates using the "&event name" field of the object, for example self\['&MyEvent'\](), where MyEvent is the event name. ## How do I patch Unity Coroutine's implementation function? See the corresponding section of the [Hotfix Operation Guide](hotfix.md). ## Is NGUI (or UGUI/DOTween, etc...) supported? Yes. The most important feature of xLua is that what you write with C# can be originally replaced with Lua, and the plugins available on C# will remain available. ## If debugging is needed, how do I deal with the filepath parameter of CustomLoader? When Lua calls require 'a.b', CustomLoader will be called and the string "a.b" will be injected. You need to understand this string, load the Lua file (from file/memory/network, etc...) and return two things. The first thing is a path that the debugger can understand, for example a/b.lua, which is returned by setting the filepath parameter of the ref type. The second thing is the bytes[] of the source code in UTF8 format, which is returned with the returned value. ## What is generated code? XLua supports one kind of Lua-C# interaction technique, which implements interaction by generating adaptation code between the two. It has better performance and is therefore recommended. Another interaction technique is reflection, which has less impact on the installation package and can be used in scenarios which have lower performance requirements and has installation package size limit. ## How do I solve errors with the code generated before and after changing the interface? Clear the generated code (execute the "Clear Generated Code" menu, which may disappear after restart. Then you can manually delete the entire generated code directory), and then regenerate the code when the compilation is completed. ## When should the code be generated? During the development period, it is not recommended that code be generated, to avoid many compilation failures due to inconsistency and waiting time during compilation of the generated code. The generated code must be executed before the build version for the mobile phone. Automatic execution is recommended. Optimize performance. The generated code must be executed before the performance test because there are significant differences between the generated code and the code not generated. ## Do all C# APIs in CS namespaces occupy high memory? Due to the use of LazyLoad, their existences are just a virtual concepts. For example, for UnityEngine.GameObject, its methods, and properties are loaded only when accessing the first CS.UnityEngine.GameObject or transferring the first instance to Lua. ## In what scenarios are LuaCallSharp and CSharpCallLua used? It depends on the caller and the callee. For example, if you want to call C#'s GameObject, find a function in Lua, or call GameObject's instance methods or properties, the GameObject type needs to be added to LuaCallSharp. If you want to add a Lua function to the UI callback (in this case, C# is the caller and the Lua function is the callee), the delegate declared by the callback needs to be added to CSharpCallLua. Sometimes, it is confusing, like when calling List, for example. Find(Predicate match) and List will of course be added to LuaCallSharp. However, Predicate needs to be added to CSharpCallLua, because the caller of match is C#, and a Lua function is called. A more unthinkable way: When you see, "This delegate/interface must add to CSharpCallLua: XXX", just add XXX to CSharpCallLua. ## Will gc alloc appear in value type transfer? If you are using the delegate to call a Lua function, if the LuaTable and LuaFunction that you use have no gc interface, or if there is an array, the following value types have no gc: 1. All the basic value types (all integers, all floating-point numbers, decimals) 2. All enumerated types 3. The field contains only the struct of value type, and it can nest other struct. For 2 and 3, pleases add those types to GCOptimize. ## Is reflection available on iOS? There are two restrictions on iOS: 1. no JIT; 2. code stripping; When C# calls Lua via delegates or interfaces, using reflection emit instead of the generated code relies on JIT, so this is currently only available in the editor mode. If Lua calls C#, it will be mainly affected by code stripping. In this case, you can configure ReflectionUse (but not LuaCallSharp), and execute "Generate Code". No package code except link.xml will be generated for the type this time. Set this type to 'not to be stripped'. In short, only CSharpCallLua is necessary (a little code of this type is generated), and reflection can be used in LuaCallSharp generation. ## Is calling generic methods supported? This is partially supported. See [Example 9 for the degree of support.](../Examples/09_GenericMethod/) There are other ways to call generic methods. If it is a static method, you can write a package to instantiate the generic method. If it is a member method, xLua supports the extension method. You can add an extension method to instantiate a generic method. This extension method is just like an ordinary member method. ```csharp // C# public static Button GetButton(this GameObject go) { return go.GetComponent