OFPEC Forum

Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: Ironman on 21 Feb 2009, 04:39:12

Title: Randomizing the location of an object from predefined locations
Post by: Ironman on 21 Feb 2009, 04:39:12
obj1.sqf:
Code: [Select]
_AA1 = getPos AA1;
_AA2 = getPos AA2;
_AA3 = getPos AA3;
_AA4 = getPos AA4;
_AA5 = getPos AA5;
_AA6 = getPos AA6;

_cache1 = typeOf AA1;

_obj1positions = [_AA1, _AA2, _AA3, _AA4, _AA5, _AA6];

deleteVehicle AA1;
deleteVehicle AA2;
deleteVehicle AA3;
deleteVehicle AA4;
deleteVehicle AA5;
deleteVehicle AA6;

_arraypos = (random (count _obj1positions)-1);
_spot1 = _obj1positions select _arraypos;
_obj1positions = _obj1positions - (_obj1positions select _arraypos);
_arraypos = (random (count _obj1positions)-1);
_spot2 = _obj1positions select _arraypos;

_spot1 = _cache1;
_spot2 =_cache1;

Error:
Code: [Select]
Error in expression <1positions)-1);
_spot2 = _obj1positions select _arraypos;

_spot1 = _cache1;
_sp>
  Error position: <select _arraypos;

_spot1 = _cache1;
_sp>
  Error Zero divisor

I want there to be 2 crates that are on the map out of these 6 possible locations. The trick is that the second designated location can never be the same as the first designated location. Otherwise their will be only one crate. Also, I do not know how to define the "typeof" once the random generated location has been picked.
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Mandoble on 21 Feb 2009, 07:56:22
Code: [Select]
_arraypos = (random (count _obj1positions)-1);
That code substract 1 from the random result, so you will have negative array positions.
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Ironman on 21 Feb 2009, 08:10:39
so should it be:

_arraypos = (random(count _obj1positions -1));

because count starts at 1 instead of 0.... unlike an array
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Worldeater on 21 Feb 2009, 11:35:09
You should use floor(random(count _someArray)). Otherwise you get an uneven distribution due to index rounding (http://community.bistudio.com/wiki/Array#Index_rounding) (the first element is less likely to be choosen).


Another thing is the following line which will generate an error:
Code: [Select]
_obj1positions = _obj1positions - (_obj1positions select _arraypos);
The correct syntax is:
Code: [Select]
_obj1positions = _obj1positions - [_obj1positions select _arraypos];
Keep in mind that this removes all matching elements from the array:
Code: [Select]
arr = [0,1,1,2];
arr = arr - [arr select 1];
// arr is [0,2]


Edit:

Could you explain why you want to use typeOf? It is also unclear to me why you are overwriting _spot1 and _spot2 at the end of script.

Title: Re: Randomizing the location of an object from predefined locations.
Post by: Ironman on 21 Feb 2009, 17:04:17
_AA1-_AA6 only hold the positions of the objects. Not the actual objects. So, I thought I had to set the _spot one to a type of object.

*edit new code*
Code: [Select]
_AA1 = getPos AA1;
_AA2 = getPos AA2;
_AA3 = getPos AA3;
_AA4 = getPos AA4;
_AA5 = getPos AA5;
_AA6 = getPos AA6;

_cache1 = typeOf AA1;

_obj1positions = [_AA1, _AA2, _AA3, _AA4, _AA5, _AA6];

deleteVehicle AA1;
deleteVehicle AA2;
deleteVehicle AA3;
deleteVehicle AA4;
deleteVehicle AA5;
deleteVehicle AA6;

_arraypos = floor(random(count _obj1positions));
_spot1 = _obj1positions select _arraypos;
_obj1positions = _obj1positions - [_obj1positions select _arraypos];
_arraypos = floor(random(count _obj1positions));
_spot2 = _obj1positions select _arraypos;

_spot1 setType _cache1;
_spot2 setType _cache1;
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Worldeater on 21 Feb 2009, 18:19:37
Uhm, so AA1-AA6 are crates?
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Ironman on 21 Feb 2009, 18:21:52
correct

New Error:

Code: [Select]
_spot1 setType _cache1;
_spot2 setType _cache1;>
  Error position: <setType _cache1;
_spot2 setType _cache1;>
  Error settype: Type Array, expected Location
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Worldeater on 21 Feb 2009, 18:33:06
Hmm, do all these creates contain the same gear?
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Ironman on 21 Feb 2009, 19:12:15
no they are completely empty
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Worldeater on 21 Feb 2009, 21:04:40
Hmmm, so you have six crates, determine the positions of two of them and then delete them all? Why don't you keep two of the crates?
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Ironman on 21 Feb 2009, 21:21:26
There are 6 crates and I store there Positions in _AA1-_AA6. Then I store the "type" of one crate. Then they are all deleted. Then the script generates two locations out of the six to put the ammo crates. If you think your way is easier please share.
Title: Re: Randomizing the location of an object from predefined locations.
Post by: Worldeater on 21 Feb 2009, 21:29:39
typeOf and setType don't do what you think they do. You would have to create new crates with "createVehicle".

To keep two of the crates try this:

Code: [Select]
_crateList = [AA1,AA2,AA3,AA4,AA5,AA6];

// select two random crates and remove them from the list...

_rnd = floor(random(count _crateList));
_crate = _crateList select _rnd;
_crateList = _crateList - [_crate];

_rnd = floor(random(count _crateList));
_crate = _crateList select _rnd;
_crateList = _crateList - [_crate];


// ...and delete the rest
{ deleteVehicle _x } forEach _crateList;

"Beware of bugs in the above code; I have only proved it correct, not tried it." -- D. Knuth  ;)

Title: Re: Randomizing the location of an object from predefined locations.
Post by: Ironman on 21 Feb 2009, 21:33:47
well it is not throwing an error I see if it is doing what I want lol..

*edit*
It is working on my local server... lets see what happens on my dedi. Thanks for the help.

**edit**
Ok it doesn't throw any errors when I run it on my dedi but no crates show up in mission. Any thoughts?
Title: Re: Randomizing the location of an object from predefined locations (Updated 2/24)
Post by: Worldeater on 25 Feb 2009, 12:07:23
This is most probably because the script is run on all computers (server + clients). Since the random numbers are not "synchronized" among them chances are that all crates get deleted.

If this is really the case then adding a...

Code: [Select]
if not isServer then exitWith {};
...at the top of the script should make it work.
Title: Re: Randomizing the location of an object from predefined locations
Post by: Ironman on 25 Feb 2009, 22:50:14
Is that really all it takes lol....
Title: Re: Randomizing the location of an object from predefined locations
Post by: Ironman on 03 Mar 2009, 05:42:36
It decided to not work because it claims there is a missing semi-colon.

obj1_1.sqf:
Code: [Select]
if not isServer then exitWith {};

_crateList = [AA1,AA2,AA3,AA4,AA5,AA6];

// select two random crates and remove the others from the list...

_rnd = floor(random(count _crateList));
_crate = _crateList select _rnd;
_crateList = _crateList - [_crate];

_rnd = floor(random(count _crateList));
_crate = _crateList select _rnd;
_crateList = _crateList - [_crate];


// ...and delete the rest
{ deleteVehicle _x } forEach _crateList;

error msg:
Code: [Select]
if not isServer then exitWith {};

_crateList = [AA1,AA2,AA3,AA4,AA5,A>
  Error position: <{};

_crateList = [AA1,AA2,AA3,AA4,AA5,A>
  Error Missing ;
Title: Re: Randomizing the location of an object from predefined locations
Post by: Worldeater on 03 Mar 2009, 06:35:09
Oops... where did this "then" came" from?  :whistle:

Code: [Select]
if not isServer exitWith {};