ForEach command able to pass argument?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1180
Joined: Sun Feb 14, 2010 7:11 pm

ForEach command able to pass argument?

Post by Bugala »

I am trying to make a pathfinding which looks for the shortest distance to certain type of place, and it would be easiest to do it this way:

Code: Select all

function findshortestroad(index, value, distance)

distance = distance + 1
foreach(location.neighbors, findshortestroad(distance)

endfunction


distance = 0
foreach(location.neighbors, findshortestroad(distance))
This code is not complete of course, since first of all there needs to be added that it checks if location type is right and if location have already been checked (otherwise it would loop for ever) etc.

But just to give you the idea, that this way it would be easy to know the distance from current place. But is there any way to do this? For to my understanding using foreach command doesnt let you pass any arguments to the function to be executed?

And if not possible, then please add support for userdata or something to foreach commands to be executed functions.
User avatar
airsoftsoftwair
Posts: 5446
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: ForEach command able to pass argument?

Post by airsoftsoftwair »

It's not possible to pass user data currently, but you're right, it's a good idea. I'll consider implementing this.
Bugala
Posts: 1180
Joined: Sun Feb 14, 2010 7:11 pm

Re: ForEach command able to pass argument?

Post by Bugala »

Thanks, that would be great.

Basically you can do that using ListItems() and for-next instead, but it is much messier and also gives more chances for bugs, especially since you always have to remember that result of listitems number, is not same as last index number (to remember the -1) as well as not to forget that you still need to remember to check the index 0 as well.
User avatar
airsoftsoftwair
Posts: 5446
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: ForEach command able to pass argument?

Post by airsoftsoftwair »

Now implemented.
Post Reply