Simple command parser (debug window)

Started by OgreVorbis, Wed 12/12/2018 18:45:08

Previous topic - Next topic

OgreVorbis

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.

OgreVorbis

I got it working with this:
Code: ags
function WriteLine(String name)
{
  DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();
  surface.DrawingColor = Game.GetColorFromRGB(255, 255, 255);
  surface.DrawString(0, pos, eFontNormal, name);
  surface.Release();
  pos += 8;
  if (pos > 210)
    pos = 14;
}
function ExecCommand(String command)
{
  String param[10];
  int comapos[10];
  
  if (command == "help")
  {
    Display("The command format is 'command(parameter1,parameter2,etc.)'[Parameters must have no space after the coma.");
    Display("All output will display on top of the game background and will clear when the player leaves room.");
    //Display("Here is a list of the commands.");
  }
  
  if (command == "exit" || command == "e")
    command = "exit()";
  
  int loc = command.IndexOf("(");
  if (loc == -1)
  {
    WriteLine("Invalid Command");
    return;
  }
  String cpy = command.Copy();
  String cmd = command.Truncate(loc);
  String para = cpy.Substring(loc + 1, cpy.Length - loc - 2);
  
  int i = 0;
  int j = 0;
  while (i < para.Length - 1)
  {
    if (para.Chars[i] == ',')
    {
      comapos[j] = i;
      j++;
      if (j >= 10)
      {
        Display("ERROR: Too many parameters.");
        return;
      }
    }
    i++;
  }
  
  if (j == 0)
  {
    param[0] = para;
  }
  else
  {
    param[0] = para.Substring(0, comapos[0]);
    
    i = 0;
    while (i <= j)
    {
      if (comapos[i + 1] == 0)
        param[i + 1] = para.Substring(comapos[i] + 1, para.Length - comapos[i]);
      else
        param[i + 1] = para.Substring(comapos[i] + 1, comapos[i + 1] - comapos[i] - 1);
      WriteLine(param[i]);
      i++;
    }
  }
  
  switch(cmd)
  {  }
}


SMF spam blocked by CleanTalk