Java based on LuaJ uses Lua's battle framework
The framework is based on the secondary encapsulation implementation of LuaJ(https://luaj.sourceforge.net)
- Call encapsulation of LuAj's base interface
- Simplify the setup of the LuaJ environment
- Manages Lua battles and provides interfaces
- Lua object-oriented usage scheme(class.lua)
- Example of Lua combat framework
- Luaj's Guide to stomping pits
This framework provides a Java-Lua battle framework with the following advantages and disadvantages
- Advantages:
- Common logic LUA code: The front and rear end can use the same set of combat logic code based on the same set of language, just need to design the common war framework, can realize one piece of code used in two places. The front and back end programmers can also develop together based on this framework, which can improve the development efficiency to a certain extent for both state synchronization and frame synchronization.
- The LuaJ framework is by far the most efficient way to invoke Lua in Java.
- Lua code can be used directly without compilation, and you can design a set of hot logic through LuaJ
- Disadvantages(Guidelines on pit):
- More heap and meta space for the JVM
Luaj provides two compilers, LuaC and LuaJC. Luac will create a LuaClosure object after the LOAD file, and it will parse the lua command line by line as it runs, but it won't run very efficiently. The principle of LuaJc is to compile it into Java bytecode and load it into memory through its JavaLoader (inheriting ClassLoader), which is equivalent to compiling and running it many times. However, those familiar with the Java class loading mechanism should know that during this process, the JVM creates Klass information in the Meta space and stores the Klass reference in the ClassLoader. Meanwhile, Luaj's JavaLoader also does one thing: cache dynamically generated bytecode byte[] In this case, starting a Lua environment increases the heap and meta footprint of the JVM.
- Not as efficient as native Java
The authors of LuAJ describe that LuAJ is almost as efficient or even more efficient than native Lua virtual machines. But in my actual tests, I didn't compare Luaj to native Lua, I compared Luaj to native Java, and the performance was far worse. By observing the compiled source code of luaj, we can also find that an operation like i++, for example, can be directly operated on the basic data type int in Java, but in luaj, int is wrapped by the Integer wrapper class (LuaInteger).
- Both a strength and a weakness
- Flexible Lua code
Flexibility is a double-edged sword. If used well, it can greatly improve development efficiency. If not used well, it can not only improve development efficiency, but also bring great pain to development and maintenance, which is very important for the ability of low-level development.
From what has been discussed above:Whether you want to use Lua as your Java server's combat logic code depends on the situation, whether the advantages of Lua give you great benefits of your code and you can live with its disadvantages or have other solutions to overcome its disadvantages.
Welcome to use, any bugs and optimization requirements, welcome to discuss issues
https://hjcenry.com/lua-java-battle/doc/
See BattleDemoService for a complete code example
<dependency>
<groupId>io.github.hjcenry</groupId>
<artifactId>lua-java-battle</artifactId>
<version>1.0</version>
</dependency>
LuaInit.LuaInitBuilder luaInit=LuaInit.builder();
luaInit.preScript("print('Hello Lua Battle!!!')");
// Set the Lua root path
luaInit.luaRootPath("F:\\project\\lua-java-battle\\src\\main\\lua\\");
// Load the Lua call interface directory
luaInit.luaLoadDirectories("interface");
// Load the Lua main file
luaInit.luaLoadFiles("FightManager.lua");
// Show log
luaInit.showLog(true);
LuaBattleManager.getInstance().init(luaInit.build());
// All the methods you need to start with
this.xxxFunction=this.initFunction("XXX.xxx");
this.xxxFunction2=this.initFunction("xxx");
this.xxxFunction.invoke();
this.xxxFunction2.invoke(LuaNumber.valueOf(123));
The above is a simple example of how this framework should be used. BattleDemoService provides a set of examples comparing the completion of the Lua combat framework
- Battle Service Example
- Example of Lua-Java data conversion tool
- Example of Lua-Java library conversion tool
- https://www.lua.org/ lua官网
- https://luaj.sourceforge.net luaj官网
- Wechat:hjcenry