🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Project 2

Started by
94 comments, last by Warlord_Shaun 16 years, 3 months ago
Quote: I was thinking about using a more readible format, either XML or some pseudo class declarations, and build some kind of parser to instantiate objects for this. Or maybe instantiate my objects with DOM nodes which contain their starting values.


I am very much in favor of XML as a language to use for your data files. Its organized, easy to read, and is used in many standard and commercial engines for things such as Web Applications all the way to World of Warcraft Interface files.

How you parse/load the XML is up to you, but I can assure you, design such as this for the rooms, objects, and perhaps even NPC's will make a huge difference in your ability to create interesting rooms and objects. The easier it is to create the world, the more likely you are to do so. As well, the less times it takes a "world designer" to create your world, the more time they'd have to think up unique features.

Cheers!

Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
Quote: Original post by jwalsh
Quote: Original post by Ceoddyn
I have a question relating to the workshop as a whole - what will happen after we complete the book?


Upon completion of the textbook we'll have 1 more final project. At the end of the project the workshop will be 'completed.'

As for what GameDev plans to do with the Workshop after that, I have no idea. My advice would be to rename the threads based on the chapter contents, ie "Arrays", "Polymorphism", etc....and then just rename the forum from CPP Workshop to "CPP Language" or somesuch. We've got the beginnings of a strong question/answer base here, and I still see quite a few questions on FB about C++. So I think a forum dedicated to the C++ Language, given the language's popularity for game programming, isnt a bad idea.

Cheers!


BTW jwalsh, dont forget this:

Quote: The Future:

I can definitely see doing continuing workshops on more advanced topics and other programming languages if this experiment proves successful. Some other obvious topics include:

Data Structures

The Standard Template Library

DirectX

OpenGL

Mathematics for Computer Graphics (Linear Algebra)

Video game Physics

Windows Programming (Win32 API or .NET)

Shader programming (Pixel/vertex)

Skeletal Animation and Modeling

Procedural Texturing and materials

Network programming

Programming languages and translators

C++/CLR and the .NET Framework Library

And of course a language class can be done for any common language for video games - Java, C#, Python, Lua, ...

Cheers!


Quote: Original post by Zahlman
That's not a reason, and to be honest you're making less and less sense. Please, I'm trying to make this easier for you. Why resist that?


Please dont take me the wrong way. Im not resisting you in anyway, infact im incredibly gracious for your help. You have to be patient with me as this is my first real programing language and ive only been at this since a few weeks before project 1 was assigned. I know what I want to do, but unfortunately I dont know how to convey it.

If you look at how I did the menu system in my first project, you might get a sense at how im approaching this. clickie

The idea is for my menu system to dynamically build itself based on the users location in the world. Each location is its own object stored in a multi array. There will be menu object pointers belonging to each object that will be used to create a custom menu based on where the user is. This idea will also be ported over to the item/container system.

That being said, im still working on concepts before I can start coding for project 2. The largest concept being vectors and reading/writing from/to a file.

After your posts I went back and cleaned up the code and came close to what I want...

#include <fstream>#include <string>#include <vector>#include "object.h"#include <iostream>using namespace std;int main(){    vector <Object*> Objv;	ifstream fin("test.csv"); // open the file	std::string objtext;	int objid;     	  	  	  while (fin>>ws && getline(fin,objtext,',') && fin>>objid)	  {	    Object *newobj=new Object(objtext,objid);	    Objv.push_back(newobj);	 	 	  } // end end of line while	  fin.close();	std::string word;	int id;	int size=Objv.size();	for (int i=0; i<size; i++)	{	word=Objv->Getchar();	id=Objv->Getid();	cout << "Its word: " << word << " and its id: " << id << endl;	}	for(int i=0; i<size; i++)	{	 delete Objv;	}    char ch;	cin >> ch; //just a stop break before exiting console		return 0;}


This works and compiles fine... but I want a pointer to the Objv vector. I tried this below, but the word=Objv->Getchar() and Getid() break and want a "." instead.

vector <Object*> *Objv;Objv=new vector <Object*>;


Also, cin and cout will not work unless i #include <iostream>... but I thought iostream was included in fstream. What did I miss there? Even dropped the using namespace and tried std::cin..etc
Quote: Original post by jwalsh
Quote: I was thinking about using a more readible format, either XML or some pseudo class declarations, and build some kind of parser to instantiate objects for this. Or maybe instantiate my objects with DOM nodes which contain their starting values.


I am very much in favor of XML as a language to use for your data files. Its organized, easy to read, and is used in many standard and commercial engines for things such as Web Applications all the way to World of Warcraft Interface files.

How you parse/load the XML is up to you, but I can assure you, design such as this for the rooms, objects, and perhaps even NPC's will make a huge difference in your ability to create interesting rooms and objects. The easier it is to create the world, the more likely you are to do so. As well, the less times it takes a "world designer" to create your world, the more time they'd have to think up unique features.

Cheers!


Which are the most common used XML parsers? I'm guessing there must be a lot of open source XML parsers around, so I don't have to invent the wheel again.

Although writing an XML parser can be fun...

TinyXML is a fairly common choice.
Quote: Original post by westond
I know what I want to do, but unfortunately I dont know how to convey it.


That much is normal. [smile]

Quote: This works and compiles fine... but I want a pointer to the Objv vector.


See, the resistance that I perceive is in terms of "wanting" something that I can tell you (from my years of experience) is more complex and doesn't accomplish anything extra. :
Quote: I tried this below, but the word=Objv[i]->Getchar() and Getid() break and want a "." instead.

*** Source Snippet Removed ***


Currently, you index into the vector, yielding an Object*; then you can use -> to dereference the Object* to get an Object, and select its Getchar() member function.

If you have a pointer to the vector, then 'Objv[i]' is no longer indexing into the vector, but instead operating upon the pointer - i.e. assuming you have an array of some kind (in particular, an array of vectors). That selects a vector, so you can't use -gt;Getchar() at that point - first off because that tries to do a dereference (and you don't have a pointer), and secondly because the *vector* doesn't have a Getchar(). Although the compiler is generally very smart about the code that it writes when it gets a correct program, it's usually very dumb about how it reports errors - it basically reads one "token" at a time and reports the error as soon as it knows there is one, using only the information it has so far (even if the very next token could give it a much better understanding of what you're really trying to do). That's why it tells you it wants a '.': the only thing it sees for the time being is a -> applied to a non-pointer.

What you would need to do is dereference the pointer first (to get the vector) and *then* index in. That looks like (*Objv)[i]->Getchar().

... You see? More complicated, and it won't work any better: the whole point of using the vector in the first place is to let *it* manage the dynamic allocation.

Quote: Also, cin and cout will not work unless i #include <iostream>... but I thought iostream was included in fstream. What did I miss there? Even dropped the using namespace and tried std::cin..etc


It is not (necessarily - it could on someone else's implementation, because the person putting that implementation together found it made his/her life easier to do so, but you should not rely on that), because cin and cout aren't file streams - they're console streams. :)
Having finished the first project I am tempted to have a go at the second project however this seems to be a much more tricky project than I imagined.

I have to say reading through some of the replies concerning how to handle inventory items was quite interesting but I am not sure I can comprehend some of the more difficult concepts suggested like visitor patterns and using opaque pointers. Are these concepts covered in the book being used(I don't have the book in question). The way I thought about the inventory was to do something like adding:

vector<Weapon> wInventory;
vector<Armor> aInventory;
vector<Items> iInventory;

in the character class. I'm not sure if I should try attempting some of the ideas suggested or just use the above. It does seem crude in comparison to the various ideas suggested but much more comprehendable to me.

Anyway how are other people finding the project?
Being honest I'm finding it difficult just to start.
How about them apples?
DOUBLE POST FTW!!!!

Sorry about that ev1.

jwalsh has proly said it best, dont let what you dont know keep you from attempting these projects. Just do it the best way you know how... even if it isnt very elegant or proper coding. We are all learning here, and noone expects us to be on par with a coder that has a degree in CS and several years of experiance. We are bound to do it wrong despite our best efforts. The point of the projects is to make us think about a goal within the context of what we know. The more we use what we know, then the easier it is to pick up on those new and more difficult aspects of C++. Dont let anything hold you back... just go for it. And if anyone here says otherwise, they ought not be here.

Quote: Original post by popcorn
Having finished the first project I am tempted to have a go at the second project however this seems to be a much more tricky project than I imagined.

I have to say reading through some of the replies concerning how to handle inventory items was quite interesting but I am not sure I can comprehend some of the more difficult concepts suggested like visitor patterns and using opaque pointers. Are these concepts covered in the book being used(I don't have the book in question). The way I thought about the inventory was to do something like adding:

vector<Weapon> wInventory;
vector<Armor> aInventory;
vector<Items> iInventory;

in the character class. I'm not sure if I should try attempting some of the ideas suggested or just use the above. It does seem crude in comparison to the various ideas suggested but much more comprehendable to me.

Anyway how are other people finding the project?
Being honest I'm finding it difficult just to start.


As far as starting the project. I would suggest putting pen to paper first. I designed the world on paper as far as each cell the player could interact with. This then showed me what direction the character could move when in anyone cell. This also lead me to create multiple levels/zones to the world in which the character could move. It also inspired me to come up with a structrue of how character, item, and the world would interact. By the time I was finished drawing out my world I had a pretty good idea of to attack the code. Ill scan my drawings here so you can see what I did.

Here is the link to a pdf. My paper was much larger than the scanner bed, but you get enough to get the idea clickie
Quote: Original post by westond

jwalsh has proly said it best, dont let what you dont know keep you from attempting these projects. Just do it the best way you know how... even if it isnt very elegant or proper coding. We are all learning here, and noone expects us to be on par with a coder that has a degree in CS and several years of experiance. We are bound to do it wrong despite our best efforts. The point of the projects is to make us think about a goal within the context of what we know. The more we use what we know, then the easier it is to pick up on those new and more difficult aspects of C++. Dont let anything hold you back... just go for it. And if anyone here says otherwise, they ought not be here.


I realise that westond, I will try to continue regardless even if my code does look messy but I also want to try out some of the techniques suggested by some people - it is a project designed to help you learn more after all. I feel quite interested by the idea of handle classes and I would like to try to implement this for the items class.

I'm not quite sure I understand this yet though so I may resort to something simpler but I would like to ask if you create an item class and derive a weapon and armor class from it - how would you create the handle class for this - does the interface for the handle class have to provide all the member functions that the item class as well as the derived classes have?

How about them apples?

This topic is closed to new replies.

Advertisement