Author Topic: Simple command parser (debug window)  (Read 125 times)

OgreVorbis

  • Posts: 8
Simple command parser (debug window)
« on: 12 Dec 2018, 18:45 »
I was thinking about implementing a "console" type thing for my game so that I can enter debugging commands and cheats. As far as I can tell, AGS doesn't have something like this built in, right? There is a little list when I press the tilde key, but it doesn't seem interactive. I also see there is a parser object, but I think this is only meant for text adventures(?)

Anyway, I already got something simple going where the command is entered in "command(param)" format. This is working well, but I want to have "command(param1,param2,param3,etc)" I know it is probably possible, but my skill is limited. In C#, I could just split them into string array, but I don't see that in AGS.
Would anyone be willing to either suggest me something if this already exists, or help me to split the param by comas and put into a string array. It is OK to be a fixed length array with about [10] because I think that will be plenty. Some need to be numbers, but I think I can just convert to int after the fact.
Also, if anyone has any good tricks to "translating" object,etc. names to strings so that they can be entered real time in the debug window, that would be appreciated. I already figured out the inventory by looping through all the items and extracting their names and using the string with that.
« Last Edit: 12 Dec 2018, 18:49 by OgreVorbis »

OgreVorbis

  • Posts: 8
Re: Simple command parser (debug window)
« Reply #1 on: 13 Dec 2018, 14:04 »
I got it working with this:
Code: Adventure Game Studio
  1. function WriteLine(String name)
  2. {
  3.   DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();
  4.   surface.DrawingColor = Game.GetColorFromRGB(255, 255, 255);
  5.   surface.DrawString(0, pos, eFontNormal, name);
  6.   surface.Release();
  7.   pos += 8;
  8.   if (pos > 210)
  9.     pos = 14;
  10. }
  11. function ExecCommand(String command)
  12. {
  13.   String param[10];
  14.   int comapos[10];
  15.  
  16.   if (command == "help")
  17.   {
  18.     Display("The command format is 'command(parameter1,parameter2,etc.)'[Parameters must have no space after the coma.");
  19.     Display("All output will display on top of the game background and will clear when the player leaves room.");
  20.     //Display("Here is a list of the commands.");
  21.   }
  22.  
  23.   if (command == "exit" || command == "e")
  24.     command = "exit()";
  25.  
  26.   int loc = command.IndexOf("(");
  27.   if (loc == -1)
  28.   {
  29.     WriteLine("Invalid Command");
  30.     return;
  31.   }
  32.   String cpy = command.Copy();
  33.   String cmd = command.Truncate(loc);
  34.   String para = cpy.Substring(loc + 1, cpy.Length - loc - 2);
  35.  
  36.   int i = 0;
  37.   int j = 0;
  38.   while (i < para.Length - 1)
  39.   {
  40.     if (para.Chars[i] == ',')
  41.    {
  42.       comapos[j] = i;
  43.       j++;
  44.       if (j >= 10)
  45.       {
  46.         Display("ERROR: Too many parameters.");
  47.         return;
  48.       }
  49.     }
  50.     i++;
  51.   }
  52.  
  53.   if (j == 0)
  54.   {
  55.     param[0] = para;
  56.   }
  57.   else
  58.   {
  59.     param[0] = para.Substring(0, comapos[0]);
  60.    
  61.     i = 0;
  62.     while (i <= j)
  63.     {
  64.       if (comapos[i + 1] == 0)
  65.         param[i + 1] = para.Substring(comapos[i] + 1, para.Length - comapos[i]);
  66.       else
  67.         param[i + 1] = para.Substring(comapos[i] + 1, comapos[i + 1] - comapos[i] - 1);
  68.       WriteLine(param[i]);
  69.       i++;
  70.     }
  71.   }
  72.  
  73.   switch(cmd)
  74.   {  }
  75. }
  76.