• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Writing a kill message function

## Recommended Posts

I''m trying to write a kill message function. The function has access to the killers name, the targets name, and the kill message string. I want to return the kill message string, but the symbols %k and %t, which are killer and target respectivly, must be replaced with the proper names. I''m sure this isn''t too difficult, but I''ve had trouble working with strings in C/C++. Defintitly not as easy as workin with them in QBasic

##### Share on other sites
I may be completely ignorant, but I don't have the slighest idea of what you want to do. What is this "kill message" function? It looks rather like you want to format a string to expand variables, which is easily done with printf() like functions or with string streams: www.cppreference.com.
EDIT: Show how you would do it in QBasic (or other pseudo code), and it will be easier to propose a good C/C++ solution.

[edited by - CWizard on January 28, 2003 9:30:17 PM]

##### Share on other sites
I''m trying to make kill messages, similar to ones found in other games... like quake 2 ("Player1 ate Player2''s rocket").

In QBasic, working with a string as such is very easy. I forget the commands, but it allows my to add messages to each other, like this:

"Hello" + " World"

And you could search and replace words in strings using the MID$function. As you probably know, C doesn''t allow that. The string functions have me baffled. Plus, my old C book, which has a chapter on it, was somehow skipped in the printing... theres this big gap of about 30 pages. #### Share this post ##### Link to post ##### Share on other sites Ok, I read messages such as system messages. Oh well... You can work pretty easy with strings in C++, using the string class. Check out the above link, but I guess you should find some better introction. Try Bruce Eckel's excellent book Thinking in C++ 2nd Edition, which you can download for free. As illustration, you can do like this: #include <string> std::string KillMessage(std::string killer, std::string target){ return target + " ate " + killer + "'s rocket";}  You may want to skip the std:: prefix on the string type, by inserting a using namespace std; after the #include, but I like to do it explicitly. EDIT: For search and replace, old plain C functions might do well. For example you can do like this: char target[] = 'Player1';char killer[] = 'Player2';char buffer[128]; // make sure big enoughsprintf(buffer, "%s ate %s's rocket", target, killer);  [edited by - CWizard on January 28, 2003 10:49:49 PM] #### Share this post ##### Link to post ##### Share on other sites One thing you can do, is give control to this function to the Killer Manager or something. For example, in my game, I have a player who kills others, so when he fires a bullet or arrow, system checks whether an enemy is dead, if he is, it will retrieve bullet'' attributes (i.e ID from what weapon it was shot from and the parent of it (The gun) and the parent of the gun (player)) etc etc. By using this hierarchy, I can simply say:  void CBullet::CheckDamage(){CPlayer *player = GetCollidedModel(this);if(player !=NULL)return;If(player->State == STATE_DEAD) printf("Player %s was killed by %s with %s", player->name, this->pParentWeapon->pParentPlayer->name, this->pParentWeapon->name);}  " Do we need us? " #### Share this post ##### Link to post ##### Share on other sites I think your looking for the strstr function which is kind of the equivilent to mid$ ? It will give you the location inside the string where your search string is, I think.... Its been a while since I used it but I totaly understand where your coming from, string manipulation is just a pain in the butt... However, you can use, like CWizard mentioned, sprintf, to format a string with variables.

say killer.name is "Killer", victim.name is "Victim"

char kill_msg[128];

sprintf( kill_msg, "%s just killed %s", killer.name, victim.name );

this gives you
"Killer just killed Victim"

hope this helps, look up printf, sprintf in the reference CWizard gave the link to to get a listing of the other *lacks the propper wording* things like %s and how to use them, there are different ones for integer %i, and double %d etc.

• ### Forum Statistics

• Total Topics
627719
• Total Posts
2978789

• 9
• 21
• 14
• 12
• 42