XML 文件中的游戏逻辑 [英] Game Logic in XML Files

查看:42
本文介绍了XML 文件中的游戏逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理游戏对话文件(玩家和不可玩角色之间的对话),其中对话选择及其结果取决于某些条件并导致某些动作.现在,我可以编写一个简单的解析器来处理某种语言来指定前置条件和后置条件,但是我的一个朋友建议使用 XML.条件可以存储为对话元素的属性,而选择和动作是内部元素.然后我会使用一个 eval 函数来解析这些条件和语句(我使用 Ruby 来制作这个游戏).为了使这种方法更简单,我可以编写一个简单的 GUI 来操作这些文件,而不必担心丑陋的 XML.

I'm dealing with game dialogue files (conversation between player and non-playable-characters) where dialogue choices and their outcome depend on certain conditions and result in certain actions. Now, I could write a simple parser to handle some sort of language for specifying the pre and post-conditions, but a friend of mine suggested using XML. Conditions could be stored as attributes of a dialogue element and choices and actions are inner elements. I'd then use an eval function to parse these conditions and statements (I'm using Ruby to make this game). To make such an approach simpler, I could then write a simple GUI to manipulate these files without worrying about ugly XML.

但在我看来,处理 XML 文件中的逻辑是一个奇怪的选择.我的理解是 XML 文件用于数据的存储和交换,我总是读到关于人们如何过度使用 XML 的各种事情的咆哮.我的朋友回答说 XML 是如何用于一切事物的,包括 XHTML 和 这个项目符号描述语言(也说明了一些逻辑).

But it strikes me as an odd choice to handle logic in XML files. My understanding is that XML files are for the storage and exchange of data, and I always read rants about how people overuse XML for all sorts of things it wasn't designed for. My friends responds by noting how XML IS used for everything, including XHTML and this bullet description language (which also illustrates some logic).

老实说,使用 XML 会为我简化很多事情.编写解析器可能既痛苦又耗时,我的要求通常很简单.但是这样真的好吗,还是以后会后悔这样的选择?

To be honest, using XML would simplify a lot of things for me. Writing a parser can be painful and time consuming, and my requirements are generally simple. But is it really okay or would I regret such a choice later?

对于对细节感兴趣的人,以下是 XML 文件中基本对话交换的样子:

For people interested in details, here's what a basic dialogue exchange might look like in an XML file:

<dialogue id="101" condition="!npc.carsFixed">
  <message>Man, fix my car!</message>
  <choices>
    <choice condition="hero.carFixingSkill > 5" priority="7" id="Sure!">
      <command>hero.carFixingSkills += 1</command>
      <command>npc.carFixed = true</command>
      <command>hero.playSmokeAnimation()</command>
      <command>nextDialogue = 104</command>
    </choice>
    <choice condition="hero.carFixingSkill <= 5" id="I can't...">
      <command>nextDialogue = 105</command>
    </choice>
    <choice id="Fix it yourself">
      <command>npc.likesHero -= 1</command>
    </choice>
  </choices>
</dialogue>

如果用 Ruby 编写,对应的代码是:

The corresponding code if written in Ruby would be:

def dialogue101
  if !npc.carsFixed
    showMessage("Man, fix my car!")
    choices = []
    if hero.carFixingSkill > 5
      choices.push(Choice.new("Sure!", 7)) 
    else
      choices.push(Choice.new("I can't"))
    end
    choices.push(Choice.new("Fix it yourself"))
    choices = selectTopPriority(choices) if choices.size > 4
    result = showChoices(choices)
    case result
    when "Sure"
      hero.carFixingSkills += 1
      npc.carFixed = true
      hero.playSmokeAnimation
      dialogue104
    when "I can't"
      dialogue105
    when "Fix it yourself"
      npc.likesHero -= 1
    end
  end
end

likesHero 和 carFixingSkills 之类的东西是玩家和 NPC 可以拥有的知识块,在实际实现中可能会存储在哈希中.我发现对话文件方法更灵活,因为我可以让编辑器轻松编辑对话和条件/动作,并且因为游戏对话树的复杂性.Ruby 或 Lua 之类的脚本语言会有所帮助,但它需要复杂的结构来处理此类树的逻辑.

Stuff like likesHero and carFixingSkills are knowledge pieces player and NPCs can have, which would probably be stored in a hash in the real implementation. I find the dialogue file approach more flexible because I could make an editor to easily edit dialogue and conditions/actions, and because of the complex nature of game conversation trees. A scripting language like Ruby or Lua helps, but it'll require complex structures to handle to logic of such trees.

回到最初的问题,XML 是适合这项工作的工具还是我遗漏了什么?

Back to the original question, is XML the right tool for the job or am I missing something?

推荐答案

除非您的游戏只有不到六个独特的对话框,否则您绝对应该将这些信息放在某种数据文件中.XML 是该格式的有力竞争者.我不会说 Ruby,所以在这种情况下它可能不起作用,但另一种选择是直接在 Ruby 代码中将对话框定义为数据.(我知道这在 Lua、Python 和 Javascript 中效果很好……我认为在 Ruby 中定义嵌套数据结构也很容易.)

Unless your game will have less than half a dozen unique dialogs, you should definitely put this information in some kind of data file. XML is a strong contender for the format. I don't speak Ruby, so it may not work in this case, but another option would be to define the dialog as data directly in Ruby code. (I know this would work pretty well in Lua, Python, and Javascript... I assume defining nested data structures is also easy in Ruby.)

我们使用 XML 文件定义了 Pirates of the Burning Sea 中的所有静态数据,这是一个很好的方法.拥有这样的数据格式可以让非程序员控制数据,并使程序员腾出时间来处理功能而不是数据输入.将这些数据文件设为文本意味着您可以将它们置于源代码控制之下,以便您知道它们何时发生变化.

We used XML files to define all the static data in Pirates of the Burning Sea, and it was a great way to go. Having a data format like this lets non-programmers control the data and frees up the programmers to work on features instead of data entry. Having those data files be text means that you can keep them under source control so you can tell when they change.

这篇关于XML 文件中的游戏逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆