Jump to content
Sign in to follow this  
tryteyker

Running into problems with for loop

Recommended Posts

Hi, basic question here. Seems like I forgot most of the stuff already since I took quite a break from scripting. :(

Anyway, onto my question. I have a for loop, which is supposed to run through all towns and villages found via nearestLocations and place a marker. So far so good, it works, most of it anyway.

Here's the script:

_villages = nearestLocations [getmarkerpos "center",["NameVillage","NameCity","NameCityCapital"], 50000];

for "_i" from 0 to count _villages do {
sleep 0.5;
_town = _villages select _i;
_mkr = createMarker ["asdf",getpos _town];
_mkr setmarkershape "ICON";
_mkr setmarkertype "Destroy";
};

The problem is that the script uses _mkr over and over and does not seem to renew variables once executing the for loop. That means it wont return an error, but won't place any more markers except in the first town. I got no idea on how to tell it to refresh the variables though.

(the whole thing probably has to be wrapped in a foreach loop)

Share this post


Link to post
Share on other sites

maybe something like this?

[color=#000000][color=#0000BB]_mkr [/color][color=#007700]= [/color][color=#0000BB]createMarker [/color][color=#007700]Format[[/color][color=#DD0000]"%1"[/color][color=#007700],random 1000[/color][color=#007700]];[/color][/color]

Share this post


Link to post
Share on other sites

try this:

_markerName = format ["asdf%1",_i];

_mkr = createMarker [_markerName, getPos _town];

Share this post


Link to post
Share on other sites

Thanks for the help, fixed it. I'm still running into problems later on though, it seems like he's not getting anything else from _villages but still executes the for loop. Any ideas why? I'm specifically getting an error that he's not passing anything to createMarker.

Share this post


Link to post
Share on other sites

Try this:

for "_i" from 0 to count _villages do {

to

for "_i" from 0 to count _villages -1 do {

Share this post


Link to post
Share on other sites

Thanks. Still running into issues though, this time with adding stuff to an array.

So what I'm doing right now is this:

for "_i" from 0 to count _villages - 1 do {
sleep 0.1;
_town = _villages select _i;
_mkrname = format ["%1",_i];
_mkr = createMarker [_mkrname,getpos _town];
towns = [] + [_mkr];
hintsilent format ["Markers: %1",towns];
};

Adding _mkr to an empty array. It's not exactly adding stuff together, more like going upwards.

Expected behavior is this:

["1","2","3"] which are the markernames

Current behavior is this:

["1"]

["2"]

etc, so towns first equals ["1"], then ["2"] and so on. I guess this is because _mkr is always newly defined but I don't see another way of doing this atm, so I need another pair of eyes to look at it.

Share this post


Link to post
Share on other sites

I would define your array as such:

_markers = [];

then in loop

_markers = _markers + [_mkr];

hintsilent format ["Markers: %1",_markers];

Share this post


Link to post
Share on other sites

I tried that but it makes essentially no difference.

Share this post


Link to post
Share on other sites

That doesn't work if towns is not pre-defined apparently, and pre-defining it with panthers method is the same as the one I'm using currently, so it doesn't work that way either.

It returns "markers: any" when using towns + [_mkr].

Share this post


Link to post
Share on other sites

Try to manually setPos the marker once created, I run into that problem once and for some odd reasons my objects got piled on the first position.

Share this post


Link to post
Share on other sites

Okay you wanna make an array of markers right?

Try:

towns = [];

for "_i" from 0 to count _villages - 1 do {

sleep 0.1;

_town = _villages select _i;

_mkrname = format ["%1",_i];

_mkr = createMarker [_mkrname,getpos _town];

towns = towns + [_mkr];

hintsilent format ["Markers: %1",towns select _i];

sleep 1;

};

sleep 2;

hintSilent format["Number of markers in array: %1",count towns];

Share this post


Link to post
Share on other sites

Alright your solution worked JW, but no idea why. I tried using select at first (in the outer hintsilent) but it just returned null. Thanks though.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×