🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

References can't be used with more than one variable.

Started by
24 comments, last by Josheir 4 years, 1 month ago

Well I just learned an important lesson. References can not be used to point to different variables. I am trying to refactor everything that is affected. The function parameters have been changed from references to pointers. My guess is that the address of operator used in the function call will dismiss the error. Is this workable? I am making a different character move using the original function so I need pointers to use the sprite and positioning, etc.

Advertisement

Oops, sent with a click. The function is a member function. It is C++.

Thanks, Josheir

Can you show some code of what you are trying to do?

I am guessing you are referring to the fact references can not be changed to refer to something else once initialised? Assigning to them changes the thing they reference, not the reference themselves (C++ references are really not much like other languages “references” at all, pointers are closer in many way, other than the “a.b()” vs “a->b()” thing).

There are still many places in C++ using references is preferable (especially the “const T &param” ones that are more a “by-value, but don't do the expensive copy”), or almost required (operator overloads), although the extent of pointers vs references is always debated.

In C++, references are bound to a variable. They can not be reseated.

It gets confusing, in that function paramters that are passed by (cv-qualified) reference are bound to the actual arguments. They are not reseatable, but are context-dependent. In other words, they are bound to a (potentially) different variable on every function invokation. It doesn't matter if they are member functions or namespace functions.

I agree with @syncviews: post your code. It sounds like you're doing something wrong based on a fundamental misunderstanding of the language. Remember: references are not pointers.

Stephen M. Webb
Professional Free Software Developer

Okay, sure I can post the code.

//in between both endzones or in left endzone
int Character::movesRightInEndZoneToo(int facingLeft, const int SCREENHEIGHT, const int SCREENWIDTH, sf::RenderWindow& window, int& goingDown, int& characterLeft, int& characterY, std::vector<platform>& vector, sf::Sprite& marioSpriteL, sf::Sprite& marioSpriteR, Character& character, int& isLeft, float& positionY, float & positionX)
{
	goingDown = 1;
	//not in this section
	if (currentCharacterPlacement >= rightEndzoneLine)
	{
		//calls while to drop
		int val = movesRightInRightEndZoneToo(facingLeft, SCREENHEIGHT, SCREENWIDTH, window, goingDown, characterLeft, characterY, vector, marioSpriteL, marioSpriteR, character, isLeft, positionY, positionX);
		{
			if (val == -1)
			{
				return(-1);
			}
			return(1);
		}
	}
	//not used anymore
	sf::Vector2f  position = marioSpriteR.getPosition();
	int temp1 = position.x;
	int temp2 = position.y;

	
	//int notNeeded1, notNeeded2 = 0;
	int answer = character.isOnPlatform(window, marioSpriteR, marioSpriteL, SCREENHEIGHT, goingDown, vector, SCREENWIDTH, characterLeft, characterY, positionY, positionX);
	//on platform
	if (answer == 1)
	{

		//moving right in left hand endzone - on platform
		if (currentCharacterPlacement <= leftEndzoneLine)
		{
			//move platforms
			//platform::SearchVectorForPlatforms(vector, window, 0.0f, 0.0f, 0);
			currentCharacterPlacement = currentCharacterPlacement + VELOCITYX;
			positionX = positionX + VELOCITYX;
			marioSpriteL.setPosition(positionX, positionY);
			marioSpriteR.setPosition(positionX, positionY);
			window.clear();
			window.draw(marioSpriteL);
			platform::SearchVectorForPlatforms(vector, window, 0.0f, 0.0f, 0);
			window.display();
			//return(1);
			//currentLeftEdge = currentLeftEdge + VELOCITYX;
		}
		//moving right midzone - om platform
		else
		{
			//platform::SearchVectorForPlatforms(vector, window, -VELOCITYX, 0.0f, 0);
			currentCharacterPlacement = currentCharacterPlacement + VELOCITYX;
			marioSpriteL.setPosition(POSXCENTER, positionY);
			marioSpriteR.setPosition(POSXCENTER, positionY);
			window.clear();

			window.draw(marioSpriteL);
			platform::SearchVectorForPlatforms(vector, window, -VELOCITYX, 0.0f, 0);
			window.display();
			//return(1);


			//currentLeftEdge = currentLeftEdge + VELOCITYX;
		}



		//is still on platform?
		answer = character.isOnPlatform(window, marioSpriteR, marioSpriteL, SCREENHEIGHT, goingDown, vector, SCREENWIDTH, characterLeft, characterY, positionY, positionX);
		//was on, moved off
		if (answer == 0)
		{
			//drop - off platform
			//remove these:
			int no = 0;
			int no1 = 0;
			int goingDown2 = 0;
			//calls fall
			return(-1);
			//whileFunction(facingLeft, character, clock, window, marioSpriteR, marioSpriteL, SCREENHEIGHT, goingDown, vector, SCREENWIDTH, no, no1, goingDown2, positionY, positionX);
		}

		//still on platform after move
		else
		{
			//in mid zone
			if (currentCharacterPlacement >= leftEndzoneLine)
			{
				//platform::SearchVectorForPlatforms(vector, window, 0, 0.0f, 0);
				currentCharacterPlacement = currentCharacterPlacement + VELOCITYX;
				//currentLeftEdge = currentLeftEdge - VELOCITYX;
				//positionX = positionX + VELOCITYX;
				marioSpriteL.setPosition(POSXCENTER, positionY);
				marioSpriteR.setPosition(POSXCENTER, positionY);
				window.clear();

				window.draw(marioSpriteL);
				platform::SearchVectorForPlatforms(vector, window, -VELOCITYX, 0.0f, 0);
				window.display();
				return(1);

			}
			//in leftzone
			else
			{
				
				currentCharacterPlacement = currentCharacterPlacement + VELOCITYX;
				positionX = positionX + VELOCITYX;
				marioSpriteL.setPosition(positionX, positionY);
				marioSpriteR.setPosition(positionX, positionY);
				window.clear();
				window.draw(marioSpriteL);
				platform::SearchVectorForPlatforms(vector, window,0, 0.0f, 0);
				
				
				window.display();
				return(1);
			}
		}

	}
	//not on platform
	else
	{
		//not on platform
		if(positionY >= SCREENHEIGHT - 76)
		{ 
		//not on platform
		//left endzone
		if (currentCharacterPlacement <= leftEndzoneLine)
		{
			//platform::SearchVectorForPlatforms(vector, window, -2.0f, 0.0f, 0);
			currentCharacterPlacement = currentCharacterPlacement + VELOCITYX;
			positionX = positionX + VELOCITYX;
			//currentLeftEdge = currentLeftEdge + VELOCITYX;
			//isLeft = 0;
			
			
			marioSpriteL.setPosition(positionX, positionY);
			marioSpriteR.setPosition(positionX, positionY);
			window.clear();
			
			window.draw(marioSpriteL);
			platform::SearchVectorForPlatforms(vector, window, 0, 0, 1);
			window.display();
			return(1);
			
		}
		//in middle - moving right -  not on platform
		else
		{


			currentCharacterPlacement = currentCharacterPlacement + VELOCITYX;
			//currentLeftEdge = currentLeftEdge + VELOCITYX;
			marioSpriteL.setPosition(POSXCENTER, positionY);
			marioSpriteR.setPosition(POSXCENTER, positionY);
			window.clear();
			window.draw(marioSpriteL);
			platform::SearchVectorForPlatforms(vector, window, -VELOCITYX, 0.0f, 0);
			
			window.display();
			return(1);
		}

		}//is not on ground
		else
		{
			int no = 0;
			int goingDown2 = 0;
			//whileFunction(facingLeft, character, clock, window, marioSpriteR, marioSpriteL, SCREENHEIGHT, goingDown, vector, SCREENWIDTH, no, no, goingDown2, positionY, positionX);
			return(-1);
		}

		

	}

	return(1);
}

This is the original header:

int movesRightInEndZoneToo(int facingLeft, const int SCREENHEIGHT, const int SCREENWIDTH, sf::RenderWindow& window, int& goingDown, int& characterLeft, int& characterY, std::vector<platform>& vector, sf::Sprite& marioSpriteL, sf::Sprite& marioSpriteR, Character& character, int& isLeft, float& positionY, float &positionX);
	

This is the new header:


int movesRightInEndZoneToo(int facingLeft, const int SCREENHEIGHT, const int SCREENWIDTH, sf::RenderWindow* window, int* goingDown, int& characterLeft, int& characterY, std::vector<platform>* vector, sf::Sprite* marioSpriteL, sf::Sprite* marioSpriteR, Character* character, int* isLeft, float* positionY, float *positionX);
	

Description:

When calling the function with a different sprite, the original sprite moves to it's position and does the two pixel movement.

I'm sorry if this should have been in the beginner's forum.

Thank you,

Josheir

On Github:

https://github.com/josheirm/MarioBros/tree/GettingReadyForEnemy

I'm not offering a fix, just an observation…

Don't use pointers or references to basic types as arguments. (or really never ever unless you have a specific need)

int* goingDown
int& characterLeft

just use

int goingDown 
int characterLeft

It's simpler, faster, and easier to reason about.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

The most important code:

answer = character.movesRightInEndZoneToo(isLeft, SCREENHEIGHT,  SCREENWIDTH, window, goingDown, temp1, temp2,  vectorPlatforms, &marioSpriteL, &marioSpriteR, character, isLeft, positionY, positionX);
			

Note the left and right marioSprites,, how they have the address operator and now work as a pointer. Is this the solution to my problem?

thanks,

Josheir

I am wondering when they will fix the editor, but anyways I mean work with a pointer.

Thank you, fleabay!

This topic is closed to new replies.

Advertisement