Game Development Community

Enhanced GetField

by Ritchey "Hawk" Mulhollem · in Torque Game Engine · 10/17/2004 (6:23 am) · 2 replies

One of the first things I noticed about torque script was the inability to specify delimiters or tokens. Most of the functions are hard coded to "\t\n" as acceptable delimiters. This modification changes that.

In engine/console/consoleFunctions.cc replace GetField with this:

ConsoleFunction(getField, const char *, 3, 4, "getField(text, index [,set])")
{
   argc;
   const char *str;
   if(argc==3)
      str =  "\t\n";
   else
      str = argv[3];

   return getUnit(argv[1], dAtoi(argv[2]), str);
}

This is backwards compatible with existing torque script code, but also allows you to specify your own delimiter set. Say for example you are parsing a CSV file. You would do:

getfield(%x,3,",");

Now you have just obtained the 4th field in the CSV delimited record!

I am NOT a C programmer, so if the above code is not right PLEASE let me know! I have tested it and it does appear to work.

#1
10/18/2004 (11:15 am)
Take a look at the nextToken function :)
#2
10/18/2004 (11:27 am)
Yes, I am familiar with that, however, "nextToken" will not allow you to access one specific field. You have to sit there chokin' your token until you finally reach the one you want.

As per my example, you would need 3 lines of "nextToken" to duplicate my 1 line of "getfield".

I wish I knew more about C because I would write some code to make a split function like every other language has.