How do I make a combat msg buffer?

This is a discussion on How do I make a combat msg buffer? within the Game Programming forums, part of the General Programming Boards category; The game I am working on is a text console game. I am having some trouble implementing the combat messages. ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    How do I make a combat msg buffer?

    The game I am working on is a text console game. I am having some trouble implementing the combat messages. I intend to have 4 lines of combat messages showing on the screen, the newest message would be pushed in at the bottom of the combat message box and effectively popping out the oldest message on the top. I think I know how to do that, I'll simply have 4 pointers and I will shift them up by one and have the pointer representing the newest line point to the new message string. So I am going to have a function that looks like

    void DrawCombatMsg(char *text);

    The argument would be the latest combat message, I don't know if passing it a cstring is the best way to do it but when I experimented with passing it a string class my compiler failed to recognize the function prototype in the header file It whined something about the identifier not found.

    But before I pass the latest combat message to my DrawCombatMsg() function I need to formulate the string (name of attacker, name of defender, hit or miss, how much damage). I have another function called

    FormCombatMsg()

    for that. Now this is where I am really confused. What kind of argument and return type should I put for FormCombat Msg function? Ideally I want it to return a string but the only way I know how to "form"(where I combine variables and strings) a string is by using cout and that that puts it directly to the standard output. So I guess my question is, is there a way that I can format a string and "save" it to another string so I can use it later on as an argument?

    I experimented with the string class of the string library and ostringstream of the sstream library, none of them really worked for me, most likely because I didn't use them right. Please help.

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Maybe you were using stringstream wrong
    First, include <sstream>. Let's say you create stringstream msg. Then you could pass a reference to a stringstream (msg) to FormCombatMessage:
    Code:
    void FormCombatMessage( stringstream &msg );
    //...
    stringstream msg;
    FormCombatMessage( msg );
    FormCombatMessage will fill it with info like a stream:
    Code:
    msg << Attack << " attacks " << Defender; //Etc.
    Then, DrawCombatMsg would use your prototype (void DrawCombatMessage( char* text )), and you can pass the stringstream using:
    Code:
    DrawCombatMsg( msg.str().c_str() );
    Then DrawCombatMsg does all that about the four messages and then outputs text. There you go

    - SirCrono6

    Edit: Shouldn't pass &msg, just msg.

    Edit 2: To avoid confusion:
    Code:
    FormCombatMessage( stringstream &msg ); //Prototype
    //...
    FormCombatMessage( msg ); //Function call
    Last edited by SirCrono6; 04-10-2005 at 04:39 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    The book I am reading tells me to use ostringstream. So instead of
    Code:
    void FormCombatMessage( stringstream &msg );
    I did
    Code:
    void FormCombatMessage( ostringstream &msg );
    Is that a problem? Also, the book tells me that using the str() method "freezes" the object, and I will no longer be able to write to it, does that mean the "msg" variable can only be used once in this case? Would I need to create it dynamically then if so?

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    You should use stringstream instead of ostringstream. str() will not "freeze" the object (test this code):
    Code:
    #include <iostream>
    #include <sstream>
    
    void Msg( std::stringstream &Hello )
    {
      int X = 10;
      Hello << X;
    }
    
    int main( void )
    {
      std::stringstream Hello;
      Msg( Hello );
      std::cout << Hello.str().c_str() << std::endl;
      Hello << " 10";
      std::cout << Hello.str().c_str();
      std::cin.get();
      return 0;
    }
    
    /* Output -
    10
    10 10
    */
    Also, see my edit (it's important).

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    Cool, thanks SirChrono! Now is there a way to write over the old string completely with new string or does whatever I add always get attached to the end of the original string? Also what's the difference between using stringstream and ostringstream?
    Last edited by pliang; 04-10-2005 at 05:10 PM.

  6. #6
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Ah, I've just found a way to clear the contents of a stringstream:
    Code:
    stringstream msg;
    msg.str( string() );
    There is a stringstream, ostringstream, and istringstream. I believe that they each lean towards either input or output (stringstream being neutral), but I'm not sure.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by SirCrono6
    Ah, I've just found a way to clear the contents of a stringstream:
    Remember to clear the flags also, or you might experience some troubles in some rare cases:

    Code:
    stringstream msg;
    msg.str( string() );
    msg.clear();
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple (?) problem rendering a vertex buffer
    By Dark_Phoenix in forum Game Programming
    Replies: 4
    Last Post: 08-11-2007, 07:32 PM
  2. How do I make a string buffer?
    By pliang in forum C++ Programming
    Replies: 6
    Last Post: 04-11-2005, 07:19 AM
  3. make Child Dialog not Popup?
    By Zeusbwr in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2005, 02:42 PM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21