How to pass keyboard control to new class function?

The Partridge Family were neither partridges nor a family. Discuss.
Ducky
Posts: 4
Joined: December 9th, 2016, 2:18 am

How to pass keyboard control to new class function?

Post by Ducky » December 9th, 2016, 2:35 am

Hi all, thanks for taking the time to read this and thanks chili for taking the time to teach us, much appriciated.

I've ended up deviating from the tutorials somewhat, and attempted to use (wnd.kbd.KeyIsPressed) in a new Class and function (not in the constructor) but I'm getting a red line under "kbd" telling me Keyboard has no member "kbd"

Code: Select all

void Control::KeyPresses(MainWindow&kbd, Keyboard &wnd)
{
		if (CoPlayerNum = 1)
		{
			if (wnd.kbd.KeyIsPressed(VK_UP)) {
				CoPlayerY = CoPlayerY - 1;
What is the correct way to allow use of kbd?

My header file looks like

Code: Select all

#pragma once
#include "Keyboard.h"
#include "MainWindow.h"
class Control
{
public:
	Control(int GetControlX,int GetControlY,int PlayerNum);
	void KeyPresses(MainWindow&kbd, Keyboard &wnd);
	int SetBoundaryX(int PosX);
	int SetBoundaryY(int PosY);
private:
	int CoPlayerX;
	int CoPlayerY;
	int CoPlayerNum;
};
and the cpp:

Code: Select all

#include "Control.h"
#include "Keyboard.h"
#include "MainWindow.h"
#include "Graphics.h"



Control::Control(int PlayerLocX, int PlayerLocY, int GrabPlayerNumber)
{
	int CoPlayerX;
	int CoPlayerY;
	int CoPlayerNum;
	KeyPresses();
}


void Control::KeyPresses(MainWindow&kbd, Keyboard &wnd)
{
		if (CoPlayerNum = 1)
		{
			if (wnd.kbd.KeyIsPressed(VK_UP)) {
				CoPlayerY = CoPlayerY - 1;
			}
			if (wnd.kbd.KeyIsPressed(VK_DOWN)) {
				CoPlayerY = CoPlayerY + 1;
			}
			if (wnd.kbd.KeyIsPressed(VK_LEFT)) {
				CoPlayerX = CoPlayerX - 1;
			}
			if (wnd.kbd.KeyIsPressed(VK_RIGHT)) {
				CoPlayerX = CoPlayerX + 1;
			}
			CoPlayerX = SetBoundaryX(CoPlayerX);
			CoPlayerX = SetBoundaryY(CoPlayerX);
			if (wnd.kbd.KeyIsPressed('I')) {
				Fire::Fire(CoPlayerX, CoPlayerY, 1);
			}
		}
		if (CoPlayerNum = 2) {
			if (wnd.kbd.KeyIsPressed(0x57)) {
				CoPlayerY = CoPlayerY - 1;
			}
			if (wnd.kbd.KeyIsPressed(0x53)) {
				CoPlayerY = CoPlayerY + 1;
			}
			if (wnd.kbd.KeyIsPressed(0x41)) {
				CoPlayerX = CoPlayerX - 1;
			}
			if (wnd.kbd.KeyIsPressed(0x44)) {
				CoPlayerX = CoPlayerX + 1;
			}
			CoPlayerX = SetBoundaryX(CoPlayerX);
			CoPlayerY = SetBoundaryY(CoPlayerY);
			if (wnd.kbd.KeyIsPressed('T')) {
				Fire::Fire(CoPlayerX, CoPlayerY, 2);
			}
		}
	}
int Control::SetBoundaryX(int GetLocX)
{
	int right = GetLocX + 5;
	int left = GetLocX - 5;
	if (right >= Graphics::ScreenWidth)
	{
		return Graphics::ScreenWidth - 6;
	}
	else if (left < 0)
	{
		return 5;
	}
	else {
		return GetLocX;
	}
}
int Control::SetBoundaryY(int GetLocY)
{

	int Up = GetLocY - 5;
	int down = GetLocY + 5;
	if (Up <= 0) {
		return 5;
	}
	else if (down >= Graphics::ScreenHeight)
	{
		return Graphics::ScreenHeight - 6;
	}
	else {
		return GetLocY;
	}
}


Im sure some of it doesn't make sence atm and I've no idea if it will run because I'm not able to solve this kbd problem, any help is much appreciated!

I'm not sure if you have later explained this in the tutorals but I'm reluctant to bindge watch the next few to find out and risk flooding my brain and losing interest in this current problem and thus not learning the answer to what has taken me hours :P
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:

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 9th, 2016, 2:42 am

Keyboard &wnd

You are passing a referrence to a keybaord object here, not a MainWindow object.

Your wnd is a reference to a keyboard object, and can be used directly as wnd.KeyIsPressed( somekey );
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: How to pass keyboard control to new class function?

Post by albinopapa » December 9th, 2016, 2:43 am

Also, it's more helpful to upload the project after cleaning the project and zipping it up, or even easier is to post to GitHub and post the link to your repository here.
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 9th, 2016, 2:51 am

Wow thanks for the quick reply! I only attempted using MainWindow thinking "wnd" was a member of it and required to use kbd lol, I'll get back to the tutorials.

I'll follow your advice about GitHub in the future as well, I'm guilty of reading no stickys sorry!
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:

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 9th, 2016, 3:39 am

No prob.
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
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

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

Post by LuisR14 » December 9th, 2016, 4:42 am

funny thing tho is that you were mixing up the names xP
Ducky wrote:

Code: Select all

	void KeyPresses( MainWindow &kbd, Keyboard &wnd );
	...
	void Control::KeyPresses( MainWindow &kbd, Keyboard &wnd )
(notice kbd on MainWindow, wnd on Keyboard :P), but as albpapa said, ya only need the KeyB reference :)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

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 9th, 2016, 4:44 am

albinopapa wrote:Also, it's more helpful to upload the project after cleaning the project and zipping it up, or even easier is to post to GitHub and post the link to your repository here.
Next tutorial is gonna be on using Git & GitHub.
Chili

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

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

Post by egizz983 » December 9th, 2016, 9:26 am

You dont need wnd object to pass all you need is a Keyboard& kbd and include ChiliWin.h for VK codes , and then you can pass wnd.kbd from game or any where you construct control object
exmaple :
void KeyPresses(Keyboard& kbd);
in game.cpp
Control::KeyPresses(wnd.kbd);

and as i mentioned for VK codes you need include ChiliWin.h

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

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

Post by Ducky » December 10th, 2016, 1:16 am

Hi again all, I thought I had the problem solved but I think I got a little too ahead of myself :lol:

What I've attempted to do is put some basic controls from the early tutorials (pre-poo game) into their own classes then "daisy chain" one class function to call another classes functions and so on. I'm sure this probably isn't the best way of doing things IRL but I thought it would serve good practice to see how things can link together.

I've made a GitHub and have a rudimentary knowledge of how it works, I'm assuming this is how you link to it https://github.com/Xeptic/ChiliTuts

If anyone could pop their head in and point me in the right direction I'll have their babies.

I'm not quite grasping when and where (.cpp/.h?) to create objects to access things

(I'm aware the fire::shoot is doing some whacky shit, I was working on it before I went down this rabbit hole)

Thanks again for your time, I promise to help noobs when I’m competent enough :D
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 10th, 2016, 2:29 am

Hey Ducky, nice work with the repo. It'll make troubleshooting a lot easier.

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.

We create classes to model entities (or sometimes concepts) of the system we're creating. We create (instantiate) objects (instances) based on our classes (models, moulds, cookie cutters). We generally add functions to our classes to enable us to access properties (variables) or invoke behaviors on our objects (instances of the class).
Chili

Post Reply