使用 javax.script 运行多个脚本的有效方法
我正在开发一个游戏,我希望有多个脚本都实现相同的结构。每个脚本都需要在自己的范围内运行,以便代码不会与其他脚本重叠。例如:
struction.js
function OnInit() {
// Define resources to load, collision vars, etc.
}
function OnLoop() {
// Every loop
}
function ClickEvent() {
// Someone clicked me
}
// Other fun functions
现在,假设我有:“BadGuy.js”、“ReallyReallyBadGuy.js”、“OtherBadGuy.js” - 它们在结构上都与上面类似。在游戏中,每当事件发生时,我想调用适当的函数。
问题归结为效率和速度。我通过为每个脚本实例创建一个引擎(使用 getEngineByName )找到了一个可行的解决方案,但这对我来说似乎并不理想。
如果没有更好的解决方案,我可能会使用每个脚本都有自己独特的类/函数名称。即
BadGuy.js
var BadGuy = new Object();
BadGuy.ClickEvent = function() {
}
I am developing a game where I'd like to have multiple scripts that all implement the same structure. Each script would need to be run in its own scope so that code doesn't overlap other scripts. For example:
structure.js
function OnInit() {
// Define resources to load, collision vars, etc.
}
function OnLoop() {
// Every loop
}
function ClickEvent() {
// Someone clicked me
}
// Other fun functions
Now, lets say I have: "BadGuy.js", "ReallyReallyBadGuy.js", "OtherBadGuy.js" - They all look like the above in terms of structure. Within the game whenever an event takes place, I'd like to invoke the appropriate function.
The problem comes down to efficiency and speed. I found a working solution by creating an engine for each script instance (using getEngineByName
), but that just doesn't seem ideal to me.
If there isn't a better solution, I'll probably resort to each script having its own unique class / function names. I.e.
BadGuy.js
var BadGuy = new Object();
BadGuy.ClickEvent = function() {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要为每个“家伙”创建一个新的 ScriptEngine。您可以在一个引擎中管理它们。因此,对于屠杀你的游戏场景提前表示歉意……
这是我用 Groovy 编写的示例。
I don't think you need to create a new ScriptEngine for every "Guy". You can manage them all in one engine. So with advance apologies for butchering you game scenario.....
Here's a sample I wrote in Groovy.