home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / rec / games / programm / 5232 < prev    next >
Encoding:
Internet Message Format  |  1993-01-01  |  1.9 KB

  1. Path: sparky!uunet!caen!uflorida!elm.circa.ufl.edu!u9999aer
  2. From: u9999aer@elm.circa.ufl.edu (Cathryn Lasky)
  3. Newsgroups: rec.games.programmer
  4. Subject: Re: Goto [variable] in C++ ?
  5. Message-ID: <38103@uflorida.cis.ufl.edu>
  6. Date: 1 Jan 93 07:09:08 GMT
  7. References: <1992Dec28.144429.1427@jupiter.sun.csd.unb.ca>
  8. Sender: news@uflorida.cis.ufl.edu
  9. Distribution: rec.games.programmer
  10. Organization: University of Florida, Gainesville
  11. Lines: 43
  12. Nntp-Posting-Host: elm.circa.ufl.edu
  13.  
  14. In article <1992Dec28.144429.1427@jupiter.sun.csd.unb.ca> kinsman@jupiter.sun.csd.unb.ca (Aphoriel/Kinsman) writes:
  15. >This is a question from someone new to C...
  16. >
  17. >How could I set up C code so that a program goes to a function where the name
  18. >of the function is placed in a string variable? The specific use I'm thinking
  19. >of is for an adventure game, where the function the parser calls depends on
  20. >the verb the player used in typing in his command.. I could set it up as:
  21. >
  22. >-If verb_parsed is 'eat' then call eat_function
  23. >-If verb_parsed is 'give' then call give_function
  24. >
  25. >and so forth, but this is awful... How could I write it in C to look like:
  26. >
  27. >-Go to [verb_parsed + string_holding_parameter_characters]
  28. >
  29. >Like that.
  30. >
  31. >Thanks in advance,
  32. >-Sean Givan / Aphoriel/Kinsman
  33.  
  34. Hmm, I think this problem has come up several times before.  Unless
  35. there is some nifty new keen way of doing it, I don't think that this
  36. can be done easily.  The best way I can think of offhand is using
  37. a look-up table of function pointers and to tokenize the verb
  38. parameter.  For example:
  39.  
  40. enum Functions {
  41.   EAT, SLEEP, KILL
  42. };
  43.  
  44. void (*ftbl)(char *arglist)[MAX_FUNCTIONS];
  45.  
  46. void DoFunction( int token, char *arg_list )
  47. {
  48.    (*ftbl)(arg_list)[token];
  49. }
  50.  
  51. You may need to check the syntax, but this is in effect what you do.
  52. To add other functions you simply expand the token values (
  53. "Functions" ) and the ftbl.  This is as simple as you can get.
  54.  
  55. Brian
  56.  
  57.