Game Development Community

Scripting Features

by Gary "ChunkyKs" Briggs · in Torque Game Engine · 03/03/2005 (10:25 am) · 7 replies

I remember a while ago seeing a discussion about scripting features people would like. I can't find it, so I'll put some stuff here.

If anything I'm asking for is already supported, or if I'm being really stupid, please let me know.

1) an "@=" operator, for just appending something to the current string. I have code that looks like this:
for(%i = 0; %i < %pattlen; %i++) {
    %s = %s @ "(";

    %s = %s @ JMSiteValidator::siteChar(%left[%i]);
    %s = %s @ tertiary(%leftcross[%i], "x", "");
    %s = %s @ ",";
    %s = %s @ JMSiteValidator::siteChar(%right[%i]);
    %s = %s @ tertiary(%rightcross[%i], "x", "");

    %s = %s @ ")";
}

2) Multiline comments

3) Pass by reference. Currently, I do something like:
function modify(%s) {
    if(getSubStr(%s,0,1) $= "A") {
        %s = %s @ "Appended";
        return %s;
    } else {
        return "KnownBadValue";
    }
}

%s = "Something";
%t = modify(%s);
if(%t !$= "KnownBadValue") {
    %s = %t;
}

Which seems pretty hacky compared with something where I could pass by reference, and return an error code.

4) On the subject of references, a simple variable reference assignment. Currently, I have bits of code that look like this:
if(getRandom() > 0.5) {
  do_many_many_things_to_%left;
} else {
   copy_that_code_and_replace_%left_with_%right;
}
Much nicer [IMHO] would be:
if(getRandom() > 0.5) {
  %array = \%left;
} else {
  %array = \%right;
}
code_that_does_many_things_to_%array;


5) Something like C's tertiary operator. Current code:
function tert(%one, %two, %three) {
    if(%one) {
        return %two;
    } else {
        return %three;
    }
}

%tmp = tert(%a, "0", "1");
%a = tert(%b, "0", "1");
%b = %tmp;

While it's about as terse, it's simply not as clear, and pretty ugly to boot.

6) Finally, it would be nice if the compiler threw a warning for identifiers that appear exactly once. A simple tpyo in an identifier can lead to hours of debugging joy, where the compiler warning that "%numbals" only appears once would have made it pretty clear what the problem was, given that my code uses "%numballs" really quite a lot.

Thanks,
Gary (-;

#1
03/03/2005 (12:21 pm)
Pass by reference would also allow multiple things to be returns from a function, something that I'd also find very useful:
%one=0.0;
%five=0.0;
%fifteen=0.0;

if(get_loadaverage(\%one, \%five, \%fifteen) != 0) {
    echo("Loadaverage gave an error!");
    return;
}

echo("Load Average: " @ %one @ " " @ %five @ " " @ %fifteen);

Obviously a pretty trumped-up example, and meaningless to any non-unix people, but you get the idea.

Gary (-;
#2
03/03/2005 (1:33 pm)
You can already return a tuple (multiple things from a function). Concatenate them all into a string and getField() each item. Nicer syntax would be a good idea though.

The tertiary operator would be nice.

I thought variable reference assignment was already in.

Concatenate Assignment "@=" would be nice.

I don't follow what you're trying to get across in example #3.
#3
03/03/2005 (1:33 pm)
Also, I thought I read the script language would be getting an overhaul in the near future?
#4
03/03/2005 (1:40 pm)
Basically, in example #3, I'm trying to munge a string.
What I want to be able to do would be, in C, this:

int munge(char *string) {
do_things_to_string();
if(do_things_successful) return 0;
else return 1;
}

At the moment, there's no easy way to return both a string, and a success-or-failure code. I can have one, or the other. I consider returning an invalid string a pretty bad way of doing this. I could return, as you say, a return code *and* the modified string in the same string, but again, that feels like a hideous hack.

Gary (-;
#5
03/03/2005 (2:11 pm)
Gotcha. Yeah, it is pretty hackish. :)
#6
03/03/2005 (5:01 pm)
Hmm. Multiple return vars ala Lua would be awesome. I'll look into this one myself. As for the @=, I can probably whip up a patch to do that, you interested in that ChunkyKs?
#7
03/03/2005 (5:23 pm)
I was just hoping that something like this could become part of the official distribution in the future, given that they're working on it at the moment, apparently.

Hmmm. Quick implementation of "@=":

1) Add
"@=" return(CMDlval.i = opALASN);
to scan.l

2) Add
#define opALASN 311
to gram.h

3) to gram.y, add
opALASN
at the end of the line
%token opXORASN opORASN opSLASN opSRASN opCAT

4) to gram.y, also add
| opALASN expr
{ $$.token = '@'; $$.expr = $2; }

After the opMLASN bit.

As I said, it's not that it's hard. It's just thoughts for things to implement.

Gary (-;