SaxonRah
(SaxonRah)
May 27, 2014, 4:16am
1
There is a better way! Don’t listen to me. Go to Iniside’s post !
As requested
You can do something like this to read any file separated by a delimiter, but here we are reading csv files letter by letter separated by a delimiter “,”; line by line in c++
I like to typedef stuff, if im going to use it allot/complicated bracketing.
Init:
/*--------------------INIT--------------------*/
typedef struct{
int lineCount;
int letCount;
}infoCSV;
typedef vector<vector<string> > csvVector;
infoCSV readCSV(istream &input, vector< vector<string> > &output);
/*--------------------INIT--------------------*/
Code:
/*--------------------CODE--------------------*/
infoCSV readCSV(istream &input, vector< vector<string> > &output) {
infoCSV newInfo;
newInfo.lineCount = 0;
newInfo.letCount = 0;
string csvLine;
while (getline(input, csvLine)) {
istringstream csvStream(csvLine);
vector<string> csvColumn;
string csvElement;
while (getline(csvStream, csvElement, DELIMITER)) {
csvColumn.push_back(csvElement);
newInfo.letCount++;
}
output.push_back(csvColumn);
newInfo.lineCount++;
}
return newInfo;
}
/*--------------------CODE--------------------*/
Usage:
/*--------------------USAGE--------------------*/
string fileName = "filename.csv";
fstream file(fileName, ios::in);
if (!file.is_open()) {
exit(1);
}
csvVector csvData;
infoCSV currentCSVInfo;
currentCSVInfo = readCSV(file, csvData);
int cX = 0, cY = 0;
// print out read data to prove reading is working
for (csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i) {
for (vector<string>::iterator j = i->begin(); j != i->end(); ++j) {
vector<string>::iterator iY = find(i->begin(), i->end(), "
");
if (iY != i->end()){
cY = distance(i->begin(), iY);
}
else {
cY = distance(i->begin(), iY);
}
// Check for Values of slot
if (*j == something1) {
doSomething1();
}
if (*j == something2){
doSomething2();
}
//Sample Code
//CaptureY += offset;
//if (CaptureY >= offset * cY){ CaptureY = 0; }
}
//Sample Code
//CaptureX += offset;
}
/*--------------------USAGE--------------------*/
You could use this for an inventory system for when a player quits the game. I use this to read random levels generated and saved into CSV files real-time;
Or whatever. Enjoy.
~Rah
iniside
(iniside)
May 27, 2014, 5:51am
2
After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...
Reading time: 1 mins 🕑
Likes: 8 ❤
https://docs.unrealengine.com/latest/INT/Programming/Gameplay/DataDriven/index.html
There is already buld in support for csv files and data reading.
Though nice effort, you didn’t need to reinvent a wheel ;).
SaxonRah
(SaxonRah)
May 27, 2014, 2:47pm
3
I was requested to make a tutorial about the CSV options from my random level gen, I guess it would have been really smart to SEARCH it first, hehe.
Thank you for bringing this to my attention ! It’s so much simpler !