从字符串执行 as3 代码
我正在开发一个简单的文本角色扮演游戏,并将所有数据对象存储为 xml 文件,但我需要能够为许多事情运行一些简单的语句。
我做了一些谷歌搜索,但没有想到太多。
我想做的是采用简单的语句,例如:
playerhp += 15;
or
if(playerisvampire == 1) {blah blah;}
并将它们嵌入到 xml 结构中,以便项目或对话行可以包含检查和可执行代码,使 rpg 类更多地作为解释器和接口。这样的事可能吗?
I'm working on a simple text rpg and I'm storing all of my data objects out as xml files but I need to be able to run some simple statements for many things.
I have done some googling I havent come up with much.
What I'm trying to do is take simple statements like:
playerhp += 15;
or
if(playerisvampire == 1) {blah blah;}
and embed them inside of the xml structure so that an item or conversation line can contain the checks and executable code leaving the rpg class as more of an interpreter and interface. Is such a thing possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ActionScript 3 不再包含 eval 函数,因此无法直接执行此操作。但是,您可以使用自己的简单解释器来手动执行此操作。像这样的事情:
检查 ActionScript 中的操作名称,找到相应的函数并使用“value”参数调用它:
ActionScript 3 contains no eval function anymore, so this is not possible directly. However, you can roll your own simple interpreter to do this manually. Something like this:
Check action name in ActionScript, find corresponding function and call it with "value" argument:
我不确定我是否理解您想要如何构建这个想法。
如果您想使用类似于 eval 的东西,则没有本地方法可以做到这一点。
不过,您可以检查此库并查看示例这里
现在,出于多种原因,我不建议使用这样的东西。多想一想,你自己就会想出一些办法。
我建议实现一个简单的解析器并从 xml 加载命令,然后解释提供的数据并执行相应的命令:
I am not sure I understand how you want to architect this idea.
If you want to use something similar to eval, there is no native way of doing this.
Although, you can check this library and see and example here
Now, I would not recommend to use such thing for many reasons. Think about it a bit more, and you will figure out some by yourself.
I would suggest to implement a simple parser and load commands from your xml, then just interpret the data provided and execute the correspondent command: