I am having some issues involving using C-style arrays that are of a custom class type in C++ for my homebrew DS game. I was originally using the std:vector, but felt that it was too inefficient for a system like the DS, especially with the low ram and memory that it has. I refactored the code to allow for arrays of full of 10 falling objects. The falling objects are used for the player to collide with. I, however, am having some issues with it. If I start the game, and try to proceed as normal, it ends up giving me this:
This is not supposed to happen, as the array is only supposed to hold 10 values, and the sprite id goes incrementally from 23 - 32. What I think is happening is that since C++ does not check out of bounds, it is creating random values for array elements that are out of bounds. If I do a simple print to print the sprite ID of the element out of bounds, it will print either 0, or a random number.
Here is the code for creating the array:
I've tried many different solutions, and different types of arrays. Some have worked, but not to the fullest extent that could be inline with what I want. I have also linked the main.cpp as well.
If anyone has any tips or suggestions, I would appreciate that.
This is not supposed to happen, as the array is only supposed to hold 10 values, and the sprite id goes incrementally from 23 - 32. What I think is happening is that since C++ does not check out of bounds, it is creating random values for array elements that are out of bounds. If I do a simple print to print the sprite ID of the element out of bounds, it will print either 0, or a random number.
Here is the code for creating the array:
C++:
void createFallObjectArray(FallingObject arr[], s16 pos[], int& spawnIndex) {
for (int i = 0; i < fallCount; i++) {
s16 randomNum = rand() % 3 + 0;
// Set randomObj to a random number between 1 and 3.
int randomObj = rand() % 3 + 1;
arr[i].x_pos = pos[randomNum];
arr[i].y_pos = -16;
arr[i].spriteID = 23 + i;
arr[i].rotID = i + 1;
arr[i].objectType = randomObj;
}
}
C++:
void handleFallObjects(float & spawnTime, float & dt, float & spawnDelay, FallingObject fallArray[], int& fallUsedIndex, u8& fallRotID, int& spawnIndex, s16 pos[])
{
if (fallUsedIndex < fallCount)
// Increase spawnTimer by dt.
spawnTime += dt;
// If the spawn timer is greater than or equal to spawnDelay.
if (spawnTime >= spawnDelay)
{
NF_CreateSprite(1,fallArray[spawnIndex].spriteID,fallArray[spawnIndex].objectType,fallArray[spawnIndex].objectType,fallArray[spawnIndex].x_pos,fallArray[spawnIndex].y_pos);
NF_EnableSpriteRotScale(1,fallArray[spawnIndex].spriteID,fallArray[spawnIndex].rotID,false);
NF_SpriteLayer(1,fallArray[spawnIndex].spriteID,2);
spawnIndex++;
//fallRotID++;
spawnTime = 0;
}
}
I've tried many different solutions, and different types of arrays. Some have worked, but not to the fullest extent that could be inline with what I want. I have also linked the main.cpp as well.
If anyone has any tips or suggestions, I would appreciate that.