游戏《饥荒》开发架构技术分析

来源:互联网 发布:2017年淘宝怎么做 编辑:程序博客网 时间:2024/04/28 22:14

翻译来自:http://forums.kleientertainment.com/topic/25850-wots-the-diff-prefabs-components-stategraphs-and-brains/




Wots The Diff?? Prefabs, Components, Stategraphs, And Brains
有啥区别? 预设,组件,状态图,大脑


I understand that there might be some confusion about these four parts of an entity, so here's a brief description of when to use each one.
 我知道一个实体的这四个部分可能会让人困惑,所以接下来我会简述一下什么时候用哪一个。


Prefabs and Components:
预设和组件


These two are the meat and potatoes of any entity in the world and where you'll be spending the most time. There is a simple way to think of the difference:
这两个是,在饥荒世界里,任何实体最基本的组成部分。你会花费大量功夫在它俩上面。它俩最简单的区别就是:
 
Prefabs are what a thing IS.
Components are what a thing DOES.
预设:这个物体是什么
组件:这个物体做什么


Lets look at the firepit as an example. What is it? A firepit! So it becomes a "firepit" prefab. What does it do? Well, it gets built, and it burns, and takes fuel, and can be looked at, and produces light... So we don't make a "firepit" component, instead we have "burnable" component, and "fueled" component, and so forth.
现在举个例子: 火堆,它是什么?火堆!所以它就是一个“预设”。它会做什么?外欧,它能被建造,它能燃烧,它需要燃料,它能被看到,它能产生亮光... 所以,我们不会制作一个“火堆”组件,而是让它拥有“可燃”,“可加燃料”等组件。


So components are where all the work happens, all the logic and data storage. A prefab is an instruction that says, "These are the components to use, and this is how you configure them.
所以,所有的活儿,所有的逻辑和数据,都在组件里发生,在组件里存储。而预设的说明是:拥有组件,可以使用它们,可以配置它们。


"Most prefabs have a main function that basically does just that: attaches all the components and configures them, and for many prefabs this is the entire story, such as the "carrot" prefab.
大多数的预设拥有一个主函数,它基本的工作是:添加各种组件,然后配置它们。大多数预设只做这些就可以了,比如“胡萝卜”预设


The place it gets a little funny is that some components need to be told how to do something. For example, the combat component needs to know, "How do I choose a new target if I lose my target?". If we take a look at the "frog" prefab, we can see that it adds a combat component, and then calls inst.components.combat:SetRetargetFunction(3, retargetfn).
有些组件,比较有意思,它们需要被告诉怎么去做。比如“战斗”组件,它需要被告诉“如果我失去了目标,我该怎么选择新目标?” 如果你看一下“青蛙”预设,你会看到它挂载了一个“战斗”组件,然后调用接口“设置重定向目标”,把“重定向目标”函数传给它。
 
If we look, retargetfn is defined in the prefab, and it's an instruction for the combat component on how to pick a new target. So basically, it's still a configuration of the component. That's all prefabs really do!
我们会发现,接口“重定向目标”函数就定义在“青蛙”预设里,这个函数就是指示“战斗”组件如何选择一个新目标。所以,基本上,这个函数还是算一个配置,来配置“战斗”组件。这就是预设要做的全部事情。
 
Some prefab files define multiple prefabs. Usually these are things with similar components, but different configurations. "staff.lua" contains all the staves. Each one shares a common base, but then has different specialized configurations for each colour of gem.So when you are making your own mods, ask yourself, is this mod a new thing or a new something a thing can do. If you want to make a sword that shoots lightning, you've got a sword (prefab) which can shoot lighting (component). The cool thing about approaching the problem this way is that you can then put your lightning shooter component on a different weapon, or on a hat, or a spider queen, or....!
一些预设文件会定义多个预设。通常,有些实体拥有一致的组件,但组件的配置不同。
 


Stategraphs and Brains:


These two are a bit fancier and most of the prefabs in the game don't use them at all! This is because, as mentioned, components are really where the hard work is done. But it's worth knowing about them in case they suit your needs.


 


Stategraphs:


Stategraphs are mainly used for controlling animation and timing. We can look at "SGeyeplant", the stategraph for the Eye Plants that spawn with the Lure Plant, as an example. Its first state is "spawn" which gets played right when the Eye Plant is created. Notice that its "onenter" function plays some animations and plays a sound. It also listens for an "animover" event which gets triggered when the current animation finishes playing, which moves it to the "idle" state. All the idle state does is play the idle animation.Normally that kind of behaviour, "do this thing and when it's done do that thing" is kind of a drudgery to code up, but the stategraph makes it straightforward to handle. We just tell it to go to the "spawn" state and when it's done it will automatically end up idling.


 


Another interesting example is the "attack" state in SGtentacle. We wanted the tentacles to play sounds and deal damage during the "whipping" part of their animation, which occur at certain frames. So this state sets up a timeline which will do certain things at frames 2, 7, 15, etc. Again, we can set this all up in the stategraph, and then the combat component just says "go to your attack state!" and the timing gets taken care of over here.


 


Brains:


Brains are the least used of these four elements, because they are the most high-level and generally entities don't needs this level of complexity. Their purpose is for AI and decision-making so only the complicated creatures need this.I'm not going to describe them in detail here, but in a nutshell: Brains use a system called a "behaviour tree" to make decisions. Every now and then it goes to the first item in the tree and checks, "can I do this?" and if not, it goes to the next one, and then next one, and it's children, and their children, until it finds something it can do. Then it does that! So this means things higher in the list have more priority, which lets you do things like make pigs prefer fighting to eating, but to panic when they are on fire even if they were fighting.


 


Generally each behaviour in the tree boils down to sending instructions to components: components still end up doing all the real "work". So the ChaseAndAttack behaviour, for example, sends instructions to the Locomotor (for movement) and Combat (for attacking) components, and those components handle the details.So as mentioned, most mods will never need to touch brains or stategraphs. But in case you need to modify one, now hopefully you have an idea of what their purpose is.


 


All in all, I think understanding the prefab/component distinction is critical to getting the most out of Don't Starve modding. If any of this wasn't clear or if you have further questions, please ask below!
0 0