2016-07-06 58 views
-1

編輯:我正在編輯這個問題,因爲它是我唯一的低票問題,我試圖得到一個問題禁止解除。使用xml文件在c中創建對象#

我目前正在使用基於文本的遊戲的xml文件。我已經寫過這個文件,而且我知道如何從文件中讀取數據,但是我很困惑如何從文件中創建實際的對象。我的問題是如何從xml文件轉移到實際的C#對象。我正在尋找直接代碼或者一個很好的教程(我已經廣泛搜索並且無法找到它)。下面是我的xml文件和我的C#代碼的樣本。

<?xml version="1.0" encoding="utf-8" ?> 

<rooms> 
    <room> 
    <entrance> 
     <Description> 
     <Entry1> 
     You walk up and try the door bell. No answer...You force your way into the large, heavy, oak door. In the main foyer, you take 
     a quick look around. You see a baseball bat by the door and a Super Snack on a nearby dresser. Carved into one of the walls are the 
     words "First is 0." There is a door to the west and a door to the north. 
     </Entry1> 
     <Entry2> 
      You are back in the main foyer. Carved into one of the walls are the words "First is 0." There are doors to the west, north, or east. 
     </Entry2> 
     <Entry3> 
      You are back in the main foyer. You see a baseball bat by the door. Carved into one of the walls are the words "First is 0." 
      There are doors to the west, north, and east. 
     </Entry3> 
     <Entry4> 
      You are back in the main foyer. You see a Super Snack on a nearby dresser. Carved into one of the walls are the words "First is 0." There 
      are doors to the west, north, and east. 
     </Entry4> 
     </Description> 
     <Items> 
     <Item name ="Baseball Bat" type ="weapon" attribute="player.attack + 2"></Item> 
     <Item name ="Super Snack" type ="consumable" attribute ="player.health = 100"></Item> 
     </Items> 
     <border> 
     <direction>north</direction> 
     <name>room2</name> 
     </border> 
     <border> 
     <direction>west</direction> 
     <name>room3</name> 
     </border> 
    </entrance> 
    </room> 
    <room> 
    <room2> 
     <Description> 
     <Locked>You walk forward and try the door. Locked...maybe find a key?</Locked> 
     <Unlocked> 
      You use the key you found on the door in front of you. It fits! In the next room you find two doors: one to the west, one to the east. 
      There is a picture on the wall of the professor receiving a watch from a colleague, looks like maybe a work anniversary gift. 
     </Unlocked> 
     </Description> 
     <border> 
     <direction>west</direction> 
     <name>room4</name> 
     </border> 
     <border> 
     <direction>east</direction> 
     <name>room9</name> 
     </border> 
    </room2> 
    </room> 

這是我的Rooms文件C#代碼。沒有太多,因爲我不確定我需要什麼。

using System.Xml; 命名空間FirstTextBasedGame { class Rooms { string description; 字符串寄存器; 公共房間(){ // 不確定哪裏何去何從...... }} }

+0

只是要清楚,項目和房間要應用的問題兩個不同的實體? – Mfusiki

回答

1

XML應該很好地工作。很多遊戲開發人員(僅僅從我自己的經驗中談到)使用XML和序列化來定義和創建這些類型的實體。

你可能有這樣的事情:

<room id="3203jfjb" width="10" height="40"> 
    <items> 
    <item name="table"> 
    </item> 
    <item name="chest" type="container"> 
     <containerItems> 
     <item name="knife"> 

     </item> 
     </containerItems> 
    </item> 
    </items> 
</room> 

有什麼了不起的方法是現在你可以創建一個序列化(能夠)Item類,你可以在兩個房間,並在您的庫存系統中使用。

因此可以輕鬆地將物品從一個物品即PlayerInventory轉移到Room,反之亦然。您還可以將整個房間狀態以及您的清單保存到XML文件中。使保存和加載遊戲狀態的過程不那麼痛苦。

出頭,你可能會感興趣:

http://answers.unity3d.com/questions/443525/serialize-a-gameobject-including-components.html

https://msdn.microsoft.com/en-us/library/bb203924.aspx

+0

這正是我所期待的!謝謝! –

+0

對此還有一個問題。我閱讀了你的鏈接,並且我還閱讀了序列化。我明白這裏的代碼是如何工作的: –

+0

關於此問題還有一個問題。我閱讀了你的鏈接,並且我還閱讀了序列化。我瞭解這裏顯示的代碼是如何工作的:https://msdn.microsoft.com/en-us/library/mt656717.aspx但是,如果您在Room類型的房間中有多個對象,並且您實例化該房間,那麼執行所有這些對象是在同一時間創建的,還是你必須單獨做每一個?我怎樣從一系列房間中撥出特定房間? –