Jump to content

  • Log In with Google      Sign In   
  • Create Account


Trouble accessing class Variable


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Captacha   Members   -  Reputation: 141

Like
0Likes
Like

Posted 08 January 2012 - 05:45 PM

I've been working on some 2D Movement in my games for the last few weeks and i'm using the SDL game libary for this game. I have a Player class with a SDL_Rect embedded in it for tracking x,y coordinates. The problem is that my Player will not move because my Input() method does not change the X Value. After doing some debugging, I figured out that my Input()method works and my Draw()method also works, but the Input()method does not change the values at all.

Input():

void Input(SDL_Rect Rect)
{
SDL_Event Event;
while(SDL_PollEvent(&Event))
{

  if(Event.type == SDL_KEYDOWN)
  {
   if(Key == SDLK_LEFT)
   {
	Rect.x--;
  
   }
  
   if(Key == SDLK_RIGHT)
   {
	Rect.x++;
   }
  }
  if(Event.type == SDL_QUIT)
  {
   Exit();
  }
}
}


Sponsor:

#2 rip-off   Moderators   -  Reputation: 7964

Like
0Likes
Like

Posted 09 January 2012 - 04:30 AM

C++ and C use "pass by value" by default. That is, when you call Input(), the rectangle in the calling function is copied into the "Rect" parameter of the function. Your function makes modifications to the local rectangle, but they are lost when this function returns.

One approach is to pass the rectangle by reference. In C++, you would write:
void Input(SDL_Rect &Rect)
{
   // ...
}
The ampersand by the parameter asks the compiler to not make a copy, but to instead allow you to manipulate the caller's value. You don't have to change the code in the function, that remains the same. You do have to update any function declarations you have written for this function though to match.

C does not allow pass by reference, but you can simulate it by passing a pointer to a value. You must remember that this pointer itself is passed by value though. You can also do this in C++, but see below for why it might not be a good idea.

It would look like this:

void Input(SDL_Rect *Rect)
{
    SDL_Event Event;
    while(SDL_PollEvent(&Event))
    {
        if(Event.type == SDL_KEYDOWN)
        {
            if(Key == SDLK_LEFT)
            {
                Rect->x--;
            }

            if(Key == SDLK_RIGHT)
            {
                Rect->x++;
            }
        }

        if(Event.type == SDL_QUIT)
        {
            Exit();
        }
    }
}
Note the arrow syntax required to dereference the pointer to access the member variable.

However, the C function does introduce an interesting new problem, what happens if the caller accidentally or intentionally passes a NULL pointer to the function? A simple solution is to document that the function does not allow NULL parameters. A better solution is to do this, but also use the assert() macro to validate that the parameter is not null in Debug mode. Finally, we could make the function "NULL safe", by not attempting to write to the Rect pointer if it is NULL.

For these reasons, I would prefer the C++ solution if you have a choice. It requires a not-null value by contract, so you don't have to worry yourself about working with the case where the pointer might be null.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS