[ACS] Parsing a CVAR in a specific way?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
TDRR
Posts: 831
Joined: Sun Mar 11, 2018 4:15 pm
Location: Venezuela

[ACS] Parsing a CVAR in a specific way?

Post by TDRR »

So, let's say i got an ACS script that checks a CVAR for coordinates to spawn a thing. I want to store them as fixed-point coords, separated by a dot or some other special character.
First fixed-point number is X coords, the second one right after a dot are the Y coords and the third one is the angle, probably a byte-angle. And if another dot is found the next coords are for another thing, and finally if some other special character (Let's say, a comma?) is found then the spawning list is ended and there's nothing else to spawn.

Is this possible?

Also, it doesn't matter much if the solution is fairly CPU-intensive, as this will only happen at map startup.
User avatar
Empyre
Posts: 68
Joined: Thu Apr 05, 2007 10:39 pm
Location: Garland, TX, USA

Re: [ACS] Parsing a CVAR in a specific way?

Post by Empyre »

You could make the CVar be type string, and then use the ACS String Operations to manually parse the string.
User avatar
Empyre
Posts: 68
Joined: Thu Apr 05, 2007 10:39 pm
Location: Garland, TX, USA

Re: [ACS] Parsing a CVAR in a specific way?

Post by Empyre »

Now that I have had some sleep, I'll try to write an example.

Code: Select all

#define MAXTHING 10
int ThingXCoord[MAXTHING];
int ThingYCoord[MAXTHING];
int ThingAngle[MAXTHING];

function bool GetCoordsFromCVar(void)
{
  str S = GetCVarString("ThingCoords");
  int LengthS = StrLen(S);
  int i; //index into the arrays
  int j; //index into S
  bool WeAreDone = FALSE;
  bool CoodDone = FALSE;
  until (WeAreDone)
  {
    until (CoordDone)
    {
      switch(GetChar(S,j))
      {
        case '0';
        ThingXCoord[i] = ThingXCoord[i] * 10;
        break;

        case "1";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 1;
        break;

        case "2";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 2;
        break;

        case "3";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 3;
        break;

        case "4";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 4;
        break;

        case "5";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 5;
        break;

        case "6";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 6;
        break;

        case "7";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 7;
        break;

        case "8";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 8;
        break;

        case "9";
        ThingXCoord[i] = ThingXCoord[i] * 10 + 9;
        break;

        case ".";
        CoordDone = TRUE;
        break;

        default;
        Return FALSE;
        break;
      }
      j++;
      if(j==LengthS) Return FALSE;
    }
    CoordDone=FALSE;
    until (CoordDone)
    {
      switch(GetChar(S,j))
      {
        case '0';
        ThingYCoord[i] = ThingYCoord[i] * 10;
        break;

        case "1";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 1;
        break;

        case "2";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 2;
        break;

        case "3";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 3;
        break;

        case "4";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 4;
        break;

        case "5";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 5;
        break;

        case "6";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 6;
        break;

        case "7";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 7;
        break;

        case "8";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 8;
        break;

        case "9";
        ThingYCoord[i] = ThingYCoord[i] * 10 + 9;
        break;

        case ".";
        CoordDone = TRUE;
        break;

        default;
        Return FALSE;
        break;
      }
      j++;
      if(j==LengthS) Return FALSE;
    }
    CoordDone=FALSE;
    until (CoordDone)
    {
      switch(GetChar(S,j))
      {
        case '0';
        ThingAngle[i] = ThingAngle[i] * 10;
        break;

        case "1";
        ThingAngle[i] = ThingAngle[i] * 10 + 1;
        break;

        case "2";
        ThingAngle[i] = ThingAngle[i] * 10 + 2;
        break;

        case "3";
        ThingXAngle[i] = ThingAngle[i] * 10 + 3;
        break;

        case "4";
        ThingAngle[i] = ThingAngle[i] * 10 + 4;
        break;

        case "5";
        ThingAngle[i] = ThingAngle[i] * 10 + 5;
        break;

        case "6";
        ThingAngle[i] = ThingAngle[i] * 10 + 6;
        break;

        case "7";
        ThingAngle[i] = ThingAngle[i] * 10 + 7;
        break;

        case "8";
        ThingAngle[i] = ThingAngle[i] * 10 + 8;
        break;

        case "9";
        ThingAngle[i] = ThingAngle[i] * 10 + 9;
        break;

        case ".";
        i++;
        CoordDone = TRUE;
        break;

        case ",";
        CoordDone = TRUE;
        WeAreDone = TRUE;
        break;

        default;
        Return FALSE;
        break;
      }
      j++;
    }
  }
  Return TRUE;
}
There might be errors, but this will at least give you a good start. Feel free to change the names of the variables, the function, and the CVar, as well as the value of MAXTHING.
User avatar
TDRR
Posts: 831
Joined: Sun Mar 11, 2018 4:15 pm
Location: Venezuela

Re: [ACS] Parsing a CVAR in a specific way?

Post by TDRR »

Empyre wrote:Now that I have had some sleep, I'll try to write an example.

an example.

There might be errors, but this will at least give you a good start. Feel free to change the names of the variables, the function, and the CVar, as well as the value of MAXTHING.
That's really helpful! Will post back if i have any problems.
User avatar
Empyre
Posts: 68
Joined: Thu Apr 05, 2007 10:39 pm
Location: Garland, TX, USA

Re: [ACS] Parsing a CVAR in a specific way?

Post by Empyre »

I have thought of a couple thing I left out, and an improvement. Near the beginning, you need to check if the length of the string i 0, and if it is, you're done already. Whenever you reach a period, you need to check if the number you just read is a legal coordinate (or angle). Instead of returning a boolean, you could return an int: the number of successfully-read coordinate-angle sets, with zero meaning either an invalid string, or no string.
Post Reply

Return to “Scripting”