1. 程式人生 > >遊戲《饑荒》開發架構技術分析

遊戲《饑荒》開發架構技術分析

翻譯來自: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!