The use of script engine in Java in games

original
2015/11/07 18:43
Reading amount 1.6K

First, check which script engines are currently supported by the jvm jdk6 edition

 ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories(); if (engineFactories.size() == 0) { System. out. println ("This JVM does not support any script engine yet"); return; } System. out. println ("The script engines supported by this JVM are:"); for (ScriptEngineFactory engineFactory : engineFactories) { System. out. println ("engine name:"+engineFactory. getEngineName()); System. out. println (" tNames recognized by ScriptEngineManager:"+engineFactory. getNames()); }
result:
 The script engines supported by this JVM are: Engine name: Mozilla Rhino Name recognized by ScriptEngineManager: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]
It can be seen that the jvm supports JavaScript script by default. Of course, the jvm provides corresponding interfaces and can access third-party script engines, such as Python, Expression Language and other support. Currently referenced jython.jar,jython-engine.jar,jexl.jar, Review the supported engines.

 The script engines supported by this JVM are: Engine name: Mozilla Rhino Name recognized by ScriptEngineManager: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript] Engine name: jython Name recognized by ScriptEngineManager: [jython, python] Engine name: JEXL Engine Name recognized by ScriptEngineManager: [JEXL, Jexl, jexl, JEXL2, Jexl2, jexl2]

Well, since the jvm can extend to support so many scripts, here are some practical examples:

      1. For example, there is such a demand , there is a reward for players based on the players Level For calculation, for example, 100 *level Here we can use two methods to solve the problem:
The first kind: write death in the code, and judge when it is a designated reward *level
      Second, use expressions, and then use the engine to execute expressions. See the following example, using the JavaScript engine:

 ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("javascript"); String exp = "100 * level"; scriptEngine.put("level", 4); Object result = scriptEngine.eval(exp); System.out.println(exp + " = " + result);
In this way, we can give our reward expression to an expression processor for unified processing. Here we use JavaScript to execute expressions. Of course, we can use a more professional expression execution engine, jexl, for better performance.
 ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("jexl");

2. More complex requirements That is, the combat system needs more logic than just parsing expressions. For example, the most commonly used calculation of damage requires a very complex logic, which is most clear about planning. So we provide interfaces and then implement them by scripts. Our programs are only responsible for calling.

For example, this interface is implemented by Python:
 Def increaseHp (attackUnit, defenceUnit, skill, buff, type): # attackUnit skill releaser, defenceUnit skill recruit, skill skill, buff, type 1=value 2=description .... Omitted
The damage is finally calculated through a series of parameters.
Or look at a simpler Python script, test.py:
 def addition(num1,num2): return num1+num2;
 ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("python"); String jsFileName="scripts/test. py";//Read the py file FileReader reader = new FileReader(jsFileName); //  Execute the specified script engine.eval(reader); if (engine instanceof Invocable) { Invocable invoke = (Invocable) engine; Integer c = (Integer) invoke.invokeFunction("addition", 2, 3); System.out.println("c = " + c); }
Well, let's introduce these two requirements for the time being. There must be requirements applied in other places.

I forgot to mention that scripts such as JavaScript, Python, and lua are interpreted and executed, and do not need to be compiled. Now, all games often use scripts to implement hot updates.

Expand to read the full text
Loading
Click to join the discussion 🔥 (2) Post and join the discussion 🔥
Reward
two comment
six Collection
zero fabulous
 Back to top
Top