
Deleting and Redefining Variables
For the past 2 days I have been making a small 2D space invaders type game, very inefficient but what do you expect in 2 days
My problem is this, I have around 70 variables (from things like moving the ship, moving the enemy, bullets etc) and when you fail and it says GameOver I want it to restart but I need to reset all my variables. Is there any way to delete a variable so I can then define it again
ie:
Delete Variable[5];
int Variable[5];
The reason I want to do this is because its easier to define an array and everything is automatically set to zero rather than having to set up a loop to set each array element back to zero.
Thanks

September 18, 2001 01:24 PM
memset() can be used to do this, search the web to find out how!
quote:
Original post by Anonymous Poster
memset() can be used to do this, search the web to find out how!
Come on! You could have given him a perfunctory explanation at least!
memset() can be used to set the contents of a segment of memory, such as an array, to a specific value:
memset( array, value, size_of_array );
As for actually deleting and redeclaring arrays, it can't be done with static arrays (e.g. int array[5]; ), but it can with dynamic arrays
|
This is a fairly simplistic discussion. Look in your development environment's help files or on the web for more information. Hope this has helped.
EDIT: Damn smileys!
Edited by - Oluseyi on September 18, 2001 2:31:18 PM
September 18, 2001 05:00 PM
In C/C++, you can use malloc and realloc
array=malloc(size); // allocates memory for "array", with size of "size"
realloc(array, size) // reallocates memory for "array", with new size of "size"
array=malloc(size); // allocates memory for "array", with size of "size"
realloc(array, size) // reallocates memory for "array", with new size of "size"
Hi
Just a suggestion.
Have a function to initialise your variables. When ''game over'' occurs have a function to flatten them again - then recycle back to your function for initialising variables etc.
eg.
//declare variable
int *nVariable;
int nNumberOfVariables;
//then initialise them in suitable function
nVariable=0; /*setting pointer to ''NULL'' makes it easy to test*/
nNumberOfVariables=5; /* or however many you need*/
//ensure memory not already allocated for this
if (!nVariable)
{
/*dynamically allocate memory for 5 (ie ''nNumberOfVariables'') integers*/
nVariable=new int[nNumberOfVariables];
/*you can use your favourite function to ''zero'' the array; memset is good, ZeroMemory is ok in windows*/
ZeroMemory(nVariable,sizeof(int)*nNumberOfVariables);
}
//
//then just put in whatever values you want
//
//Once you''re finished, call your ''variable kill function'':
/*make sure nVariable is valid as trying to delete it when it''s not is Very Bad*/
if (nVariable)
{
delete[] nVariable; /* note delete[] signifies array to be deleted*/
nVariable=0;
//
Then just call ''initialisation'' function again to reset everything.
Most of this is a lot easier with c++: if you just have a class for the game itself then when that class goes out of scope or is deleted then restarting the game is simply a matter of instantiating another class of that type.
Hope that is of some use to you.
Just a suggestion.
Have a function to initialise your variables. When ''game over'' occurs have a function to flatten them again - then recycle back to your function for initialising variables etc.
eg.
//declare variable
int *nVariable;
int nNumberOfVariables;
//then initialise them in suitable function
nVariable=0; /*setting pointer to ''NULL'' makes it easy to test*/
nNumberOfVariables=5; /* or however many you need*/
//ensure memory not already allocated for this
if (!nVariable)
{
/*dynamically allocate memory for 5 (ie ''nNumberOfVariables'') integers*/
nVariable=new int[nNumberOfVariables];
/*you can use your favourite function to ''zero'' the array; memset is good, ZeroMemory is ok in windows*/
ZeroMemory(nVariable,sizeof(int)*nNumberOfVariables);
}
//
//then just put in whatever values you want
//
//Once you''re finished, call your ''variable kill function'':
/*make sure nVariable is valid as trying to delete it when it''s not is Very Bad*/
if (nVariable)
{
delete[] nVariable; /* note delete[] signifies array to be deleted*/
nVariable=0;
//
Then just call ''initialisation'' function again to reset everything.
Most of this is a lot easier with c++: if you just have a class for the game itself then when that class goes out of scope or is deleted then restarting the game is simply a matter of instantiating another class of that type.
Hope that is of some use to you.
Country: Scotland [not listed in the ''Country'' combobox].
I use malloc/free to make an array of pointers so that I can use realloc later and resize an array if I need to. then I can fill up the array with pointers to object that I can get from new. (malloc doesn''t clear the memory, though)
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement