2011-12-23 99 views
0

我想找到一種方法來將stat數據導入到正在進行的遊戲中通過傳播板?這就是我正在使用的:如何從外部源獲取信息到遊戲中

現在,例如..法術,爲了命名它們,設置統計數字,並且能夠通過數字來調用它們我有這樣的事情在實際的代碼中進行:

void spell(int & eMoney, int eSpell[10]) 
{ 
    using namespace std; 
    char spellname[10][25] = {"Minor Heal", "Fire Shard", "Lightening Shard", "Ice Shard", "Magic Barrier", "Essence Of Life", 
"Earth Shard", "Wind Shard", "Insigma", "Weaken"}; 
    int spellcost[10] = {50, 80, 80, 80, 100, 100, 80, 80, 120, 80}; 

這是所有罰款和花花公子,它的作品...但它現在是一個問題,後來..我希望能夠用一個電子表格,就像一個CSV文件,這樣我就可以有一個流傳表格就像剛纔的法術,只是劍,只是俱樂部...我打算有一個非常大的選擇,它是更加理想的能夠編輯一個單一的文件成列和行,並讓實際的遊戲從外部文件中獲取信息何時需要......但我無法弄清楚如何去做這件事?我願意接受任何想法..

這裏是我現在該怎麼呼籲咒語的信息的例子:

case 2: 
      do 
      { 
       cout << "Which spell would you like to cast?\n\n"; 
       for(x=0;x<10;x++) 
        cout << x+1 << ". " << spellname[x] << ": " << eSpell[x] << " left" << endl; 
       cout << "11. Leave\n\n>> "; 
       cin >> decision; 
       system("cls"); 
      } 
      while((decision<1)&&(decision>11)||(eSpell[decision-1]==0)); 
      switch(decision) 

,這裏是試算表我心裏有基本的例子嗎?起始於A1:

又如:

目前代碼:

char monsters[16][25] = {"Wolf", "Bear", "Bandit", "Traveler", "Gargoyle", "Knight", "Warlock", "Mammoth", "Cyclops", "Unicorn", "Dragon", "Your Mother", "Demon", "Jesus", "Satan", "God"}; 
    //monster strengths 
    int monsterdamagemax[16] = {32, 42, 53, 53, 65, 65, 75, 75, 85, 85, 90, 90, 95, 95, 110, 110}; 
    int monsterdamagemin[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    int monsterdefensemax[16] = {2, 7, 13, 13, 20, 20, 25, 25, 35, 35, 40, 40, 45, 45, 55, 55}; 
    int monsterdefensemin[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    int monsterhealth[16] = {32, 52, 73, 73, 95, 95, 118, 118, 142, 142, 167, 167, 193, 193, 220, 220}; 
    int monsterspeed[16] = {7, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}; 
    int monstergold[16] = {20, 30, 41, 41, 53, 53, 66, 66, 80, 80, 95, 95, 110, 110, 125, 125}; 

理想情況下,我希望能夠得到所有從一個CSV文件,如:

mID mName mDmgMax mDmgMin mDefMax mDefMin mHp mSpeed mGold 
1 Wolf 32 0 2 0 32 7 20 
2 Bear 42 0 7 0 52 8 30 
3 Bandit 53 0 13 0 73 9 41 
4 Traveler 53 0 13 0 73 9 41 
5 Gargoyle 65 0 20 0 95 10 53 
6 Knight 65 0 20 0 95 10 53 
7 Warlock 75 0 25 0 118 11 66 
8 Mammoth 75 0 25 0 118 11 66 
9 Cyclops 85 0 35 0 142 12 80 
10 Unicorn 85 0 35 0 142 12 80 
11 Dragon 90 0 40 0 167 13 95 
12 Your Mother 90 0 40 0 167 13 95 
13 Demon 95 0 45 0 193 14 110 
14 Jesus 95 0 45 0 193 14 110 
15 Statan 110 0 55 0 220 15 125 
16 God 110 0 55 0 220 15 125 
+0

如何使用需要的信息製作文件並從中讀取信息? – lapk 2011-12-23 04:40:01

+0

@AzzA這就是想法,但我無法從實際的遊戲中正確讀取文件中的信息= \ – Leaum 2011-12-23 04:41:34

+0

請查看http://www.cplusplus.com/reference/iostream/fstream上的C++文件流/, 例如。他們有大部分功能的小例子可以幫助您入門。基本上,如果您從未嘗試從文件讀取/寫入,則需要練習如何讀取格式化數據以及如何從中提取信息。這並不難,你只需要嘗試一下,看看它是如何工作的,哪些功能是有用的。 – lapk 2011-12-23 04:47:41

回答

0

如果您將要保留大量基於表格的數據,則可以使用SQLite進行調查。它有一些有趣的成本和收益。

在下面(也許),它是SQL。它可能會更復雜一些,取決於您的搜索算法,可能會更慢。它也不能手動編輯,你需要一些東西來打開數據庫(有免費的工具可用)。

在了一邊,你就會得到一個數據庫(任何你需要像spell='fireball' AND damage < 5)的所有排序和過濾能力,和SQLite是快速(很輕鬆地存儲在遊戲的數據,很可能更快比你自己的代碼)。您可以將所有數據存儲在單個文件中,以便輕鬆進行部署或修改,併爲每種類型(武器,法術,角色等)提供獨特的表格,並且不涉及涉及的服務器(SQLite是單個DLL)。

關係數據庫擅長處理一致形式的數據表,這正是遊戲環境中的每個對象類型(每個對象類型有幾個字段,沒有太多變化,可能有一些空白,以及各種數據類型)。儘管SQLite是最小的數據庫,但它可以用極好的時間處理成千上萬行,所以你不必擔心你的遊戲數據變得很笨重(這種情況很快發生在純文本表文件中,比如NWN(2)的2DA格式)。 (有一個新的對象類型是一個新的表格和查詢,而不是很多的代碼)和一個非常穩定的數據格式和加載/保存庫。根據您的需求,這可能值得一試。

0

如指出問題的評論,你應該去<fstream>如果你真的想處理CSV文件。使用這種方法,getline應該足夠滿足您的需求。

This thread in C++.com and this question應該指出你如何處理CSV的一些方向。

0

我使用Boost解析我使用的CSV文件。這是一個簡單的例子。

雖然我同意peachykeen,SQLite可能更適合你,但也許這會幫助你入門。

#include <iostream> 
#include <fstream> 
#include <vector> 

#include <boost/tokenizer.hpp> 
#include <boost/token_functions.hpp> 

typedef std::vector<std::string> csvLine; 
typedef std::vector<csvLine> csvLines; 
typedef boost::tokenizer<boost::escaped_list_separator<char> > csvTokenizer; 

csvLines ReadCSVFile(const std::string& fileName) 
{ 
    csvLines retVec; 
    std::ifstream inFile(fileName.c_str()); 
    if(inFile) 
    { 
     std::string fileLine; 
     while(std::getline(inFile, fileLine)) 
     { 
      csvTokenizer lineTokens(fileLine); 
      retVec.push_back(csvLine(lineTokens.begin(), lineTokens.end())); 
     } 
     inFile.close(); 
    } 
    return retVec; 
} 

int main(int argc, char** argv) 
{ 
    csvLines lines(ReadCSVFile(argv[1])); 
    for(csvLines::iterator lineIt = lines.begin(); lineIt != lines.end(); ++lineIt) 
    { 
     for(csvLine::iterator tokenIt = (*lineIt).begin(); tokenIt != (*lineIt).end(); ++tokenIt) 
     { 
      std::cout << *tokenIt << " "; 
     } 
     std::cout << std::endl; 
    } 
    return 0; 
} 
1

如何寫,爲您創建的記錄,並在你的「主」程序,它是遊戲中一個小的命令基於應用程序,你只需要讀取這些記錄。

樣品結構 -

struct monster 
{ 
    int mID; 
    char mName[25]; //from your code 
    int mDmgMax; 
    //and these as well mDmgMin mDefMax mDefMin mHp mSpeed mGold 
}; 
在這種「輔助者」節目在記錄中逐個讀取每個數據項(如MNAME),並且在這個結構中插入

。將結構寫入monsters.dat文件

std::ofstream fout; 
fout.open("monsters.dat", std::ios::app | std::ios::binary);  
fout.write((char*) &monsterInstance, sizeof(monsterInstance)); 
fout.close(); 

這將簡單地附加記錄。 (我跳過錯誤檢查和讀取數據。) 爲了更加方便,該程序應該能夠顯示當前的怪物,添加怪物,刪除怪物(通過輸入mID)。

在主程序中讀取這些記錄應該是一件容易的事。