从字符串执行 as3 代码

发布于 2024-10-28 12:25:48 字数 309 浏览 8 评论 0原文

我正在开发一个简单的文本角色扮演游戏,并将所有数据对象存储为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吖咩 2024-11-04 12:25:48

ActionScript 3 不再包含 eval 函数,因此无法直接执行此操作。但是,您可以使用自己的简单解释器来手动执行此操作。像这样的事情:

var item:XML =
    <health_item>
        <action name="hp_change" value="15"/>
    </health_item>;

检查 ActionScript 中的操作名称,找到相应的函数并使用“value”参数调用它:

for each (var action:XML in item.action) {
    var actionName:String = action.@name;

    //switch variant
    switch (actionName) {
        case "hp_change":
            hpChange(action.@value);
            break;
        //and so on for other known actions
    }

    //direct call by name variant
    if (hasOwnProperty(actionName)) {
        this[actionName](action.@value);
    } else {
         //report error
    }
}

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:

var item:XML =
    <health_item>
        <action name="hp_change" value="15"/>
    </health_item>;

Check action name in ActionScript, find corresponding function and call it with "value" argument:

for each (var action:XML in item.action) {
    var actionName:String = action.@name;

    //switch variant
    switch (actionName) {
        case "hp_change":
            hpChange(action.@value);
            break;
        //and so on for other known actions
    }

    //direct call by name variant
    if (hasOwnProperty(actionName)) {
        this[actionName](action.@value);
    } else {
         //report error
    }
}
毁我热情 2024-11-04 12:25:48

我不确定我是否理解您想要如何构建这个想法。

如果您想使用类似于 eval 的东西,则没有本地方法可以做到这一点。
不过,您可以检查此并查看示例这里

现在,出于多种原因,我不建议使用这样的东西。多想一想,你自己就会想出一些办法。

我建议实现一个简单的解析器并从 xml 加载命令,然后解释提供的数据并执行相应的命令:

<command id="hurt" params="-15"/>

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:

<command id="hurt" params="-15"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文