Jump to content
Blitzen88

Need help with Random Position Script

Recommended Posts

Im trying to create a random position script for missions. The idea is the script will take an array of positions (placed objects) and, after a unit has been placed, that position will be removed from the array. 
 

Here is how it is called: ([Pos1, Pos2, Pos3], [Obj1, Obj2]) execvm Scripts/Misc/RandomPos.

 

Quote

_Positions = _this select 0;

 

_Objects = _this select 1;

 

_NumberofPositions = count _Positions;

 

_NumberofObjects = count _Objects;

 

if (_NumberofObjects > _NumberofPositions) then { exitwith "Say/notify player Random Pos Script: Items to be moved outnumbers available positions};

 

while (_NumberofObjects > 0) do {

 

                _RandomPosition = select random _Positions;

 

                _SelectedObject = _objects select 0;

 

                _SelectedObject setpos getpos _RandomPosition;

 

                _Positions = (_Positions - _RandomPosition);

 

                _Objects = (_Objects - _SelectedObject);

 

                _NumberofObjects = _NumberofObjects - 1;

 

                Sleep 0.5;

 

}

 

//End Loop


However, the script doesnt seem to recognize the array from the call/input...?

Share this post


Link to post
Share on other sites

I guess your script takes 2 arrays one with positions and one with objects. what exactly is the script supposed to do?

Share this post


Link to post
Share on other sites
3 minutes ago, sarogahtyp said:

I guess your script takes 2 arrays one with positions and one with objects.

Yes.

 

The idea is to use the script with hostage rescue style missions. Since I play single player and I play my own missions, I know where an objective (hostage, etc) is placed. The idea is to randomly place the hostage from a selection of available positions. 
 

Once a position is “used” then that position should be removed. This is to prevent a situation where multiple hostages could be placed at the same location. 

Share this post


Link to post
Share on other sites

you do not place any object anywhere in your script therefore I dont know what you are trying... Describe EXACTLY where do you want to call the script and what it has to do. What is that objects array and what is that positions array.

Share this post


Link to post
Share on other sites
1 minute ago, sarogahtyp said:

you do not place any object anywhere in your script therefore I dont know what you are trying... Describe EXACTLY where do you want to call the script and what it has to do. What is that objects array and what is that positions array.


([Pos1, Pos2, Pos3], [Obj1, Obj2]) execvm ....

 

First array (“Pos1”) would be available positions. These would be named objects.

 

Second array (“Obj1”) are objectives (hostages, etc) that would be placed via the script. These would be named units, etc. 

 

 _SelectedObject setpos getpos _RandomPosition;

 

Thought this would move the objective to the position...?

 

Share this post


Link to post
Share on other sites
Posted (edited)

Not tested:

params [ "_Positions", "_Objects" ];

private _NumberofPositions = count _Positions;

private _NumberofObjects = count _Objects;

if (_NumberofObjects > _NumberofPositions) exitWith { systemChat "Random Pos Script: Items to be moved outnumbers available positions"; };

private ["_RandomPosition"];

private _dummy =
{
 _RandomPosition = selectRandom _Positions;
 
 _x setpos ( position _RandomPosition );
 
 _Positions = _Positions - [_RandomPosition];
 
} count _Objects;

 

execute it with:

[ [ Pos1, Pos2, Pos3 ], [ Obj1, Obj2 ] ] execVM "YourScriptPathAndName";

 

Edited by sarogahtyp

Share this post


Link to post
Share on other sites

Whats the _dummy for?

 

Also, how does it loop through all of the objects?

Share this post


Link to post
Share on other sites

count loops through the objects and _x is the current element.

 

_dummy is not needed here but its a habit of me. Count returns a value always and the dummy just stores it to prevent to have it as return value of the script. I always store not needed return values in a dummy because sometimes you have not wanted behavior if u dont do that.

But at your scripting skill level u dont need to care bout the dummy.

 

You should always read the biki entries for each command u use. This is the fastest way to learn scripting...

  • Like 1

Share this post


Link to post
Share on other sites
16 minutes ago, sarogahtyp said:

count loops through the objects and _x is the current element.

Okay, thought count just returned a numerical value. Didnt know it could be used as a loop

Share this post


Link to post
Share on other sites

You want something like this: ???

 

description.ext (to load the file into a function)

class CfgFunctions {
  class JF {
    class Scripts {
      class RandomPos {file = "RandomPos.sqf"};
    };
  };
};

 

RandomPos.sqf

params ["_positions", "_units"];

if ((count _positions) == 0 || (count _units) == 0) exitWith {hint "ERROR"};

private _pos = selectRandom _positions;
private _unit = selectRandom _units;

_positions deleteAt (_positions find _pos);
_units deleteAt (_units find _unit);

private _hostage = (createGroup civilian) createUnit [_unit, _pos, [], 0, "FORM"];


[_positions, _units, _hostage]

 

the file you call the function from

private _positions = [[3105.73,6287.15,0], [3069.41,6329.24,0], [3123.77,6294.1,0]];
private _units = ["C_Man_casual_4_v2_F", "C_man_polo_5_F"];

private _handle = [_positions, _units] call JF_fnc_RandomPos; //returns array: [_positions, _units, _hostage]

//now you could work with _hostage and assign a task

 

  • Like 1

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

×