Game Development Community

Char Functions?

by rmullen · in Torque Game Builder · 04/22/2006 (10:05 am) · 5 replies

The Console dump() command is pretty helpful, I found the majority of what I needed there, but I still had a few questions. Its been years since my C++ classes, (and they never got into OOP) so there are quite a few dumb questions rolling around in my head. Here's one...

I have a string, lets say...%Command.

I've already parsed %string to get everything between two brackets, as per '[L234]', which goes into %Command.

Now, my objective is to find the lowercase letter 'd', then check for a number after it. I'm going to use some pseudocode, since I'm not sure of the real way to do this.

%commandstart=strpos(%string, "[");
%commandend=strpos(%string, "]");
echo("(" SPC %commandstart SPC "," SPC %commandend SPC ")");
/////
//insert OnError code here, sooner or later
/////
if((%commandstart>=0)&&(%commandend>0))
{
  %Command=getSubStr(%string, %commandstart, %commandend+1-%commandstart);
  echo(%Command);  // echos the current contents of the parsed command

  %beginDcommand=strpos(%Command, "d");
//begin pseudocode
for(%i=0, %i>4);
  char %charafterD=getChar(%Command, %beginDcommand+%i);
  if (charCode(%charafterD)!=#)  //  How would I check to see whether the character was a numeral?
   {                                               // I assume I would use ASCII codes or something similar
   %i=10
   } else
   {
   %number=getSubStr(%Command, %beginDcommand+1, %beginDcommand+%i);
}

I know this is somewhat inelegant and clunky, but you get the picture, I think. The main objective was to check: Is there a 'd' in there somewhere? If so, is there a '#' after it? How many '#'s follow? Put whole series of '#'s into %number. Then I could theoretically try:
%diceroll=getRandom(0, %number);
Right?

#1
04/22/2006 (10:12 am)
Okay, I found a blog here that states there is no function in Torquescript to check the ASCII value of a char. Is this still valid?

If it is, I'll modify his code to work in my application. *shrugs*
#2
04/22/2006 (11:32 am)
Hey R-

Here's some really useful console functions for playing with strings. Add these to your consoleFunctions.cc file and rebuild:

ConsoleFunction(strasc, int, 2, 2, "(char)")
{
   argc;
   return (int)(*argv[1]);
}
 
// -- Formats the given value as a string, using a printf-style format string --- jason_cahill -
ConsoleFunction(strformat, const char *, 3, 3, "(string format, value)"
                "Formats the given given value as a string, given the printf-style format string.")
{
   argc;
   char* pBuffer = Con::getReturnBuffer(64);
   const char *pch = argv[1];
 
   pBuffer[0] = '[[60c251eddab70]]';
   while (*pch != '[[60c251eddab70]]' && *pch !='%')
      pch++;
   while (*pch != '[[60c251eddab70]]' && !dIsalpha(*pch))
      pch++;
   if (*pch == '[[60c251eddab70]]')
   {
      Con::errorf("strFormat: Invalid format string!\n");
      return pBuffer;
   }
    
   switch(*pch)
   {
      case 'c':
      case 'C':
      case 'd':
      case 'i':
      case 'o':
      case 'u':
      case 'x':
      case 'X':
         dSprintf(pBuffer, 64, argv[1], dAtoi(argv[2]));
         break;
 
      case 'e':
      case 'E':
      case 'f':
      case 'g':
      case 'G':
         dSprintf(pBuffer, 64, argv[1], dAtof(argv[2]));
         break;
 
      default:
         Con::errorf("strFormat: Invalid format string!\n");
         break;
   }
 
   return pBuffer;
}

This adds an "sprintf" like command called "strFormat" and a function to get the ASCII value of a charact "strAsc". Hope this helps!
#3
04/22/2006 (12:37 pm)
Thanks for the help, but I'm not very accomplished with real world use of C++, so I'll try to stay out of the source for now, just for...self preservation. :)

Okay, one more question in this topic,

getsubstr(%Command, %Dposition+1, %Dposition+1);

gives me a string 2 characters long. ??? I thought to use rtrim, but I can't tell how that's supposed to work, and there's no getChar() function (I'm used to AGS functions and such, that's the most recent form of code I've used.)

I'm hacking away at it, I've got the ascii code working now, at least partly. I just can't get it to pull out a single character, instead of a two char string. :( I'll post if I get it working.
#4
04/22/2006 (12:58 pm)
Try this: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5718

I use this rtf file quite regularly and found it extremely handy :)
#5
04/22/2006 (2:25 pm)
Thanks for that link, it answered my question about the trim() family, and also about strchr()! That's just what I needed. Now I don't have to use strpos.

Oh, and in case anyone else noticed, I screwed up the syntax on that getSubStr() above. The last argument should have been the length (1), and not the ending position. Argh.

Now I can type [d#] (where # is a number with an indefinite number of digits) and get a random number between 1 and #. That's actually a huge step towards what I needed. :)