Page 1 of 1

Help with functions (Tutorial 6)

Posted: August 11th, 2017, 8:38 pm
by Radical
I have been trying to get some functions working in one of my games, but I just can't. The function works fine for the actual PutPixel commands, but the part with the logic ceases to function.

I have recreated this issue in a minimalist game attached in the zip file.

I realize that if I replace "x" and "y" in the logic with "x1" and "y1", it would successfully move one of the boxes. But I want it to move both of the boxes, and do so without having to create a specific new function for each new box I create.

Re: Help with functions (Tutorial 6)

Posted: August 11th, 2017, 9:42 pm
by albinopapa
You were close.

Re: Help with functions (Tutorial 6)

Posted: August 12th, 2017, 4:54 am
by Radical
Ah, thank you very much. I don't really understand why your change works the way it does, or wtf the & operator is doing, but now I have a starting direction for my research at least.

(Also, I enjoyed your slight edit to my comments lol)

Re: Help with functions (Tutorial 6)

Posted: August 12th, 2017, 5:18 am
by albinopapa
Yeah, after I posted I realized that you probably haven't made it to references yet, sorry.

The parameters you pass to a function by value ( int x, int y ) makes a copy of the variables you pass in, so in function, x = x+1 has no affect on the x1, y1 you pass in. References allow you to pass in the variable as if you are using those variables, but with a different name. You can consider references to be an alas for the original and not a copy of the original. Chili will cover references in a few episodes from lesson 6 so you'll get it later.

I forgot about the comments, hehe. I like keeping all lines between 80 and 100 characters so it all fits in my window. I'm visually impaired so I have the font kind of big, around 16 pt. This way I only have to scroll vertically and not horizontally as well.

Re: Help with functions (Tutorial 6)

Posted: August 13th, 2017, 1:55 am
by Radical
Sorry to bother you again papa. I tried to do a bit of research on pointers/references, but I still don't understand why my original code doesn't work.

If you needed to use a pointer to assign the value of the variable "x1" to "x", I would fully understand that. But my issue is that even without a pointer/reference, the PutPixel calls work perfectly fine; the value of x1 is transferred to x, and their position is properly displayed on the screen.

I even put my "x = x + 1" logic inside of the same function as the PutPixel one. So the x already has the value of x1. Why would x lose the value of x1 as soon as it get's to my specific line of code involving "x = x + 1"?

Re: Help with functions (Tutorial 6)

Posted: August 13th, 2017, 5:21 am
by albinopapa
Refer back to passing by value I mentioned in previous post.

Code: Select all

void Game::Dude(int x, int y, int r, int g, int b)
{
	// You draw the object at the old X and Y
	gfx.PutPixel(x, y, r, g, b);
	gfx.PutPixel(x + 1, y, r, g, b);
	gfx.PutPixel(x + 1, y + 1, r, g, b);
	gfx.PutPixel(x, y + 1, r, g, b);
	gfx.PutPixel(x, y + 2, r, g, b);
	gfx.PutPixel(x + 2, y, r, g, b);
	gfx.PutPixel(x + 2, y + 1, r, g, b);
	gfx.PutPixel(x + 1, y + 2, r, g, b);
	gfx.PutPixel(x + 2, y + 2, r, g, b);

	// You update X and Y
	if (wnd.kbd.KeyIsPressed(VK_RIGHT))
	{
		x = x + 5;
	}
	if (wnd.kbd.KeyIsPressed(VK_LEFT))
	{
		x = x - 5;
	}
	if (wnd.kbd.KeyIsPressed(VK_UP))
	{
		y = y - 5;
	}
	if (wnd.kbd.KeyIsPressed(VK_DOWN))
	{
		y = y + 5;
	}

	// When function ends, this x and y are are destroyed and the Game::x1 is not changed,
	// because x and y are copies of x1 and y1.
}

void Game::ComposeFrame()
{
//	Movement1(bx, by);
	//You copy x1 and y1 to the x and y function parameters.
	Dude(x1, y1, r1, g1, b1);
	Dude(x2, y2, r1, g1, b1);
}
The reference or pointer points to the object you want to work with. So it's like Robert going by Rob, it's still Robert, but now you call him Rob.