Game Development Community

Meta TorqueScript

by Daniel Buckmaster · in General Discussion · 08/30/2013 (6:33 pm) · 1 replies

Okay, I just had a brainwave. I just wrote two functions that looked like this:
function Level::getForwards(%this, %from) {
   if(%from $= "") {
      %from = "0 0 0";
   }
   return getWord(%from, 0) SPC getWord(%this.forwards, 1) SPC 0;
}
function Level::getBackwards(%this, %from) {
   if(%from $= "") {
      %from = "0 0 0";
   }
   return getWord(%from, 0) SPC getWord(%this.backwards, 1) SPC 0;
}
Don't worry about the logic, just notice that they're the same function twice, using the words forwards and backwards respectively.

Then I wrote this:
foreach$(%w in "forwards backwards") {
   eval(
   "function Level::get" @ %w @ "(%this, %from) {" @
      "if(%from $= "") {" @
         "%from = "0 0 0";" @
      "}" @
      "return getWord(%from, 0) SPC getWord(%this." @ %w @ ", 1) SPC 0;" @
   "}"
   );
}

HOLY CRAP IT WORKS.

(But there should be some backslashes, which the forum seems to eat.)

So I've decided what I'm going to do is this. Eventually. I'm going to write a macro preprocessor for TorqueScript. It will be what CoffeeScript is to JavaScript - the exact same underlying semantics, with loads of syntax sugar on top. And I'm going to add an easy way to do what I just did above, so you can script your scripts. It's like Template TorqueScript. The idea is that if you exec a file with a certain extension, my custom preprocessor will run and turn the file into a regular TS file. It should be possible to leverage a preprocessor to do stuff like:
if($enableSomething) {
   function xyz(...) {}
}
which we can't do with TS's parser as it is.

That way we combine the benefits of TorqueScript, such as they are, while leveraging the ridiculous fact that TS is all strings, and I get to quit bitching about the language design :P.

About the author

Studying mechatronic engineering and computer science at the University of Sydney. Game development is probably my most time-consuming hobby!


#1
08/30/2013 (7:13 pm)
Daniel, you'd really like some of the hideous eval() hacks I pulled in 3 Step Studio's Tower Defense Wave Editor....