Jump to content

Random pos in trigger area

Recommended Posts

How should I go about getting a random position within a trigger area (circle - 500m) to spawn units on?  I have the following so far:

_random_pos_in_trigger = trigger_spawn call BIS_fnc_randomPosTrigger;

Won't this pick a random position that isn't necessary safe for a unit spawn (e.g., on a rock, in the water, etc.)?  Is there a way to use BIS_fnc_findSafePos with this to make sure I'm not spawning units in unsafe locations?

Many thanks.

Share this post

Share on other sites

I'm not familiar with that particular function, however there is another way you could get a random position within a circle.

As long as the trigger is a perfect circle, you could do something like this:

_random_pos_in_trigger = (trigger getRelPos [(random ((triggerArea trigger) select 0)), random(360)]);

I just tested this and it works without issue. Let me know if you need something a bit more complex for different shapes.

EDIT: I figured I would show how this works:

trigger is the name of your trigger, obviously. getRelPos is script command that gets a position relative to the location of an object. So trigger is our object, and then the array we pass after the getRelPos command is [distance, direction]. So if you put in trigger getRelPos [90,180], that would return a pos that is 90m to the direct south of the object. So, we want the distance and direction to be random. The distance needs to not exceed the radius of the trigger. We can get the radius of the trigger by using this: ((triggerArea trigger) select 0). triggerArea returns this array: [a, b, angle, isRectangle, c] . Since we have a perfect circle, selecting the first element of the array will work as it is the same as the second, so we use select 0 (arrays elements are like so [0,1,2,3.....,15,16]). We then add random in front of that, as this will return a random whole number from 0 to the stated value, which in our case would be the radius of the trigger. Next, we need a random direction. Since headings in Arma are 0-360, we simply need a random number from 0 to 360. Tada!

• 1

Share this post

Share on other sites

To get a uniform distribution inside an area use this:

` _random_pos_in_trigger = (trigger getRelPos [((triggerArea trigger) select 0) * sqrt random 1, random(360)]); `

Cheers

• 2

Share this post

Share on other sites

Will either of these methods return a SAFE position to spawn units on?

Share this post

Share on other sites

No they will not. Better of using BIS_fnc_findSafePos that you linked to. If you need to spawn vehicles, then after finding a position then use https://community.bistudio.com/wiki/findEmptyPosition afterwards to ensure it is safe for vehicles.

Share this post

Share on other sites

What about the BIS_fnc_randomPos and BIS_fnc_randomPosTrigger? (randomPos is more versatile but i guess randomPosTrigger is faster and sufficient for your purpose)

EDIT: this wasn't helpful see below

Edited by 7erra

Share this post

Share on other sites

He needs to spawn units/vehicles so he needs somewhere safe.  Those two functions just pick somewhere within the area, not necessarily somewhere safe.

Share this post

Share on other sites

I don't know what was wrong with me it seems i didn't read the question at all. Here is a script with the suggestions from above:

```_random_pos_in_trigger = [];
_loopCount = 0;
while {_random_pos_in_trigger isEqualTo [] && _loopCount < 100} do {
_random_pos_in_trigger = trigger_spawn call BIS_fnc_randomPosTrigger;
_random_pos_in_trigger = _random_pos_in_trigger findEmptyPosition [0, 0, "vehicleType"];
_loopCount = _loopCount +1; //Prevents infinite executions with severe hit on performance if no position can be found
};```

_random_pos_in_trigger should be safe to spawn vehicles on afterwards.

Share this post

Share on other sites
On 8/21/2017 at 8:54 PM, 7erra said:

I don't know what was wrong with me it seems i didn't read the question at all. Here is a script with the suggestions from above:

```
_random_pos_in_trigger = [];
_loopCount = 0;
while {_random_pos_in_trigger isEqualTo [] && _loopCount < 100} do {
_random_pos_in_trigger = trigger_spawn call BIS_fnc_randomPosTrigger;
_random_pos_in_trigger = _random_pos_in_trigger findEmptyPosition [0, 0, "vehicleType"];
_loopCount = _loopCount +1; //Prevents infinite executions with severe hit on performance if no position can be found
};```

_random_pos_in_trigger should be safe to spawn vehicles on afterwards.

Thanks!  Finally got it to work with a solution I came up with (at work so I don't have the script on me).  But I like yours a lot better so I think I'm going to use it instead since it's a lot less complicated- thanks!

Another problem now:

I'm trying to select a random trigger to be "trigger_spawn", so I have three triggers named "trigger_spawn1", "trigger_spawn2", "trigger_spawn3" in an array and I select one from random and pass it to a variable.  However, when I execute the BIS_fnc_randomPosTrigger on this variable, it will only select the center of the trigger every time.  When I use the actual name of the trigger instead of the variable I created with the trigger name, it works.

Works:

_random_pos_in_trigger = trigger_spawn call  BIS_fnc_randomPosTrigger; // Just like in your solution; returns random location within trigger area

Doesn't Work:

_select_trigger_spawn = _trigger_spawn_array call BIS_fnc_selectRandom;

_random_pos_in_trigger = select_trigger_spawn call  BIS_fnc_randomPosTrigger; // Just returns the center of the trigger area

In order to try to isolate the issue I used triggerArea on my variable that has the randomly picked trigger from the array, and received the "string, expected object" error.  Now I'm stuck.  Any idea how to make this work?  Again, trying to select trigger from array, and having that serve as the trigger to execute BIS_fnc_randomPosTrigger on.

_area_of_trigger = triggerArea select_trigger_spawn; // ERROR

Share this post

Share on other sites
33 minutes ago, zigzagtshirt said:

In order to try to isolate the issue I used triggerArea on my variable that has the randomly picked trigger from the array, and received the "string, expected object" error.  Now I'm stuck.  Any idea how to make this work?  Again, trying to select trigger from array, and having that serve as the trigger to execute BIS_fnc_randomPosTrigger on.

Hard to tell without seeing the rest of your script.

Cheers

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

×

• Clubs