Jump to content
  • Advertisement
Sign in to follow this  
MatthewDiaz

Problem With User Input In My Game

This topic is 2447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically I have a good working font system that uses a vector of strings to display multiple sentences on the screen in one draw call. The problem comes when I try to find a way to store user input in one of these strings in the vector. When I make and define a char array and modify it based on button presses and then save it in my vector, checking the length of that string from the vector gives me the total length rather than the total amount of letters, which I need. What is the best way for me to always have a char array with no more elements than the letters within it for user input? I tried having a int that increments with each button press and use that for the definition of the char array for the element size, but the compiler wants it to be a constant? Any suggestions?

Share this post


Link to post
Share on other sites
Advertisement

Why dont you just use std::string?


Technically my vector is a group of objects with char array members as the "strings". I guess I wasn't clear on that, sorry.

Share this post


Link to post
Share on other sites
Oh, so its like this: vector<char*/string>? A vector of multiple strings. What language are you using (assuming C++, just want to make sure)

Share this post


Link to post
Share on other sites
You can't resize a char array so you will have to allocate a new char array and copy if you need to make it bigger. You also need to put '\0' at the end. Using std::string will make life much easier.

Share this post


Link to post
Share on other sites
Nah it's vector<StringDisplay> stringdisplay_;


#pragma once
#include <cstring>

//String display vector in GameFont will change based on the current screen state
class StringDisplay
{
public:
StringDisplay(char* string, float startx, float starty, int size);
~StringDisplay();
StringDisplay(const StringDisplay& sd);
void Duplicate(char* string, float startx, float starty, int size);
StringDisplay& operator=(const StringDisplay& sd);

void SetString(char* str);
void SetStartPositions(float startx, float starty);

char* GetString();
float GetStartX();
float GetStartY();
int GetStringMaxSize();

private:
char* string_;
float startx_;
float starty_;
int stringmaxsize_;

};

//My GameFont object holds the vector and modifies it, here is the function to add a string to vector<StringDisplay> object.
//The size controls how much of the string is displayed on screen, has no bearning on the size of the actual char array.

void GameFont::AddString(char* str, float x, float y, int size)
{
stringdisplay_.push_back(StringDisplay(str, x, y, size));
}

void GameFont::SetStringSpecific(char* str, int index)
{
stringdisplay_.at(index).SetString(str);
}



So you see when I do a call like AddString("hello", 1.0f, -1.0f, 20), and i ask for the length I get 5.
If I define a char string[40] though put hello in it element by element throuh user input and use SetStringSpecific, asking for it's length gets me 40, instead of 5.

Share this post


Link to post
Share on other sites

What are you doing to ask for the length? Did you try strlen()?


yeah that is what I am using.

int length = strlen( font.GetStringDisplay().at(i).GetString() );

Someone said I need to add my own end char. I didn't try that so that may work.

Share this post


Link to post
Share on other sites
Yeah, just set the last character to '\0' and strlen will stop there. How are you creating the string? If its a constant, it should already have the \0, and you may be copying it wrong (if youre copying it)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!