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

Started by
13 comments, last by Servant of the Lord 9 years, 1 month ago

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.

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.

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

Are the convertedCoords always in bounds of _board?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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.

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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.

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

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?

Sean Middleditch – Game Systems Engineer – Join my team!

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

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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.

This topic is closed to new replies.

Advertisement