Archived

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

Tsu

sscanf(?)

Recommended Posts

Tsu    122
I decided to make a simple little scripting engine in c, and i''ve run into a little problem. i havent started to program it yet, because i want to make sure i know what i''m doing first ... anyway, so far (although its only on paper) it has the capability to read in a command from a ''script'' file, and put it (and it''s paremeters) into a string variable. Now i want it to figure out which command i want to run, and run it using it''s paremeters... i looked through a book of mine, and the command i thought might work the best is sscanf. my question is, will this work:
  
if (sscanf(command_to_run,"move %s %i %i",playername,xmove,ymove)){
  move(playername,xmove,ymove);
  }

//just for fun, another one... (hey that rhymes)

if (sscanf(command_to_run,"load_map %s",loadthismap)){
  loadmap(loadthismap);
  }
  
assuming command_to_run = "move player1 3 4", i guess this example would move the player named ''player1'' 3 to the right and 4 to the left. It doesnt matter though, i just want to know if this will work,or theres an easier way...

Share this post


Link to post
Share on other sites
Null and Void    1088
You should do it like this (if I understand what you want):
  
/* Declare you variables here */
/* Get stuff in command_to_run now... */
char command_value[50];
sscanf(command_to_run,"%s",command_value);

if(strcmp(command_value,"move") {
/* %*s means discard the first string */
sscanf(command_to_run,"%*s%s%i%i",playername,&xmove,&ymove);
move(playername,xmove,ymove);
} else if(strcmp(command_value,"load_map") {
sscanf(command_to_run,"%*s%s",loadthismap)
loadmap(loadthismap);
} else {
/* ... */

}


"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/

Share this post


Link to post
Share on other sites