Game Development Community

TorqueScript Improvement Status

by JeffH · in Torque 3D Professional · 03/01/2014 (4:47 pm) · 35 replies

Current improvements to TS:

Check it out here:

https://github.com/JeffProgrammer/Torque3D

*Lamda functions (original code credits to Konrad Kiss from his resource)
*Native global variable arrays
*Native local variable arrays
*foreach loop for iterating through native arrays.

Other plans that I have (Difficulty for me indicated):

* pass arrays to functions [harder]
* make object arrays (%obj.array[] = new array[3];) [harder]
* @= operator [mid]
* fix do-while loops to end with a semicolon [easy]

Native arrays do NOT break current arrays in torquescript. In fact, to access elements of the new array, its the same way. You don't even have to have to use the new array declaration if you still wish to use torquescript style arrays. What this is for is eventually be able to just pass the array instead of passing each individual variable. It kinda makes a wrapper for it. If you are still confused, or need more clarification, see post 18.

Code examples:

//Lamda function:
function bleh() {
   schedule(1, 0, function() {

   });
}

// local array
function blah() {
   %array[] = new array[2];
}

// global array
$array[] = new array[5];

// foreach iterator with array
function arrayForTest() {  
   %array[] = new array[3];  
   %array[1] = 1;  
   foreach (%i in %array[]) {  
      echo(%i);  
   }  
}
Page «Previous 1 2
#1
03/01/2014 (5:08 pm)
see below post, redacted code i had here as it wasn't working.
#2
03/01/2014 (10:52 pm)
I got them to work! Native arrays now allocate (not pass to functions yet, that's next)

Edit: code removed, that's why the github is there now =)
#3
03/01/2014 (11:27 pm)
Wow, this is kind of cool. So this doesn't conflict with the existing [] syntax for script variables?
#4
03/02/2014 (5:15 am)
Did something similar in my consoletyperefactor branch. https://github.com/jamesu/Torque3D/tree/consoletyperefactor

Which basically allows you to do things such as:

$array = {1,2,3};
$array[0] = 0;
echo($array[0]);

Though currently it is largely abandoned due to lack of interest.

A core problem at least with the current scripting implementation is that all variables are assumed to be strings. Also you have the problem that the array operator is normally used to generate variable names. In order to fix things properly you need to do one of the following:

A) Make a new sigil to define array values
B) Refactor the whole console variable stack
C) Make a copy value operator so at least values can be copied without needing to be converted to strings

In any case, good luck! :)
#5
03/02/2014 (6:00 am)
Wow. Diving into the TS parser and OP calls. Brave and skillful. Nice contribution!
#6
03/02/2014 (7:53 am)
@Michael Thanks, I already got lamda functions working as well in my build. You can do this now too:

%var = function() {
   return 5;
}

function bleh() {
   // do something
   schedule(50, 0, function() {
      // do something 50ms later
   });
}

That was thanks to Konrad Kiss's resource that he made a few years back, however some changes had to be made with it to be compatible with Torque3D.

@James thanks for the tips, much appreciated.

@DB Nope, you can do this:

%array = new Array[5];
%array[0] = 1;

// native TS code before this works
%b[0] = 1;

All this code does, is simply adds local variables into the stack. So it still uses the same system as it does before. Its pretty neat eh :P
#7
03/02/2014 (12:02 pm)
Quote:Thanks, I already got lamda functions working as well in my build. You can do this now too

Now THAT is truly epic. I never used lambdas until I started iOS development and even more recently using them in more complex C# work. Both improvements you have made would be excellent contributions to both T3D and T2D. I would find myself using them heavily in the T2D editor work.
#8
03/02/2014 (12:55 pm)
Once I learn github a bit more, I'll probably make a repo with the improvements.

Another thing I plan on doing is adding @= operator for concatenation since I am understanding the parser somewhat :P

Michael how is the editor work coming along. I plan on using T2D for projects as well once editors come and I have an actual project to use it on. You have an email by the way that I could reach ya at for any purposes relating the torque technology?
#9
03/02/2014 (1:14 pm)
I'm also a fan of the @= operator. Seems we are on the same page as far as TS syntax improvements go.

The editor work is slow, primarily since it's a spare hour project and the 3.0 release takes precedent. I have a hand full of modules that speed up things like asset creation. Once 3.0 is released, the full editor work can begin.

My e-mail is listed in my profile. You should know that I'm slow to respond, if at all, during normal working hours if it's not related to GG projects. If it's Torque related, then I probably can't get to it until night after my family goes to bed.
#10
03/02/2014 (2:22 pm)
James, I had no idea arrays were in your your branch. Take heart, though - the current SC are all huge fans of your work and we'll be working hard to get your changes pulled in.

Jeff: anonymous functions are awesome! Great work.
#11
03/02/2014 (3:27 pm)
Once, I get arrays fully support to meet my needs I'll set up a github branch where you guys can test it out and use it if you want too. This will include the lambda functions as well of course. :)

This does break some editor scripts atm too so I want to finalize it and make sure its working properly so then you guys can bugtest (if you wouldn't mind c:)

Also a warning is that this stuff could potentially break TS debuggers, but I do not intend on adding support for that. I don't think I've ever used a TS debugger in the past 3 years of doing heavy torque coding (mind you writing multiplayer for marble blast platinum, which I don't do anymore, which was a pain in the you-know-what especially when you can't fall back on the engine at all. Strict TS is a pain. Oh how ugly TS can get.) so sorry about that, but if someone else decides too why not i guess.
#12
03/02/2014 (3:31 pm)
Great work :D

We're gonna need a "Torquescript comformance test"
#13
03/02/2014 (3:32 pm)
Thanks Luis, its so little compared to your openGL work!
#14
03/02/2014 (4:38 pm)
I changed the name of the thread to TorqueScript Improvement Status as I think this reflects a better title. Considering I am talking about lamda functions, concat. strings, and arrays.
#15
03/02/2014 (8:01 pm)
I found a bug with isDefined on global variables:

$a[1] = 1;
isDefined("$a[1]"); // prints 0
isDefined("$a1"); // prints 1

Can someone confirm that this isn't just in my codebase, and is an actual bug with T3D's stock code. local vars work fine, so I doubt its my code changes to the array code.
#16
03/02/2014 (9:33 pm)
It's unfortunately not a bug, just how TS works. Writing %a[1] creates new variables %a1 and %a_1 (and were there some others? I can't remember). At least that's sort of how it works AFAIR.

Do you know the reason the editor scripts break? Are you changing the way the [] operator is interpreted for regular non-array variables?
#17
03/02/2014 (11:09 pm)
Quote:
I found a bug with isDefined on global variables:

$a[1] = 1;
isDefined("$a[1]"); // prints 0
isDefined("$a1"); // prints 1

Can someone confirm that this isn't just in my codebase, and is an actual bug with T3D's stock code. local vars work fine, so I doubt its my code changes to the array code.

Very weird. Getting 0 either way with isDefined("$option[0,0,0]"); and isDefined("$option000"); (hadn't tried with _ yet)

edit: By the way, for clarity, is that one intended to replace the interface for an arrayobject (more a vector, really), or provide a different option specifically bound to a given size? I can see a lot of confusion cropping up there...
#18
03/03/2014 (12:22 pm)
Quote:
Native arrays do NOT break current arrays in torquescript. In fact, to access elements of the new array, its the same way. You don't even have to have to use the new array declaration if you still wish to use torquescript style arrays. What this is for is eventually be able to just pass the array instead of passing each individual variable. It kinda makes a wrapper for it.

Posted this at the top too, does this help clear up confusion.

This means that if say you have this:

Both of these lines do the same now, and both DO work.
// new style:
%array[] = new array[2];

// old style
%array[0] = 0;
%array[1] = 0;

now say you wanted to pass those to functions. Both of the following things WILL work in the new codebase. This is what I have to implement next:
//new style:
calling_a_function(%array[]);

// old style:
calling_a_function(%array[0], %array[1]);

As you see, you will be able to add the [] to the end of %array when passing. That will let the interpreter know "hey I'm an array, pass all my values in the array stack". If you omit the [], it will just pass the count of the array, or what %array is. That will also automatically pass with the [] specified.

See how much neater this will make coding, say you had an array of 10. That's much less typing for the programmer, plus its handled internal which I do believe does give a slight performance boost (although not much, and i can't confirm this either.)

It does not break anything, unless you use .array on an object. You can't use reserved words as object fields, just try .new or .package or .function, it just don't work. I had to make changes where in script (mainly tools) there was like %this.array = new ArrayObject(); I had to change it to %this.arrayObj = new ArrayObject();

This does not ruin the functionality of arrayobjects either, as that is a dictionary object.

In regards to comparison to vectors, this is where it gets messy. The array[] does not allocate new memory by default. However, using native torquescript, you can technically allocate as much as you wanted too, but when passing to functions it would only pass up to the size of the array, unless of course you were to change that. You'll have to wait and see once I push the code changes to github, then it will make better sense, but yes it doesn't break any existing code functionallity besides using a reserved word for object properties. c:
#19
03/04/2014 (1:35 pm)
FINALLY, THE CODE IS HERE =D

link in first post, enjoy and please test and report all bugs to me!

Thanks guys, your awesome!
#20
03/04/2014 (6:16 pm)
arrays can now be itterated through for each loops!

Example code provided as always:

function arrayForTest() {
   %array[] = new array[3];
   %array[1] = 1;
   foreach (%i in %array[]) {
      echo(%i);
   }
}

That echos:

0
1
0

Go check it out on github!
Page «Previous 1 2