用 Java 编写脚本
我和一些朋友正在用 Java 编写 MORPG,我们想使用脚本语言来,例如。 创建任务。
我们没有 Java 脚本编写经验。 我们使用过Python,但对它非常缺乏经验。 我们其中一位也使用过 Javascript。
我们应该使用什么脚本语言? 我们不应该使用哪种脚本语言?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我负责一个相当大的混合 Java/Jython 系统。 我们使用 java 进行核心 API 开发,然后使用 Jython 将 Java 对象连接在一起。 这是在科学计算环境中,我们需要能够快速组合临时数据分析脚本。
如果我今天从头开始启动这个系统,我不会选择 Jython 作为脚本语言。 我很喜欢 Python,但我经常遇到 Python 类型系统和 Java 类型系统之间尴尬的不匹配问题。 例如,如果你只想要一个哈希表,你应该使用Python字典还是Java HashMap? 根据您是只是在 Python 代码中本地使用数据结构还是跨 Java 边界传递数据,决定可能会有所不同。 Jython 为您执行一定量的类型强制,但它并不完美。 当使用脚本语言的首要目的是提高生产力时,甚至不得不考虑这样的问题是很烦人的。
我认为 JavaScript 或 JRuby 也会有类似的问题。 今天我会选择一种专门针对 JVM 并利用 Java 类型系统的脚本语言。 显而易见的候选者是 Groovy 和 Beanshell; Groovy 最近似乎势头强劲,所以我会仔细研究它。
I'm responsible for a fairly large hybrid Java/Jython system. We use java for core API development, then wire Java objects together using Jython. This is in a scientific computing environment where we need to be able to put together ad-hoc data analysis scripts quickly.
If I were starting this system from scratch today, I would not choose Jython as the scripting language. I like Python fine, but I frequently encounter awkward mismatches between the Python type system and the Java type system. For example, if you just want a hashtable, should you use a Python dictionary or a Java HashMap? The decision might be different depending on whether you are just using the data structure locally in Python code or passing it across the Java boundary. Jython does a certain amount of type coercion for you, but it's not perfect. It's annoying to even have to think about issues like this when the purpose of using a scripting language in the first place is to enhance your productivity.
I assume JavaScript or JRuby would have similar issues. Today I would choose a scripting language that is specifically targeted to the JVM and leverages the Java type system. The obvious candidates are Groovy and Beanshell; Groovy seems to have been picking up momentum lately so I'd look most closely at it.
我同意 Viktor 的 Jython 建议。 除此之外,还有 JavaScript(您已经提到过,它通过
javax.script
包内置于 Java 6+ 中),Groovy 和 JRuby 也值得一看。顺便说一下,您应该看看 Wyvern,它也是一个用 Java 编写并使用 Jython 进行脚本编写的 MMORPG。 Steve Yegge,其作者,不时对此有很多话要说。 :-)
I agree with Viktor's Jython suggestion. Other than that and JavaScript (which you've mentioned, and is built into Java 6+ via the
javax.script
package), Groovy and JRuby are also worth looking at too.By the way, you should look at Wyvern, also an MMORPG written in Java and using Jython for scripting. Steve Yegge, its author, has much to say about it from time to time. :-)
Java 支持多种(脚本)语言,维基百科此处和此处。 您可能应该选择具有强大 DSL 和元编程功能的语言,例如 Clojure。
但如果您需要更简单的东西,JavaScript 可能是一个可行的替代方案。
Java supports a variety of (scripting) languages, some are listed in Wikipedia here and here. You probably should choose language with powerful DSL and metaprogramming capabilities, such as Clojure.
But if you need something simpler, JavaScript might be a viable alternative.
Jython 怎么样?
http://www.jython.org/Project/
How about Jython?
http://www.jython.org/Project/
创建您自己的专用脚本语言怎么样? 如果您的应用程序是用 java 编写的,则可以使用 ANTLR (http://www.antlr.org/ )来创建您的语言解析代码。
我这么说的原因是因为通用脚本语言可能提供太多的功能(因为我假设该脚本仅用于任务)。
但是,如果创建自己的语言太难,那么上述任何建议都有效 - 您只需弄清楚如何将游戏的运行时绑定到脚本中。 我还建议 Lua (http://www.lua.org/) 作为许多游戏的另一个选择使用。
what about creating your own specialized scripting language? If your app is written with java, you can use ANTLR (http://www.antlr.org/) to create your language parsing code.
The reason I say this is because a general purpose scripting language may provide too much power (because the script it to be used for quests only i assume).
But if making your own language is too hard then any of the above suggestions works - you just have to figure out how to bind the game's runtime into the script. I also suggest Lua (http://www.lua.org/) as another choice that lots of games use.
简短版本
不要使用脚本语言! 相反,应关注可配置性(这是非程序员也能做好的事情)。
更长的版本
支持拥有脚本语言的一个经常使用的论点是它允许较少的程序员完成更琐碎的任务。 不要相信这一点,它不会节省任何时间,因为真正的程序员已经不花时间完成琐碎的任务。 以可配置性而不是脚本为目标,这样您将复杂的算法和概念落入无能的游戏设计师手中的风险就会大大降低。 :)
缺乏热交换(编辑并继续)可能是在 MMOG 中实现脚本语言的一个原因(你不想为了一个小的代码更改而重新加载整个游戏),但是使用 Java,带有内置的在热插拔中,您确实没有理由在上面添加脚本语言。
我花了很多年的时间思考这些问题; 那天我自己为 MMOG 实现了完整的脚本语言、IDE、VM、调试器等。 从此,我变得更加聪明了。
如果您仍然选择走上无限糟糕的不归路,请记住以下几点。
迄今为止,我还没有见过任何 DSL 能够改善这种情况(获得更易于维护的产品)。 我自己也曾将 Python 集成到我的独立游戏引擎中,但最终我意识到并把它撕掉了。 “Stackless Python”只是“不可维护但速度快”的一种说法。 如果我错了,请有人纠正我吗?
Short version
Don’t use a scripting language! Instead focus on configurability (which is something that a non-programmer can do well).
Longer version
One oft-used argument in favour of having a scripting language is that it allows for lesser programmers to more trivial tasks. Don't belive this, it will not save you any time, since trivial tasks are already accomplished by real programmers in no time. Aim for configurability instead of scripting, and you will have a much lower risk of bleeding over complex algorithms and concepts into the incapable hands of game designers. :)
Lack of hotswapping (edit-and-continue) would have been a reason to implement a scripting language in an MMOG (you don’t want to reload the whole game for a minor code change), but using Java, with built-in hotswap, you really have no reason for adding a scripting language on top.
I have spent years pondering these questions; in the day I implemented a complete scripting language, IDE, VM, debugger, etc for an MMOG myself. Since, I have grown wiser.
If you still choose to go down the infinitely crappy path of no return, keep the following in mind.
To date, I have never seen a DSL that improved the situation (getting a more maintainable product). Myself, I integrated Python into my indie game engine, but eventually came to my senses and ripped it out. "Stackless Python" is just a way of saying "unmaintainable but fast". Please, anyone correct me if I'm wrong?
请参阅 Java:嵌入到 Java 桌面应用程序的脚本语言(宏)
See Java: Scripting language (macro) to embed into a Java desktop application
您有很多选择:
甚至可能是 BeanShell (http://www.beanshell.org/)
我自己就是 Python 的粉丝,所以我推荐 Jython,但它们可能都是合理的选择。
You have quite a few options:
Possibly even BeanShell (http://www.beanshell.org/)
I'm a fan of Python myself so I'd recommend Jython, but they're probably all reasonable options.
为此我不得不推荐 Javascript。 Mozilla Rhino http://www.mozilla.org/rhino/ 是一个出色的实现,适合完美满足您的需求。
由于熟悉,我推荐 Javascript 而不是 Jython 或 JRuby。 Trivial Javascript 遵循任何人都可以使用的非常熟悉的语法。 然而,如果有人想做更激烈的事情,Javascript 是一种非常强大的函数式编程语言。
我经常专业地使用 Groovy 和 Ruby,并且相信它们的目的最适合编写具有特别复杂逻辑的应用程序部分,而 Java 编写起来很麻烦。 作为在游戏中使用的嵌入式通用脚本语言,Javascript 是一个更好的选择。 我没有使用过 Python,但它在语法上与 Ruby 相似,我相信它的用途也相似。
I would have to recommend Javascript for this purpose. Mozilla Rhino http://www.mozilla.org/rhino/ is an excellent implementation that would fit your needs perfectly.
I recommend Javascript over Jython or JRuby because of familiarity. Trivial Javascript follows a very familiar syntax that anybody can use. However if someone wants to do something more intense, Javascript is a very powerful functional programming language.
I regularly use Groovy and Ruby professionally and believe that their purpose is best for writing parts of applications with particularly complex logic where Java is cumbersome to write. Javascript is a much better choice as an embedded, general scripting language to use in a game. I haven't used Python, but it's syntactically similar to Ruby and I would believe it's purpose would also be similar.
LuaJ 似乎是将 Lua 嵌入到 Java 中的好方法:
http://sourceforge.net/projects/luaj/
LuaJ seems to be a nice way to embed Lua into Java:
http://sourceforge.net/projects/luaj/
我是 Python/Jython 的忠实粉丝,因为它的语法简洁 - 如果您有一些 Python 经验,这可能适合您。
否则,Groovy 是基于 Java 语法的,如果您的大多数开发人员都是 Java 人员,那么学习曲线可能会更容易。 它还具有与 Java 语言和库联系更紧密的优点。
如果您想要简单的脚本编写,Beanshell 会很好 - 它不支持类。 然而,我认为它在过去几年中没有得到任何支持(JSR 似乎将其消灭了......),因此如果支持对您很重要,那么这可能是一个糟糕的选择。
I am a big fan of Python/Jython due to the clean syntax - which may suit you if you have some python experience.
Otherwise Groovy which is based on Java syntax and may be an easier learning curve if most of your developers are Java guys. It also has the advantage of closer ties with the Java language and libraries.
Beanshell is good if you have simple scripting in mind - it doesn't support classes. However I don't think it has had any support over the last few years (the JSR seemed to kill it off...) so is perhaps a bad choice if support is important to you.