Jump to content
  • Advertisement
Sign in to follow this  
Jay Pascua

Returning a true in a function that has a bool return type

This topic is 1254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm honestly confused as to why this doesn't work any help would be greatly appreciated:

 

the call in main.cpp:

 

				if ((*_bagOfTetrominos.front()).checkCollision()) {
					// Do Nothing
					std::cout << "Did Nothing" << std::endl;
				}
				else {
					std::cout << "Did Something" << std::endl;
					(*_bagOfTetrominos.front()).setX((*_bagOfTetrominos.front()).getX() + BLOCK_SIZE);
				}

Had the cout's just for debugging purposes:

 

and the checkCollision function :

bool T_Block::checkCollision()
{
	for (int i = 0; i < _blockMapHeight; i++) {
		for (int j = 0; j < _blockMapWidth; j++) {
			if (blockMap[j][i] != 0) {

				// Draw Individual Blocks
				SDL_Rect checkPosition;

				// Check Below Each Brick
				checkPosition.x = _x + (i * BLOCK_SIZE);
				checkPosition.y = _y + (j * BLOCK_SIZE);

				Vector2D convertedCoords = convertCoords(checkPosition.x, checkPosition.y);

				if (_gameBoard->_board[convertedCoords.getX()][convertedCoords.getY()] != 0) {
					std::cout << "Collision Detected!" << std::endl;
					return true;
				}
			}
		}
	}
	return false;
}

When I watch the debugger it goes into the if statement showing that gameBoard coods != 0;

It even prints out "Collision Detected!" in the console but then it returns false? and prints out "Did Something".....

but then continues merrily down to return false.

 

Edited by Delite413

Share this post


Link to post
Share on other sites
Advertisement

Sorry to the people I was talking to in the chatroom, it crashed and now it says I'm unable to connect to the chatroom please contact an administrator.

Share this post


Link to post
Share on other sites

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness. 

This is usually the first thing you should do when you encounter something that doesn't make sense. The other thing you can do is look at the assembly code that is being executed and see if it doesn't jump to the wrong location.

Share this post


Link to post
Share on other sites
Debugging per line "step into" should take you through the code per line. So you can see if the values are as expected and why true or false is returned. You could start the debugging by a break point in the main code, where you call the collision function.

Share this post


Link to post
Share on other sites

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness. 

 

This happens a lot; especially under Windows with Visual Studio.  Do a clean and rebuild.

Share this post


Link to post
Share on other sites
It's doing exactly what you told it to do.
 
if ((*_bagOfTetrominos.front()).checkCollision())
This code is obviously printing "Did Nothing" when checkCollision returns true.

**Update**: I may have misread the problem. Maybe still too early for my brain, but am I understanding what the problem is properly? Edited by SeanMiddleditch

Share this post


Link to post
Share on other sites

Sorry for the late reply guys, long day, just now finally able to sit down at my PC >_<
 

Sounds suspiciously what might happen if you write out of bounds somewhere (and probably somewhere else).

 

Are the convertedCoords always in bounds of _board?

 

Checking the values in the debugger it never goes out of bounds.

 

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness. 

That worked! I apologize I'm still new so can you clarify this statement for me cause I'm sorta lost. Is it just that what's being run isn't what I'm seeing on my screen? What exactly is it running instead? Just asking for my knowledge, thank you.

 

 

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness. 

This is usually the first thing you should do when you encounter something that doesn't make sense. The other thing you can do is look at the assembly code that is being executed and see if it doesn't jump to the wrong location.

 

As noted above this solved it, thank you!

 

Debugging per line "step into" should take you through the code per line. So you can see if the values are as expected and why true or false is returned. You could start the debugging by a break point in the main code, where you call the collision function.

Yeah even debugging, I would see it enter the if statement within the checkCollision method, go right to the return true, and like completely disregard it, just go outside of the if statement block, and go down the "}" until it got to the return false; then exit the method.

 

 

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness. 

 

This happens a lot; especially under Windows with Visual Studio.  Do a clean and rebuild.

 

This solved it thank you sir ;) and indeed I'm using Visual Studio.

 

It's doing exactly what you told it to do.
 

if ((*_bagOfTetrominos.front()).checkCollision())
This code is obviously printing "Did Nothing" when checkCollision returns true.

**Update**: I may have misread the problem. Maybe still too early for my brain, but am I understanding what the problem is properly?

 

It was printing  "Did Something" which confused the hell outa me lol. It's all good, mornings do that to me too.

 

How about you print a  seperator in between consecutive executions of the if statement that should help too.

The above solved it, but I definitely do appreciate your input.Thank you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!