Article View: rec.games.programmer
Article #1653Re: Goto [variable] in C++ ?
From: Cathryn Lasky
Date: Fri, 01 Jan 1993 07:09
Date: Fri, 01 Jan 1993 07:09
42 lines
1968 bytes
1968 bytes
In article <1992Dec28.144429.1427@jupiter.sun.csd.unb.ca> kinsman@jupiter.sun.csd.unb.ca (Aphoriel/Kinsman) writes:
>This is a question from someone new to C...
>
>How could I set up C code so that a program goes to a function where the name
>of the function is placed in a string variable? The specific use I'm thinking
>of is for an adventure game, where the function the parser calls depends on
>the verb the player used in typing in his command.. I could set it up as:
>
>-If verb_parsed is 'eat' then call eat_function
>-If verb_parsed is 'give' then call give_function
>
>and so forth, but this is awful... How could I write it in C to look like:
>
>-Go to [verb_parsed + string_holding_parameter_characters]
>
>Like that.
>
>Thanks in advance,
>-Sean Givan / Aphoriel/Kinsman
Hmm, I think this problem has come up several times before. Unless
there is some nifty new keen way of doing it, I don't think that this
can be done easily. The best way I can think of offhand is using
a look-up table of function pointers and to tokenize the verb
parameter. For example:
enum Functions {
EAT, SLEEP, KILL
};
void (*ftbl)(char *arglist)[MAX_FUNCTIONS];
void DoFunction( int token, char *arg_list )
{
(*ftbl)(arg_list)[token];
}
You may need to check the syntax, but this is in effect what you do.
To add other functions you simply expand the token values (
"Functions" ) and the ftbl. This is as simple as you can get.
Brian
Technical Details
Message-ID:
<38103@uflorida.cis.ufl.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!caen!uflorida!elm.circa.ufl.edu!u9999aerReferences:
<1992Dec28.144429.1427@jupiter.sun.csd.unb.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!uflorida!elm.circa.ufl.edu!u9999aer From: u9999aer@elm.circa.ufl.edu (Cathryn Lasky) Newsgroups: rec.games.programmer Subject: Re: Goto [variable] in C++ ? Message-ID: <38103@uflorida.cis.ufl.edu> Date: 1 Jan 93 07:09:08 GMT References: <1992Dec28.144429.1427@jupiter.sun.csd.unb.ca> Sender: news@uflorida.cis.ufl.edu Distribution: rec.games.programmer Organization: University of Florida, Gainesville Lines: 43 Nntp-Posting-Host: elm.circa.ufl.edu