Author Topic: Why doesn't Blockland have foreach?  (Read 2482 times)

/title, it seems extremely useful to have integrated into the modded engine that's for blockland, instead of doing some hacky stuff like
Code: [Select]
for(%i=0;%i<ClientGroup.getCount();%i++){stuff here} we could just do
Code: [Select]
foreach(%client in ClientGroup){stuff here}thoughts?

(yes i did take that example from the torquescript wiki)

a for loop going through the client group doesn't seem hacky to me...or it's because i'm used to it maybe
« Last Edit: May 21, 2016, 09:23:34 PM by QuadStorm »

a for loop going through the client group doesn't seem hacky to me...
well it really isn't hacky tbh, it's just a bit of a burden to keep using for loops.
we also do have while loops but you know

edit: while loops are easier to read, but sorta longer to use
Code: [Select]
%f=ClientGroup.getCount();while(%f >= 0){stuff here %f--;}
« Last Edit: May 21, 2016, 09:34:09 PM by Metario12 »

Probably because C/C++ doesn't have it and that's what the engine was programmed in.
It's not like it's a huge inconvenience or anything, and it'd probably be a huge pain to implement it into the TS VM at this point.

It wasn't embedded into the engine all those years ago like the newer Torque3D does

Why? It's only a few more characters of codes. And for loops for more customizable.

It's also not a good idea to use .getCount() within the for statement. That'll call a function each time the for loops. Best to do something like %count = .getCount prior to starting the for loop then using <%count

Yes, it's only a very minor slowdown. But if enough add-ons continue to do this, it'll eventually become noticeable when they're all constantly doing it.



Probably because C/C++ doesn't have it and that's what the engine was programmed in.
False.
C specifically has foreach
C++ can do a foreach within a for: for (int i : myint), as well as being able to do it normally in 'newer' editors/compilers.
Not that the language the engine was written in could stop it from existing in TorqueScript anyway. It just doesn't exist because.



But we really don't need it. Not that it'd work as expected for a lot of things anyway.

Torque 3D has both foreach (%obj in RootGroup) and foreach$ (%word in "a b c d").

C specifically has foreach
Are you thinking of C#?
All of the C standards I know of do not have foreach

C++ can do a foreach within a for: for (int i : myint), as well as being able to do it normally in 'newer' editors/compilers.
That was added in C++11. A long time after TGE was made and therefore not really applicable.

Not that the language the engine was written in could stop it from existing in TorqueScript anyway. It just doesn't exist because.
It's just less likely for programmers to add new control statements if their main language doesn't have them.

Torque 3D has both foreach (%obj in RootGroup) and foreach$ (%word in "a b c d").
Too bad those are both specific. Better than nothing I suppose.

Too bad those are both specific. Better than nothing I suppose.
there really isn't much else you could use it for in TS.

there really isn't much else you could use it for in TS.
Well, a foreach for numerical arrays would be nice.



They aren't actual arrays though.
How an actual array would work:
$Variable = [0,5,3];
This would create $Variable as a pointer to an array, and set $Variable.length to 3, $Variable[0] to 0, etc.
$Variable[1] = 6; would then change it to [0,6,3]. Without the array existing, it would create $Variable as a pointer to the array, and set the value(s) accordingly.
This doesn't happen in TS.
When you do $Variable[1] = 6; $Variable is still null. There is no determinable length to it. All you're doing is actually setting $Variable1 = 6;
Example:
%x = 1;
%y = 2;
$Variable[%x,%y] = 1;
This is just doing
$Variable1_2 = 1;

There is no way to tell the engine how many things are in the 'array' when you use a foreach, in TorqueScript.
« Last Edit: May 24, 2016, 02:21:45 PM by Shift Kitty »