-
small reference problem
bah...been programming in C++ for over 6 years now and i still have the occasional problem with a reference or two :D
Anyways...im probably just too frustrated to see the problem right now. Let's see if any of you guys can spot it.
This is simple snippet of code that spawns the a barracks building on the map at coordinate (x,y) in my RTS game. For some reason the code that flags the terrain as impassable is not working. (The code in the nested for loop at the bottom of the code snippet).
It was working before I put it in the function (I had it sitting in the main function for testing, then I encapsulated it in this function), and now it doesnt seem to work. Therefore I presume it is a problem with the reference, however, I don't know why there would be a problem, because reference variables are supposed to hold their changes. They are not copies made just for use inside that function. Reference variables are originals and therefore the map should hold any changed made to it, yet for some reason it is not holding these changes that I am making to tile passability.
Here is the code snippet:
Code:
bool Spawn ( int x, int y, MAP_DTP &map )
{
LoadAnimation ( "animation\\barracks.anim" );
SetEntityName("Barracks");
int TwX = ConvertGridToWorldX ( x );
int TwY = ConvertGridToWorldY ( y );
SetWorldCoordinates ( TwX, TwY, 0 );
ConvertWorldToScreenCoord(map);
int gXf = ConvertWorldToGridX ( WorldX + widthPerFrame );
int gYf = ConvertWorldToGridY ( WorldY + heightPerFrame );
//This nested for loop is the problem.
//I set the passable variable to false in each specified spot on the map.
//The map should hold the changes because it was passed by reference.
//However the changes do not have any effect after the function exits.
for ( ; x < gXf; x++ )
{
for ( ; y < gYf; y++ )
{
map.terrainMatrix[y][x].passable = false;
}
}
return true;
}
Anyone see anything I am doing wrong?
-
>Anyone see anything I am doing wrong?
You're right about the reference part, so it has to be something else, like the loops are never entered. Perhaps something to do with gXf and gYf.
> int gXf = ConvertWorldToGridX ( WorldX + widthPerFrame );
> int gYf = ConvertWorldToGridY ( WorldY + heightPerFrame );
I don't see where WorldX and WorldY are declared. I assume they're global or within a class, along with widthPerFrame and heightPerFrame. Just guessing, but maybe it should be:
int gXf = ConvertWorldToGridX ( TwX + widthPerFrame );
int gYf = ConvertWorldToGridY ( TwY + heightPerFrame );
-
the vars are a global part of the class that this function is part of. they are correct as well.
the function is defined inside the class, that is why there is no class scope identifier. in other words, the class looks somewhat like this:
class BARRACKS_ENTITY : public INANIMATE_ENTITY
{
protected:
....code...
public:
...code...
bool Spawn ( ...la la la.... ) {
...the spawn function....
}
};
-
Ok that makes sense.
> ConvertWorldToScreenCoord(map);
This is probably getting away from the problem, but did you make map a reference here? Of does this only read the map?
This is stating the obvious, but it has to be the value of one of four variables: x, y, gXf, and gYf.
-
blah...got it to work...there were a few problems (and still are a few problems, but i am working them out)...one of them was i was forgetting to reset the counter on the inner for loop.
-
>forgetting to reset the counter on the inner for loop
Oh wow, I guess that would have been obvious to a more observant eye. Sorry I missed that.
-
I don't know if your conversion functions/methods do bounds checking but you may do well to add in some checking on the x and y parameters to avoid potential problems.