Game Development Community

Foreach not working...

by Anders Jacobsen · in Torque Game Engine · 06/21/2005 (8:24 pm) · 11 replies

This one bugs up on me, how should I use foreach correctly?


function setChannels(%channels)
{
	foreach(%str in %channels)
	{
		joinChannel(%str);
	}
}


Am I missing something?

#1
06/21/2005 (11:05 pm)
Yes. That isn't valid TorqueScript. There is no 'foreach'
#2
06/22/2005 (3:54 am)
Torquescript is based on c++ ... You will need a c++ style for loop. I'm assuming you want something like this:

function setChannels(%channels, %numChannels)
{
   for (%c = 0; %c < %numChannels; %c++)
   {
      joinChannel(%channels[%c]);
   }
}
#3
06/22/2005 (11:56 am)
Ooops. Ok, guess I'll use the normal for loop then :)
#4
06/22/2005 (4:46 pm)
Also note that the above wouldn't work as you think it would, because torque arrays aren't. Search the forums, there's many discussions about that already.

Gary (-;
#5
06/22/2005 (5:57 pm)
Well... I don't want to send each channel as an individual string from server to client...
This is what I changed it to:

function setChannels(%channels)
{
	%count = sizeof(%channels);
	for(%x = 0; %x < %count; %x++)
	{
		joinChannel(%channels[%x]);
	}
}

I haven't yet tested this tho since I'm working on other code right now, but you're saying this won't work either?
#6
06/22/2005 (6:27 pm)
Gary - My code would work. Wouldn't it ? Well... assuming joinChannel works.
#7
06/22/2005 (6:32 pm)
Yeah no worries, joinChannel works, and your code should work too, been testing against a python based server.
I still want to compute how many channels there are on the client tho, rather than having to have the server tell the client how many there are.
So my version is a more optimized way of doing it. Just hope the torque script has sizeof() atleast ;) If not it should be easy enough to add.
#8
06/22/2005 (8:34 pm)
Quote:So my version is a more optimized way of doing it. Just hope the torque script has sizeof() atleast ;) If not it should be easy enough to add.

nope it doesnt.
#9
06/22/2005 (10:48 pm)
So what would be the preffered way to retrieve the number of indices in an array in torque script then?

Extending torque script language won't be on the top of my priority list right now, but if I'm forced I'm forced...
#10
06/22/2005 (10:53 pm)
Hmm looks like a better bet would be using the GetField and GetFieldCount functions and just use a space delimited string of channels...

But it really would be useful to have explode/implode/sizeof/foreach functionality for the torque arrays. Anyone know if this is planned in later versions?
#11
07/20/2005 (8:33 am)
Torquescript and arrays... I had such a nightmare with this.

That was, until I discovered the torque arrayobject resource.

(Note, this is for TGE license owners only.)

If you haven't got the engine source, the best route to take is to use a ScriptGroup to handle dynamically-sized lists of objects in TorqueScript.

There aren't any examples of ScriptGroups in the stock TGE demos, but fortunately they work exactly like SimGroups:

$mylist = new ScriptGroup(SomeList);

SomeList.add(%somevar);
SomeList.add(%othervar);

echo("This list has" SPC SomeList.getCount() SPC "members");

for(%i = 0; %i < SomeList.getCount(); %i++)
{
    echo(SomeList.getObject(%i));
}

SomeList.delete();