Jump to content
Sign in to follow this  
Bunny75

Random marker

Recommended Posts

How do I create marker name dynamically?

I have :

_rMarker= floor(random 3);

And then I want to create the last marker name from that _rMarker number to this:

"main_target" setMarkerPos getMarkerPos "main_target_pos_0";

So the marker would be something like "main_target_pos_" + _rMarker

Share this post


Link to post
Share on other sites

I use the following and seems to work well.

_markname = "Your_markername" + str (random 999);

Share this post


Link to post
Share on other sites

You might run into a case where you have duplicate marker names doing this though, unless you keep track of currently used names or something.

Share this post


Link to post
Share on other sites

_markerList = [];
_markerStr = "Your_markername";

while {done_markers < wanted markercount} do
{
   _markerNum = str (random 999);
   if !(_markerNum in _markerList) then
   {
       _markerName = markerStr + _markerNum;
       _markerList = _markerList + [_markerNum];
       done_markers = done_markers + 1;
   };
};

Quite crude and can probably be done better, but should work to not get doublets.

Share this post


Link to post
Share on other sites

If you hold the used numbers in a sorted array (or list, which would be more efficient but you'd have to implement the list yourself), then picking a random number that isn't already there is rather simple:

// assuming you defined MAX_NUMBER
// assuming array name is arrUsed

_newNumber = random (MAX_NUMBER - count arrUsed);
for "_i" from 0 to ((count arrUsed) - 1) do
{
  if (_newNumber >= arrUsed select _i) then
  {
     _newNumber = _newNumber + 1;
  };
};

// now you need to add the new number into the used numbers array, which is why a linked list would owrk better than an array.

Share this post


Link to post
Share on other sites

Not necessary to maintain an array if he was previously happy to randomize it, just append a hi-marker counter and increment it by one each time a new marker is created.

Share this post


Link to post
Share on other sites

Since you know the position where the marker should be, why not just use that info instead of a number/random number.

Idea:

_markerName = _markerName + str round((_pos select 0)) + str round((_pos select 1))

If we have:

_markerName = "myMarker"

_pos = [2384.423,1492.332,0]

the code should make it something like:

"myMarker23841492"

Share this post


Link to post
Share on other sites

I thought he actually cared about what the marker name is, if the marker name itself does not matter than you can just create it as "1", "2" etc, and have a global variable that counts which is next.

Share this post


Link to post
Share on other sites

I use this in my create marker function:

// create an unique number each time
if ( isNil "mk4_uniqueNumber" ) then { mk4_uniqueNumber = 0 };
mk4_uniqueNumber = mk4_uniqueNumber + 1;


// make a "unique" marker name
_markerName = format["mk4_MRK%1", mk4_uniqueNumber ];

Share this post


Link to post
Share on other sites

Thanks everyone!

I think the first one will suit best as it's the simpliest.

The thing is that I have multiple markers on map with a name main_target_pos_x (x is number) and I just have to pick one up randomly.

Sorry, I should've mentioned that from the beginning.

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  

×