Jump to content
  • Advertisement

TigerKnee

Member
  • Content Count

    5
  • Joined

  • Last visited

Community Reputation

129 Neutral

About TigerKnee

  • Rank
    Newbie
  1. So I haven't completely rewritten my code/classes or upgraded yet, but I need some clarification on the nature of Base/Derived classes and accessing stuff that's only in Derived classes, and the GetObjAt function that was provided. Here's what I experimented on (names are... uh, a bit messy, sorry if there's any confusion anywhere):   edit: I changed GetObjAt a little bit, but only the fact that I didn't use a Position struct and just used int x/y values. // For the purpose of this code, there is already a ship at coordinates x10,y10. // ship *s1 = GetObjAt(10,10); // This doesn't compile. Since GetObjAt returns... a pointer to a MapObj, I think? It means I can't just turn it into a derived class ship pointer? mapObj *s1 = GetObjAt(10,10); // This compiles, however, I can only access functions and variables from the base class. //If I wanted to find specific stuff from derived classes e.g turns used for ships, health for planets, I'm not able to access them with this. if (s1 != nullptr) // if I don't do this and s1 is null, it'll crash because the next few lines try to access stuff from nothing. { if (s1->clType == 1) // clType = either ship (1) or planet (2) { ship* pShip = (ship*) s1; // Now I can access stuff in the derived class. cout << "Special used: " << pShip->specialUsed << endl; //But is there any problems with doing stuff like this? } if (s1->clType == 2) // Planet (2) { //Blah blah cast to Planet instead. } } Am I on the right track with this, or is there a cleaner method that I should be using instead?
  2.  I'm using... uh, Visual Studio 2010. My stuff's kind of outdated because I haven't touched programming for a long time (a stint in the military, you see). Are C++11 features available in it?
  3. I'm trying to make a simplistic space-themed strategy game in c++ and I need some help with this since I'm not a great programmer (my skill level is like, dabbling hobbyist?). I got a feeling my structure isn't the most robust which means I'm already in the process of ripping stuff apart and putting them back together.   The "game world" is just a simple 15x15 square tile setup. Outside of empty space, there are really only 2 possible objects that exist: "Planets" and "Ships." They're are set up like this. struct mapObj { int type; int faction; int cX; int cY; }; struct ship : public mapObj { ship(); ship(int ntype, int nfaction, int nX, int nY); int type; int faction; int cX; int cY; int turnUsed; int specialUsed; void destroyShip(); }; struct planet : public mapObj { int type; int faction; int cX; int cY; int currentHealth; }; So I'm not too familiar with trying to use polymorphism but the two derived classes can get rid of repeated stuff like "type" and "faction" since I already derived them from the base class, right?   Anyway, I need to be able to compare variables in two objects together, whether it be Ship-Ship or Ship-Planet, and then I'm going to need to be able maybe destroy and create new ships, and I'm going to need to move them around on the map and check for occupied tiles. Currently the data for individual ships are being stored in a "playerData" class.   How should I go about doing this? Can I use pointers for this? I'm extremely unfamiliar about that concept. As in mapObj* tileContent[15][15]; And this array will point to either nothing, a ship or a planet which I can use to compare to each other?   edit: Darn, should I have posted this in "For Beginners" instead?
  4. Thanks, it does work (as far as I can tell for now, hopefully no other problems crop up later), although yes, there's an extra space at the end of the name. Not sure how to get rid of that (I'm extremely unfamiliar with strings) For information, my txt file looks like this "Name= Dire Wolf Symbol= D Gender= M Item= H HP= 70"
  5. I'm attempting to create a text-based RPG and my first task is to create code that will read in data from a txt file and then store it in the game variables itself so that I can easily just change the txt file itself if I want to change character stats etc. After reading a couple of tutorials I came up with this code typedef struct { string name; char symbol; char gender; char itemType; int hp; } PlayerData; void loader(void) { // initialize PlayerData player; // read string line; ifstream fin("data.txt"); if (fin.is_open()) { while (fin.good()) { getline(fin,line); stringstream ss(line); string fieldname; string svalue; int ivalue; char cvalue; ss >> fieldname; if (fieldname == "Name=") { ss >> svalue; player.name = svalue; cout << "name=" << player.name << "\n"; } if (fieldname == "Symbol=") { ss >> cvalue; player.symbol = cvalue; cout << "symbol=" << player.symbol << "\n"; } if (fieldname == "Gender=") { ss >> cvalue; player.gender = cvalue; cout << "gender=" << player.gender << "\n"; } if (fieldname == "Item=") { ss >> cvalue; player.itemType = cvalue; cout << "Item=" << player.itemType << "\n"; } if (fieldname == "HP=") { ss >> ivalue; player.hp = ivalue; cout << "HP=" << player.hp << "\n"; } } } } So far the code seems to work fine, but I have just one problem. I am unable to have certain values like the Name contain multiple words. For example, in my text file, "Name= Wolf" works as Wolf, "Name= DireWolf" works as DireWolf but "Name=Dire Wolf" just gives me Dire instead. Having no spaces for the names of every single item in the game will definitely look very ugly. Is there a way to fix this problem?
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!