Jump to content
  • 0

Randomize Object Start Location


Stollenwerk

Question

I guess you all know Origins, Sector B and the Ural(s) with loot as reward spawning at different locations.

I created a little test mission and got stuck now to spawn the Ural at different predefined positions.

 

I guess this could work with multiple markers to act as starting points, but I couldn't set it up correctly.

Tried something else (check below), but I must confess that my knowledge of scripting is much to low...

Of course it didn't work... :rolleyes:

if(isServer) then {

private ["_posArray","_randPos","_pos","_object"];

_posArray = [

        [22680.2,19930.2,0.001],     
        [22580.2,19830.2,0.001],
        [22480.2,19730.2,0.001],
    ];

    _randPos = _posArray call bis_fnc_selectrandom;  
        _pos = _randPos select 0;                                     
    
        
_vehicle_2000 = objNull;
    if (true) then
    {
    _object = createVehicle ["Ural_TK_CIV_EP1", [_pos], [], 0, "CAN_COLLIDE"];
    _vehicle_2000 = _object;
    _object setposatl [_pos];
    };
};
Link to comment
Share on other sites

18 answers to this question

Recommended Posts

  • 0

Alright, sorry for that. I'm not sure, if you random array actually works or not, but the way i posted above is faster, looks better and is more easy..

_object setposatl [_pos];

is Wrong, because

 

_pos is an Array already.

Your code would show:
 

_object setposatl [[123,456,789]];

And therefore no position will be set, as invalid arguments are passed.

Link to comment
Share on other sites

  • 0

Firstly, when you define _posArray you are leaving a comma after the final element. Leaving a comma in an array lets the compiler know that it should expect more elements to come. This is how it should look:

_posArray = [

        [22680.2,19930.2,0.001],     

        [22580.2,19830.2,0.001],

        [22480.2,19730.2,0.001]      // Removed this final comma.

    ];

Next, I'm gonna explain the story of this bit of code:

_randPos = _posArray call bis_fnc_selectrandom;  
    _pos = _randPos select 0;   

So, _posArray holds 3 elements (all are arrays) and we set _randPos to be equal to any of the 3 elements in _posArray.
Thats this line,

_randPos = _posArray call BIS_fnc_selectRandom;

Now, let's say we know which element BIS_fnc_selectRandom gave to _randPos.
_randPos is now equal to [22680.2, 19930.2, 0.001], which is perfect because we need 3 dimensions.

 

Next, we set _pos to be equal to the first element in _randPos with this line:

_pos = _randPos select 0;

So, _pos now equals 22680.2.

 

Now, when we go to create the vehicle we are giving createVehicle a position to spawn the vehicle at, [_pos].

_object = createVehicle ["Ural_TK_CIV_EP1", [_pos], [], 0, "CAN_COLLIDE"];

Because you enclosed _pos in brackets createVehicle will read it as though it is an array.

 

Because this is a 3D game and _pos is only holding 1 dimension out of 3, how is ArmA going to know where to put the vehicle?

Can you see where you went wrong yet?

Link to comment
Share on other sites

  • 0

Thanks for the detailed explanation, it enlightened me much after all.

The leaving comma I knew of before, but didn't recognize deep in the night. :ph34r:

 

So, I'm still playing around and not sure if this would be right:

 

if(isServer) then {

private ["_posArray","_randPos","_posx","_posy","_posz","_object"];

_posArray = [

        [22680.2,19930.2,0.001],     
        [22580.2,19830.2,0.001],  
        [22480.2,19730.2,0.001]
    ];

    _randPos = _posArray call bis_fnc_selectrandom;  
    _posx = _randPos select 0;                                     
    _posy = _randPos select 1;    
    _posz = _randPos select 2;
        
_vehicle_2000 = objNull;
    if (true) then
    {
    _object = createVehicle ["Ural_TK_CIV_EP1", _posx,_posy,_posz [], 0, "CAN_COLLIDE"];
    _vehicle_2000 = _object;
    _object setposatl _posx;
    _object setposatl _posy;
    _object setposatl _posz;
    };
};

 

It seems simple but still confuses me :blink:

Link to comment
Share on other sites

  • 0

Alright, you've made some progression which is good, but there's more to do.

 

See this.

_object = createVehicle ["Ural_TK_CIV_EP1", _posx,_posy,_posz [], 0, "CAN_COLLIDE"];

createVehicle when used in this way expects 5 arguments, the second of which should be a position. Positions are represented as an array with 3 elements, each single element being the distance along one axis.

[x-coordinate, y-coordinate, z-coordinate]

Before I go on I'm going to make a quick fix to your line of code I showed aboved to make explaining easier.

You forgot to put a comma after _posz.

_object = createVehicle ["Ural_TK_CIV_EP1", _posx,_posy,_posz, [], 0, "CAN_COLLIDE"];

Now, with that comma put in place can you see an issue yet?

Exactly, you've supplied 7 arguments instead of 5.

You've also passed in the X, Y and Z coordinates as though they are seperate from each other, when in fact they should be integral to each other.

Fixing this code is a very simple matter though, you only have to add 2 brackets. Like so,

_object = createVehicle ["Ural_TK_CIV_EP1", [_posx,_posy,_posz], [], 0, "CAN_COLLIDE"];

Now we have 5 elements which is perfect and the position is now an array with 3 elements, 1 for each dimension.

 

The last error here is this

_object setposatl _posx;
_object setposatl _posy;
_object setposatl _posz;

_posx, _posy and _posz each represent 1 axis out of 3 for a 3 dimensional world.

When these values are not put together in an array, what are they? Just numbers, that's it.

Let's use this element of _posArray to demonstrate, [22480.2, 19730.2, 0.001].

So _posx has a value of 22480.2, _posy has a value of 19730.2 and _posz has a value of 0.001.

So when you run this line for example,

_object setposatl _posx;

you're basically saying "Put the vehicle at 22480.2 please.".

If I told you to walk to 22480.2, would you have any idea where I wanted you to go?

Imagine asking a friend where they are and getting the reply "Oh, I'm at 12 and a half.". Doesn't make much sense right?

 

So, remember that a position is defined as an array with 3 elements, 1 for each axis. With that, go ahead and fix that last bit.

Let me know when you've fixed it and I'll show you how you can remove about 3/4 of that code, which will make it easier to understand.

 

Also, check this place out. It has good documentation and examples for almost all functions available in ArmA.

Link to comment
Share on other sites

  • 0

wooow...this looks quite complecated ^^

 

here a script where i spawned a blacktrader on differend position on each restart:

if (true) then
{
_posArray =     [ 
        [[10458.7,2244.95,0.6455],15], //Elektro
        [[11501.2,11295.9,0],289], //Klen
        [[11256.2,12219.5,0],235], //Berezino
        [[6853.89,2505.77,0.7546],210], //Cherno
        [[3998.56,11656.7,0],90], //Bash
        [[2591.53,5062.47,0],270], //Zele
        [[1599.43,7785.17,0],90], //Mogilevka
        [[7061.92,7735.04,0.673],130], //Novy
        [[11331.9,6655.79,0.33],130] //Dolina
        ]; 
_unit_17 = _this;
_randPos = _posArray call bis_fnc_selectrandom;   
_pos = _randPos select 0;                                      
_dir = _randPos select 1; 
_this = createAgent ["GUE_Woodlander2", [1599.43,7785.17,0], [], 0, "CAN_COLLIDE"];
_this setVehicleInit "this allowDammage false; this disableAI 'FSM'; this disableAI 'MOVE'; this disableAI 'AUTOTARGET'; this disableAI 'TARGET'; this setBehaviour 'CARELESS'; this forceSpeed 0;this switchMove 'CtsDoktor_Vojak_hulakani1';";
_this setposatl _pos;
_this setdir _dir;
_this setUnitPos "up";



_this allowDammage false; _this disableAI 'FSM'; _this disableAI 'MOVE'; _this disableAI 'AUTOTARGET'; _this disableAI 'TARGET'; _this setBehaviour 'CARELESS'; _this forceSpeed 0;_this enableSimulation false;
};

This looks alot easier to me then the whole process with setting each position ;)

 

Also it does not matter where you create the object first (except on [0,0,0]) you put the object after the creation with setPos on its right position

 

The first is the position and the second value is the heading direction:

[[10458.7,2244.95,0.6455],15], //Elektro

[[Position X ,Position Y ,Position Z],Direction], //Elektro

 

Link to comment
Share on other sites

  • 0

Thanks again, you explain like a teacher, very helpful Sir! :)

I looked often at Bohemia's site in the past about scripting commands, but I never understood all these things about strings and arrays less then ever put all the code together in the right way.

 

So I guess it should be:

_object setposatl [_posx,_posy,_posz];

Also thx for you example SchwEde. We are doing this complicated way only to let me understand all these lines.

I guess that I almost understand your code by now.

Link to comment
Share on other sites

  • 0

Thanks for the detailed explanation, it enlightened me much after all.

The leaving comma I knew of before, but didn't recognize deep in the night. :ph34r:

 

So, I'm still playing around and not sure if this would be right:

if(isServer) then {

private ["_posArray","_randPos","_posx","_posy","_posz","_object"];

_posArray = [

        [22680.2,19930.2,0.001],     
        [22580.2,19830.2,0.001],  
        [22480.2,19730.2,0.001]
    ];

    _randPos = _posArray call bis_fnc_selectrandom;  
    _posx = _randPos select 0;                                     
    _posy = _randPos select 1;    
    _posz = _randPos select 2;
        
_vehicle_2000 = objNull;
    if (true) then
    {
    _object = createVehicle ["Ural_TK_CIV_EP1", _posx,_posy,_posz [], 0, "CAN_COLLIDE"];
    _vehicle_2000 = _object;
    _object setposatl _posx;
    _object setposatl _posy;
    _object setposatl _posz;
    };
};

It seems simple but still confuses me :blink:

 

you are pulling every single digit out of the array ... just use it as it is after randomizing:

if(isServer) then {

_randPos = [

        [22680.2,19930.2,0.001],     
        [22580.2,19830.2,0.001],  
        [22480.2,19730.2,0.001]
    ]call bis_fnc_selectrandom;
        
_object = createVehicle ["Ural_TK_CIV_EP1", _randPos, [], 0, "CAN_COLLIDE"];
_object setposatl _randPos;
};

no need to make it more complicated that it needs to be ...

 

 

_this setposatl _pos;
_this setdir _dir;

This looks alot easier to me then the whole process with setting each position ;)

 

Also it does not matter where you create the object first (except on [0,0,0]) you put the object after the creation with setPos on its right position

 

The first is the position and the second value is the heading direction:

 

it is important that you set the position of an object AFTER changing its direction (especially important for objects) or the crashmodel might not be set right.

Link to comment
Share on other sites

  • 0

Random spawn location works fine now.

Next I would like to do is to spawn different gearsets inside the Ural with randomized amounts of guns.

So it doesn't spawn with always the same stuff.

 

Something like this e.a.:

gearset1 = [["glock17_EP1"],["Makarov"],["M9"]];
gearset2 = [["M16A4_ACG"],["MK_48_DZ"],["SVD_CAMO"]];

I tried it only with one gearset for the beginning and randomized the amount of guns with:

_amountG = round(random 3) * 5;

So it looked like this (what of course didnt work again, lol)

_randguns     =     [["glock17_EP1"],["Makarov"],["M9"],["M16A4_ACG"],["MK_48_DZ"],["SVD_CAMO"]] call BIS_fnc_selectRandom;   
;
_amountG = round(random 3) * 5;
 
_vehicle_2000 = objNull;
    if (true) then
    {
    _object = createVehicle ["Ural_TK_CIV_EP1", _randPos, [], 0, "CAN_COLLIDE"];
    _vehicle_2000 = _object;
    _object setDir 90;
    _object setposatl _randPos;
    clearweaponCargoGlobal _object;
    clearmagazineCargoGlobal _object;
    clearBackpackCargoGlobal _object;
    _object setVariable ["permaLoot",true];   //is this necessary for vehicles?
    _object addWeaponCargoGlobal [_randguns, _amountG];
    };

I guess I still think too complicated about these things...

Link to comment
Share on other sites

  • 0

Hi,

 

that might be a maths error.

If round (random 3) return 0, which it can, then it would calculate 0 * 5, which is always 0;

Also,

 

addWeaponCargoGlobal expects an array, that contains 2 array

 

(

 [[array_1],[array_2]]

)

 

and both array have to be the same size:

[[array_1,_array1_1,array_1_2],[array_2]]


Would therefore not work.

 

Link to comment
Share on other sites

  • 0

Hi,

 

that might be a maths error.

If round (random 3) return 0, which it can, then it would calculate 0 * 5, which is always 0;

 

i dont think that will be a problem, but in that case you can use ceil instead as it will never return the lowest number ...

 

 

Also,

 

addWeaponCargoGlobal expects an array, that contains 2 array

 

(

 [[array_1],[array_2]]

)

 

and both array have to be the same size:

[[array_1,_array1_1,array_1_2],[array_2]]

Would therefore not work.

 

wut??

this makes no sence at all .. he just needs to remove the brackets and it will return an item from the array wich is inserted in the right place, with the amount after as it should.

 

 

_randguns     =     [["glock17_EP1"],["Makarov"],["M9"],["M16A4_ACG"],["MK_48_DZ"],["SVD_CAMO"]] call BIS_fnc_selectRandom;   
;

 

remove all the brackets except for the outer ones and that will work ...

Link to comment
Share on other sites

  • 0

remove all the brackets except for the outer ones and that will work ...

Thanks, that's it.

 

About using ceil...ceil 0 is 0, isn't it?

I'm using it this way now to avoid getting a zero:

round(random 3) + 2; //min: 2, max: 5
round(random 3) + 3; //min: 3, max: 6
round(random 4) + 1; //min: 1, max: 5
round(random 1) + 1; //min: 1, max: 2

 

Im playing around with more gearsets atm. Is there a way to shorten this?

_guns1     =   ["glock17_EP1","Makarov","M9"] call BIS_fnc_selectRandom;    
_guns2    =    ["RPK_74","MK_48_DZ","Pecheneg_DZ"] call BIS_fnc_selectRandom;    
_guns3    =    ["SCAR_H_LNG_Sniper_SD","VSS_Vintorez"] call BIS_fnc_selectRandom;    
_guns4    =    ["M14_EP1","SVD_CAMO"] call BIS_fnc_selectRandom;    
_guns5    =    ["DMR","M40A3"] call BIS_fnc_selectRandom;    

_amountG1 = round(random 3) * 2;
_amountG2 = round(random 3) * 3;
 
    _object addWeaponCargoGlobal [_guns1, _amountG1];
    _object addWeaponCargoGlobal [_guns2, _amountG2];
    _object addWeaponCargoGlobal [_guns3, _amountG2];
    _object addWeaponCargoGlobal [_guns4, _amountG2];
    _object addWeaponCargoGlobal [_guns5, _amountG2];
Link to comment
Share on other sites

  • 0

 

Thanks, that's it.

 

About using ceil...ceil 0 is 0, isn't it?

I'm using it this way now to avoid getting a zero:

 

tbh i dont think it matters if you add 0 whatever to an object, i doubt that will give an error, but to clarify on ceil:

 

https://community.bistudio.com/wiki/Math_Commands

 

 

Im playing around with more gearsets atm. Is there a way to shorten this?

_guns1     =   ["glock17_EP1","Makarov","M9"] call BIS_fnc_selectRandom;    
_guns2    =    ["RPK_74","MK_48_DZ","Pecheneg_DZ"] call BIS_fnc_selectRandom;    
_guns3    =    ["SCAR_H_LNG_Sniper_SD","VSS_Vintorez"] call BIS_fnc_selectRandom;    
_guns4    =    ["M14_EP1","SVD_CAMO"] call BIS_fnc_selectRandom;    
_guns5    =    ["DMR","M40A3"] call BIS_fnc_selectRandom;    

_amountG1 = round(random 3) * 2;
_amountG2 = round(random 3) * 3;
 
    _object addWeaponCargoGlobal [_guns1, _amountG1];
    _object addWeaponCargoGlobal [_guns2, _amountG2];
    _object addWeaponCargoGlobal [_guns3, _amountG2];
    _object addWeaponCargoGlobal [_guns4, _amountG2];
    _object addWeaponCargoGlobal [_guns5, _amountG2];

 

you can use a foreach or count loop if you want to repeat ... but this should work

Link to comment
Share on other sites

  • 0

Yes, it works, I tested this before.

 

Doing so much trial and error today :D

Actually I'm trying to let the Ural spawn damaged:

_object setHit ["wheel_1_1_steering", 1];

That works, but when I choose "Repair Vehicle" ingame, it's immediately repaired without doing anything. :wacko:

I read about that it has to be executed local, but I'm not sure how this would work.

By the way "set Fuel" works fine without issues.

Link to comment
Share on other sites

  • 0

Yes, it works, I tested this before.

 

Doing so much trial and error today :D

Actually I'm trying to let the Ural spawn damaged:

_object setHit ["wheel_1_1_steering", 1];

That works, but when I choose "Repair Vehicle" ingame, it's immediately repaired without doing anything. :wacko:

I read about that it has to be executed local, but I'm not sure how this would work.

By the way "set Fuel" works fine without issues.

 

You have to set a variable on it aswell.

_object setVariable ["hit_wheel_1_1_steering", 1, true];
_object setHit ["wheel_1_1_steering", 1];
Link to comment
Share on other sites

  • 0

 

You have to set a variable on it aswell.

_object setVariable ["hit_wheel_1_1_steering", 1, true];
_object setHit ["wheel_1_1_steering", 1];

Cool, that works.

 

Many thanks to all of you guys!

With your help I managed to let a damaged Ural spawn at different positions, with varying loot and random amount. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...