small reference problem

This is a discussion on small reference problem within the C++ Programming forums, part of the General Programming Boards category; bah...been programming in C++ for over 6 years now and i still have the occasional problem with a reference or ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    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

    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?
    Last edited by DavidP; 06-21-2004 at 01:50 PM.
    My Website

    "Circular logic is good because it is."

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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 );

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    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....
    }

    };
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    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.
    My Website

    "Circular logic is good because it is."

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 04:15 PM
  3. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 10:02 AM
  4. Small Problem in C, Please Help.
    By DoctorFu in forum C Programming
    Replies: 6
    Last Post: 10-31-2005, 03:06 PM
  5. compiler problem
    By sofarsoclose in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21