Sign in to follow this  
Darkware

You can't create variables at runtime, so... then what?

Recommended Posts

The way I envisioned my code flow was that I would get to a certain point in the game where you'd load a level or something and at that point would all the variables be created that go with that level. I more simple example would be: loading a mesh when the 'c' key is pressed. So in this case, I would do this: ball myBall; in the code for when 'c' is pressed. But that's creating a variable at runtime and I don't think the compiler likes that. The alternative of course is to create the variable before all the code, but that would make it a global variable, which I've heard is bad. Now I do know that you can create variables inside functions, right? But they end up being exclusive to that function. If I did that with the ball.x mesh that I have, I'd still need a way to access that mesh through the variable or something from within a different function. Only problem is that I have no idea how this is done. Something to do with something called scope I think, but I don't know.

Share this post


Link to post
Share on other sites
If you're using an Object Oriented approach, you wouldn't need global variables. When you handle input, you would access the Mesh Object, which could potentially have a vector of Meshes, which could increase to add a ball.

Share this post


Link to post
Share on other sites
Yeah, I know I don't know C++ well enough yet, but this is just how I've always learned things and it works out in the end. I bought a DirectX book that came with example code on a CD (though it doesn't explain this particular code for some stupid reason, just the other code it has) and I found where it creates the vector object like you said. It goes: std::vector <CGameObject*> objects; I understand everything about that except what's going on with the <> part. As far as I know, to create a variable, you type the class, which would be vector, and then the name of the variable, which would be "objects". What's the <> stuff for? I do know that CGameObject is a class, but I don't see what that has to do with this vector object.

One more quick question: what's the different between

cShip myShip;
and
myShip -> new cShip(); ?

I come from an Actionscript background (like javascript) where the -> operator doesn't exist, so I don't know what it's function is.

Share this post


Link to post
Share on other sites
Quote:
Original post by Darkware
It goes: std::vector <CGameObject*> objects; I understand everything about that except what's going on with the <> part. As far as I know, to create a variable, you type the class, which would be vector, and then the name of the variable, which would be "objects". What's the <> stuff for?

Look in the index of your C++ programming book under "templates". Short answer: The stuff in the <> tells you what kind of a vector it is.

Quote:

One more quick question: what's the different between

cShip myShip;
and
myShip -> new cShip(); ?
One is valid syntax and the other is not. [smile] Your enthusiasm for learning is very clear, and it's a good quality, but I think you're getting a bit ahead of yourself. C++ is a very, very big language, and a well-organized course of instruction will really help you get the answers you're seeking more quickly.

It sounds like you may have a graphics book that assumes more C++ knowledge than you currently have. (That's good. The best ones concentrate on graphics, not programming.) Might I suggest you check out the thought-provoking and free Thinking in C++, by Bruce Eckel? You'll be glad you did. Stick with it through any teeny boring parts you come across: Having a javascript background will make small parts of it old news, but not much.

Share this post


Link to post
Share on other sites
It sounds to me like you have a lot to learn on C++.
The '<' and '>' characters are used, in this context, to set the type for a template class. All you need to know about it for now is that it is the syntax to set the type of objects to be stored in the vector. For more info, you can google for templates in C++.
The new operator is used to dynamically allocate memory for your application. Practically speaking, you can use it to create objects at runtime. The basic syntax is like this:

int * a = null; //declare a pointer to a memory location (null for now)
a = new int(5); //allocate the required memory for an integer and set the pointer a to this location in memory
if (*a == 5) //a holds the memory address. Writing *a returns the integer at this address
{
cout << "Integer at address a equals 5.";
}
delete a; //free up the memory used
a = null; //reset the pointer to null or 0



The -> operator is actually a (*CObject).attribute, but writing CObject->attribute is easier, more clear and therefore more common.

Until you really know what you are doing, stay away from pointers. The C++ STL should cover your needs with the vector and string.

EDIT: I need to type faster, because I think your questions are already answered.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this