Home   Help Search Login Register  

Author Topic: Randomizing the location of an object from predefined locations  (Read 3071 times)

0 Members and 1 Guest are viewing this topic.

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
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.
« Last Edit: 03 Mar 2009, 05:51:23 by Ironman »
TS3 IP: tor.zebgames.com:9992

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Code: [Select]
_arraypos = (random (count _obj1positions)-1);
That code substract 1 from the random result, so you will have negative array positions.

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
so should it be:

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

because count starts at 1 instead of 0.... unlike an array
TS3 IP: tor.zebgames.com:9992

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
You should use floor(random(count _someArray)). Otherwise you get an uneven distribution due to 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.

« Last Edit: 21 Feb 2009, 11:51:22 by Worldeater »
try { return true; } finally { return false; }

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
_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;
« Last Edit: 21 Feb 2009, 17:54:46 by Ironman »
TS3 IP: tor.zebgames.com:9992

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Uhm, so AA1-AA6 are crates?
try { return true; } finally { return false; }

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
correct

New Error:

Code: [Select]
_spot1 setType _cache1;
_spot2 setType _cache1;>
  Error position: <setType _cache1;
_spot2 setType _cache1;>
  Error settype: Type Array, expected Location
« Last Edit: 21 Feb 2009, 18:29:32 by Ironman »
TS3 IP: tor.zebgames.com:9992

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Hmm, do all these creates contain the same gear?
try { return true; } finally { return false; }

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
no they are completely empty
TS3 IP: tor.zebgames.com:9992

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
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?
« Last Edit: 21 Feb 2009, 21:28:47 by Worldeater »
try { return true; } finally { return false; }

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
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.
TS3 IP: tor.zebgames.com:9992

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
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  ;)

try { return true; } finally { return false; }

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
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?
« Last Edit: 23 Feb 2009, 08:03:23 by Ironman »
TS3 IP: tor.zebgames.com:9992

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
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.
try { return true; } finally { return false; }

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
Is that really all it takes lol....
« Last Edit: 03 Mar 2009, 05:42:16 by Ironman »
TS3 IP: tor.zebgames.com:9992

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
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 ;
« Last Edit: 03 Mar 2009, 05:50:26 by Ironman »
TS3 IP: tor.zebgames.com:9992

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Oops... where did this "then" came" from?  :whistle:

Code: [Select]
if not isServer exitWith {};
try { return true; } finally { return false; }