0

I have been trying to make a drop-down console for my game. I have gotten the console to "drop-down" and all that, but when it comes to input, I have issues (things crash when the user types stuff). In the this particular source file, I have the main function which checks for key presses. If for instance getConsoleInput() detected that the key "G" was pressed, it would call appendLetter("g"). I believe the issue is with appendLetter() itself. When I run the game, I can type one letter, but if I type another, it crashes.

/* ConsoleInput.cpp */

#include "Engine.h"

int timeout = 0;
int lengthText = 0;
std::string line;
std::string sampleText;



void appendLetter(std::string L)
{
	line.append(L, lengthText, 1);
	lengthText++;
}


void getConsoleInput()
{
	if (timeout > 8)
	{
		//~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
		//Letters (A-Z, space)
		//~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
		if (Key_Down(DIK_A))
		{
			timeout = 0;
			appendLetter("a");
			
		}
		else if (Key_Down(DIK_B))
		{
			timeout = 0;
			appendLetter("b");
		}
		
		//rest of else if statements for other keys



		//~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
		//Special Actions (backspace)
		//~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
		else if (Key_Down(DIK_BACKSPACE))
		{
			timeout = 0;
			if (lengthText > 0)
			{
				line.erase(lengthText-1, lengthText);
				lengthText--;
			}
		}



	}
	timeout++;
}
2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by SnakeJam
0

Hm, this seem like one of those cases where you overemphasize on the methods for <string>.

What I did was remove your manual count, 'cause essentially you don't need to do the counting yourself when there is such a function to do it for you: std::string::length().
Executed as such

string foo = "bar";
cout<<foo.length(); //output 3

The string member std::string::append() has many (8 actually) interesting different overloaders, one of which does all the thinking for you and all you have to do is supply a string for it to append, by default it appends at the end of the string. Assuming this is what you wish to achieve there is no need to tell the append overloader that you wish for it to place it at the end with 1 character length. In the end, complicating an otherwise easy function may mess up your end result.

Here's some good hard documentation
std::string::append()

Try this

#include "Engine.h"

int timeout = 0;
std::string line;

int LineLength() { return line.length(); }

void getConsoleInput()
{
	if (timeout > 8)
	{
		if (Key_Down(DIK_A))
		{
			timeout = 0;
			line.append("a");
			
		}
		else if (Key_Down(DIK_B))
		{
			timeout = 0;
			line.append("b");
		}
                //........
		else if (Key_Down(DIK_BACKSPACE))
		{
			timeout = 0;
                        int length = LineLength();
			if (length > 0)
			{
				line.erase(length-1, 1);
			}
		}



	}
	timeout++;
}

This should work, if not, there might be more behind this console (which I assume there is) that can be the cause for this, for example,

  • how do you render your console line to the screen
  • is the console line rendered directly or does it go through a buffer

Edited by ShadowScripter: n/a

0

Yes, your code worked perfectly. There was no problem with my rendering system.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.