I have made a program which utilizes a loop to output a string character by character with a delay of 100 milliseconds inbetween, until, the user presses ENTER which speeds up the process by changing the delay to 10 miliseconds (This function, not THIS exact method, is what you usually see in games to speed up the npc's character text.)
But the bad thing is I only know how to do this for a defined string(When I mean "defined" I mean it has a stream of text stored) . So if my game had a thousand sentences for NPC's, and I wish to implement the method of speeding up the sentence by pressing enter, I would have to make a thousand strings and for loops which would take FOREVER and take up alot of memory...Code:#include <iostream> #include <string> #include <windows.h> using namespace std; int main( int argc, char* args[]) { string intro_speech = "Hello, and welcome to the introduction!"; unsigned int speed_msg = 100; for(unsigned i = 0; i < intro_speech.length(); i++) { cout << intro_speech[i]; Sleep(speed_msg); if (GetAsyncKeyState(VK_RETURN)){ speed_msg = 10; } else if(GetAsyncKeyState(VK_RETURN) != true){ speed_msg = 100; } } cin.ignore(); cin.get(); return 0; }
I thought of making a function but I don't know how to go about it..
I want it use it like this:
So basically the function "Speed_Msg" would be made up with the code blocks you see in the program posted at the beginning of this thread, but without having to use a defined string, it will use the cout command and know what string to output by looking at the argument in its parameter (As you can see by the example above.)Code:Speed_Msg("This is an example");
But I don't know how to set up the function to do something like that? Any comments or help will be greatly appreciated!