Script engine in Java in-game usage

Source: Internet
Author: User
Tags what scripting python script

First look at what scripting engines are currently supported by the JVM and are currently using the jdk6 version

Scriptenginemanager Scriptenginemanager = new Scriptenginemanager (); list<scriptenginefactory> enginefactories = Scriptenginemanager.getenginefactories (); if ( Enginefactories.size () = = 0) {        System.out.println ("This JVM does not yet support any scripting engine");        return;} System.out.println ("The script engine supported by this JVM has:"); for (Scriptenginefactory enginefactory:enginefactories) {System.out.println (" Engine Name: "+ enginefactory.getenginename ()); System.out.println ("\ t can be Scriptenginemanager recognized name:" + enginefactory.getnames ());}
Results:
The scripting engines supported by this JVM are: Engine name: Mozilla Rhino    can be Scriptenginemanager recognized by name: [JS, Rhino, JavaScript, JavaScript, ECMAScript, ECMAScript
It can be known that the JVM is supported by default JavaScript scripts, of course, the JVM provides the appropriate interface, can be connected to third-party scripting engine, such as Python, Expression Language and other support. the Jython.jar,jython-engine.jar,jexl.jar is currently referenced and the supported engines are re-viewed.

The scripting engines supported by this JVM are: Engine name: Mozilla Rhino    can be Scriptenginemanager recognized by name: [JS, Rhino, JavaScript, JavaScript, ECMAScript, ECMAScript] Engine name: Jython    can be Scriptenginemanager recognized by name: [Jython, Python] engine name: JEXL    Engines Names that can be recognized by Scriptenginemanager: [Jexl, Jexl, Jexl, JEXL2, JEXL2, JEXL2]

Well, since the JVM can extend support for so many scripts, here are a few examples of practical use:

      1. For example, there is a need , there is a reward for players to be based on the player is calculated, such as the need to 100*level
      The first kind: Write dead in the code, judge when the reward is specified *level
      second: Using an expression, and then using the engine to execute the expression, look at the following example, using the JavaScript engine:

ScriptEngine scriptengine = new Scriptenginemanager (). Getenginebyname ("JavaScript"); String exp = "level"; Scriptengine.put ("Level", 4); Object result = Scriptengine.eval (exp); SYSTEM.OUT.PRINTLN (exp + "=" + result);
This allows us to give our reward expression to a single expression processor for unified processing. Here we take advantage of the ability of JavaScript to perform expressions and, of course, take advantage of a more professional expression to execute the engine JEXL, which performs better.
ScriptEngine scriptengine = new Scriptenginemanager (). Getenginebyname ("Jexl");

2. There are more complex requirements , that is, the combat system, not just analytic expressions, need more logic, such as the most commonly used computational damage, need a very complex logic, and this logic is the most clear planning, so we provide the interface, and then by the script to implement, Our program is only responsible for invoking the line.

For example, this interface is implemented by Python:
def INCREASEHP (attackunit,defenceunit,skill,buff,type): #attackUnit skill release, defenceunit skill recruit, skill skill, buff, type 1 = value 2= to describe .... Omitted
The damage was calculated by 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);  Executes 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, for the moment, the two needs are introduced, and there must be a need to apply them elsewhere.

forget to say the script, such as Javascript,python,lua, are interpreted to execute, do not need to compile, all the games now often use the script to implement hot updates.

Script engine in Java in-game usage

Related Article

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.