constructor trouble with private member from another class

The Partridge Family were neither partridges nor a family. Discuss.
Skyver
Posts: 12
Joined: September 26th, 2017, 12:35 pm
Location: Netherlands

constructor trouble with private member from another class

Post by Skyver » November 30th, 2018, 5:19 pm

hey there long time no see yall


so i am doing homework 13 from the poo game i had the goal made and made it pulse color in a neat way(Had a lot of problems but solved them all like a boss) so the next step was to add a custom hitdetection i could repurpose it to make the poos bounce off the goal
so i wrote a hit detec function like this//disclaimer :i don,t know if this code will even run nicely

but i think it should or at least i think i can make it work

Code: Select all

bool Goal::HitDetection(Player & P)
{
	
	int GP = Point;
	int PX = P.GetX();
	int PY = P.GetY();
	int GDim = Point + GoalWH;
	int PXDim = P.GetX() + P.GetWH();
	int PYDim = P.GetY() + P.GetWH();

	if (GP <= PXDim && GDim >= PX && GP <= PYDim && GDim >= PY)
	{
		Test = true;
	}
	return Test;

}
the detection needs to have the player pos to work so i used the getters for it

but when i run this bs it starts trowing a hissy fit at the getter function after a look in MR.debugeur
i found out that the member vars that the getters get are not init when the code above runs

my anwser was i need to load them at start pefferbly when Goal class get loaded so i made a constructor in that class to init it

Code: Select all

Goal::Goal(Player& P)
	:
	P(P)
{
}
i tought this would load the whole class by the time hit detection would call the getters

this dini,t happen tho and i could not quite get why but it is what it is

so i was thinking that i maby could load the member var itself instead of the whole class

so underneath here is the things i try,d

Code: Select all



Goal::Goal(Player& P)
	:
	P(P),
       X(X)
{
}

Goal::Goal(Player& P)
	:
	P(P),
        P.X
{
}

Goal::Goal(Player& P)
	:
	P(P),
       Player::X
{
}

Goal::Goal(Player& P)
	:
	P(P),
       X(P)

{
}


i really think it has to do with me fucking up at loading the class or cus the member var is private

but and the end the member var is still not loaded

it might be that cus the class im calling is lower in the header list so it loads later or it only loads when a functon gets called of that class honestly i have no idea


but at this point im out of ideas

ofcourse i could trow alot of code around until i get it working but that would be way way way less shiny the having it work like i intended
that and this is what my brain came up with so i want to followtrough and get it working correctly


i don,t have git hub just yet the tut for that is a little further so ist a bit messy in here
but i hope i made some sense and that

someone with that sweet liquid code knowledge goodness can spray some on my face so i can rub it on my eyes and come to see the error of my ways



yours trully that random guy from a year ago that said that he had a good idea (not that great of an idea turns out )


and in advance thank you verry much for the help your willing to give me


Skyver

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: constructor trouble with private member from another cla

Post by albinopapa » November 30th, 2018, 7:54 pm

Hey skyver, how's it hangin'?

This original code:

Code: Select all

bool Goal::HitDetection(Player & P)
{ 
   int GP = Point;
   int PX = P.GetX();
   int PY = P.GetY();
   int GDim = Point + GoalWH;
   int PXDim = P.GetX() + P.GetWH();
   int PYDim = P.GetY() + P.GetWH();

   if (GP <= PXDim && GDim >= PX && GP <= PYDim && GDim >= PY)
   {
      Test = true;
   }

   return Test;
}
Should have no problems and is pretty much the same formula for rectangle on rectangle collision detection. The difference is the names of variables really.

I would suggest a couple of things for this function, but the issue you seem to be having is what exactly? Are you saying that the Player& P object reference doesn't have the values initialized that Player::GetX() and Player::GetY() are returning?

If so, then all the stuff you are doing with the Goal class probably isn't going to help, as the issue seems to be with what you are passing to the Goal::HitDetection function.

I understand that you may not have GitHub at the moment, but there are other ways of sharing a project. Make a zip/rar file with the .sln file and all files in the Engine subfolder excluding any folders labeled Debug, Release, x86 or x64. This way the compressed file only contains the stuff needed and none of the VS BS.

So, first thing would be to compress the files needed to help us get the whole picture because a lot of times the bug isn't where you think it is.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: constructor trouble with private member from another cla

Post by albinopapa » November 30th, 2018, 8:21 pm

Code: Select all

bool Goal::HitDetection(Player & P)
{ 
   int GP = Point;
   int PX = P.GetX();
   int PY = P.GetY();
   int GDim = Point + GoalWH;
   int PXDim = P.GetX() + P.GetWH();
   int PYDim = P.GetY() + P.GetWH();

   if (GP <= PXDim && GDim >= PX && GP <= PYDim && GDim >= PY)
   {
      Test = true;
   }

   return Test;
}
Here are the changes I personally would make:

Code: Select all

   // Not sure why Point is representing an X and Y position for Goal.  
   // This means the goal can only ever be at ( 0, 0 ) to ( Graphics::ScreenHeight, Graphics::ScreenHeight ) in a diagonal 
   // line.  Shouldn't goal have X and Y same as the poos and player?
   // Since this is rectangle on rectangle action, you could treat these like rectangles

   // Is GoalWH suppose to be; GoalWH() ?
bool Goal::HitDetection(Player const& P)
{
   const int goal_left = x;
   const int goal_top = y;
   const int goal_right = x + GoalWH;
   const int goal_bottom = y + GoalWH;
   
   const int player_left = P.GetX();
   const int player_top = P.GetY();
   const int player_right = P.GetX() + P.GetWH();
   const int player_bottom = P.GetY() + P.GetWH();

   const bool isHit = 
      ( x <= P.GetX() + P.GetWH() && x + GoalWH >= P.GetX() ) && 
      ( y <= P.GetY() + P.GetWH() && y + GoalWH >= P.GetY() );

   return isHit;
}
Another thing that might be a better option, would be to create a Rect class, and a CollidesWith function.

Code: Select all

struct Rect
{
   Rect() = default;
   Rect( int _left, int _top, int _right, int _bottom )
      :
   left( _left ), top( _top ), right( _right ), bottom( _bottom )
   {}

   int GetWidth()const { return right - left; }
   int GetHeight()const { return bottom - top; }

   bool CollidesWith( Rect const& _other )
   {
      return
         ( left <= _other.right && right >= _other.left ) &&
         ( top <= _other.bottom && bottom >= _other.top );
   }

   int left = 0;
   int top = 0;
   int right = 0;
   int bottom = 0;
};

// Player.h
#include "Rect.h"
class Player
{
   //... same code as before except for using a bounding rect to determine collision
   Rect GetRect()const
   {
      return Rect( x, y, x + GetWH(), y + GetWH() );
   }

};

// Goal.h
#include "Rect.h"
class Goal
{
   // ... same for goal
   Rect GetRect()const
   {
      return Rect( x, y, x + GoalWH, y + GoalWH );
   }
};

// Game.cpp
// I would test for collision in Game to avoid having Goal.h #include Player or Player.h #include Goal.h.  
// Things get confusing and tricky when you create these types of dependencies.
void Game::UpdateModel()
{
   if( player.GetRect().CollidesWith( goal.GetRect() ) )
   {
      // reset the goal, update points, add more poo or whatever
   }
}
You could/should do the same for the Poo class as well.

Chili will cover this stuff, though I can't remember how for down the line, perhaps in the next few episodes.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Skyver
Posts: 12
Joined: September 26th, 2017, 12:35 pm
Location: Netherlands

Re: constructor trouble with private member from another cla

Post by Skyver » December 1st, 2018, 2:05 pm

im a little unsure but i zipped up the files and post it if you would take a look at it that would be awsome dini,t commet much but i hope you can see whats going on

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: constructor trouble with private member from another cla

Post by albinopapa » December 1st, 2018, 6:33 pm

Where did you post it to?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: constructor trouble with private member from another cla

Post by chili » December 2nd, 2018, 11:47 am

o_o
Chili

Skyver
Posts: 12
Joined: September 26th, 2017, 12:35 pm
Location: Netherlands

Re: constructor trouble with private member from another cla

Post by Skyver » December 2nd, 2018, 1:02 pm

so uplouding from netherlands to japan is awfull dini,t work and it was not going so i hav struggeld to get github going i think its up on my new github so sorry for the confusing shit and i hope you can help me a bit

https://github.com/SkyverNL/PooGame.git

plzz use branch HitDetection cus that has the stupid suff Worked out of it and is the most current ;)

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: constructor trouble with private member from another cla

Post by albinopapa » December 2nd, 2018, 9:37 pm

Ok, when I go to the HitDetection branch on GitHub, I get a "404 error" like it doesn't exist. I downloaded the repository and switched to the HitDetection branch and all the files disappear, so something went wrong with that branch synchronization.

So in the master branch, you have the Goal class taking a Player object by reference in the constructor of Goal. Now, in Game, there is no " goal( P0 ) " line in the game initializer list so Visual Studio is telling me that I need to initialize goal because there is no default constructor. If I put " goal( P0 ) ", that error goes away.

Visual studio complains about Game::UpdateModel() because MaxE is 10 and you cycle from 0 to 10 inclusive when the array is only 0 to 9 inclusive.

Code: Select all

	for (int i=0; i <= MaxE; i++)
// should be 
	for( int i = 0; i < MaxE; i++ )
Aside from the constructor issue and this, there doesn't seem to be an issue.

However, there is no reason the Goal class should need to store a reference to a Player object reference. Storing a reference to an object makes that object non-copy-able and unmovable. Your hit detection logic should not be in the Draw function, this should be called from perhaps the Game::UpdateModel function where you can pass in Game's instance of Player.

If you do have a member such as the Player& in Goal, you wouldn't have needed to pass it to the HitDetection function anyway, you could have just used it directly.

For instance, you have this function Goal::ColorPulsing(int& R, int& G, int& B), unless it's called from some other place in code, then there is no reason for it to take in parameters since you are just passing in member data from Goal.

It can be simply:

Code: Select all

void Goal::ColorPulsing()
{
	if (R > 0 && R < 255 && !ReversePulse)
	{
		++R;
	}
	else if (R == 255 && G < 255 && !ReversePulse)
	{
		++G;
	}
	else if (G == 255 && B < 255 && !ReversePulse)
	{
		++B;
	}
	
	if((R == 255) && (G == 255) && (B == 255))
	{
		ReversePulse = true;
	}
	
	if (ReversePulse && B >= 1 )
	{
		--B;
	}
	else if (ReversePulse && G >= 1)
	{
		--G;
	}
	else if (ReversePulse && R >= 1)
	{
		--R;
	}
	else if ((ReversePulse) && (R == 0) && (G == 0) && (B == 0))
	{
		ReversePulse = false;
		R = 1;
		G = 1; 
		B = 1;
	}
}
Then called like: ColorPulsing();

When you were debugging, were you in Debug mode and not Release mode?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Skyver
Posts: 12
Joined: September 26th, 2017, 12:35 pm
Location: Netherlands

Re: constructor trouble with private member from another cla

Post by Skyver » December 3rd, 2018, 12:09 am

oke i have git hub sorted now tested it and it runs like butter amazing thing bythe way very handy

so i looked in to the goal(P0) like you say but for me that doesn,t work nor does vs complain to me

the loop thing you said was on point and i fixed that thanks for the tip

also the rgb i used a refrance for was stupid indeed and fixed that again thanks for pointing that out

and the main issue from hit detection you said that i should put it in update model in instead of
so i can call Player from there i did that but it gave me the same error

i made a comment at the error so you can find what i mean

i know i have been a pain in the ass so far but i really appreciate the help your giving me


https://github.com/SkyverNL/PooGame.git

this is the new reposetory it works and there is only one branch for now i hope you can help me further

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: constructor trouble with private member from another cla

Post by albinopapa » December 3rd, 2018, 6:36 am

Github Pull Request

Here are the changes I was talking about.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply