How to pass keyboard control to new class function?

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: How to pass keyboard control to new class function?

Post by albinopapa » December 10th, 2016, 5:49 am

First off, if you are going to have a Keyboard object in the Control class, then you don't have to pass a Keyboard & to KeyPress. If keyOb is suppose to be a reference to the one stored in MainWindow, then you'd need to make keyOb a Keyboard & and initialize it using the Control constructor.

In the Control.h file:

Code: Select all

Control( Keyboard & Kbd );


private:
     Keyboard &kbd;
In the Control.cpp file

Code: Select all

Control( Keyboard &Kbd )
     :
     kbd(Kbd)
{
}

If you aren't trying to hold a reference, then take the Keyboard keyOb out of Control, and just pass it in to the KeyPress function and use it.

Not sure if this is what you are planning, but

Code: Select all

void Control::GetDeets(int PlayerLocX, int PlayerLocY, int GrabPlayerNumber)
{
	int CoPlayerX = PlayerLocX;
	int CoPlayerY = PlayerLocY;
	int CoPlayerNum = GrabPlayerNumber;
}
Just as a warning, you are declaring new variables here, not assigning new values to the members of Control.
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

Ducky
Posts: 4
Joined: December 9th, 2016, 2:18 am

Re: How to pass keyboard control to new class function?

Post by Ducky » December 11th, 2016, 1:49 am

chili wrote: Now I'm not 100% clear about what your problem is though. You say: "I'm not quite grasping when and where (.cpp/.h?) to create objects to access things". Maybe there's a little disconnect in terminology.
I'm sure there's a disconnect with a lot of things :lol: I feel honored to speak to the man himself :) Must have heard your voice for over 30 hours now, love the comedic relief, thanks for taking time to teach others, I've felt very welcomed by the programming world, I think there is something that draws similar characters to this interest. no homo

What I was getting lost on was essentially what albinopapa touched on with:
albinopapa wrote:First off, if you are going to have a Keyboard object in the Control class, then you don't have to pass a Keyboard & to KeyPress. If keyOb is suppose to be a reference to the one stored in MainWindow, then you'd need to make keyOb a Keyboard & and initialize it using the Control constructor.
I was confused as to how to allow a new class access to the Keyboard, what the role of the constructor was, and if I could declare a Graphics object in the header file of another class to access PutPixel.
I had tried sending variables along with graphics& reference into a new class's constructor but came up against errors. I'm assuming the constructor only accepts certain arguments? (sorry if I'm butchering the terminology).

I'm going to try implimenting albinopapa's advice now, allowing access by passing graphics pointer in the constructor and then declaring a graphics object to access PutPixel.. at least that's what I think is being suggested haha, soon to find out.

albinopapa wrote:Just as a warning, you are declaring new variables here, not assigning new values to the members of Control.
Oops, thanks for pointing that out :) it was pushing 5am when I hastily tackled that :D

I'm in the UK (GMT) hence the delayed response.
I learned modding and GtkRadiant when I was 13 (UT2,Quake3 era) learned Visual Basic 6 at collage for a year, worked as a IT tech for 3 years, lost interest in computers for almost 10 years. Now 28. Thanks for reading my life story. :lol:

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

Re: How to pass keyboard control to new class function?

Post by chili » December 11th, 2016, 4:27 am

Yeah, you shouldn't declare a new Graphics object anywhere. Consider, a graphics object represents the screen (the client area of your application's window). There is only one screen, so there should only ever be one graphics object. You pass references to that graphics object around to any other objects that need to access its services. :)

The role of the constructor is to initialize the object. Is there anything in particular that you are unclear on in that regard?
Chili

Post Reply