Jump to content
Sign in to follow this  
dragonsyr

spawning and deleting objects on other object radius (help)

Recommended Posts

i have this code and

works for spawn the object on an object radial ,but i cant delete it

light script

if (isNil {missionNamespace getVariable "hlight"}) then
{
_ang = 0;
_rad = 6.5; //radius
_bcount = 6; //number of lights
_inc = 360/_bcount; 
for "_i" from 0 to _bcount do
{
_a = (_target_pos select 0)+(sin(_ang)*_rad);
_b = (_target_pos select 1)+(cos(_ang)*_rad);
_pos = [_a,_b,_target_pos select 2];
_ang = _ang + _inc;
_light = "Land_runway_edgelight" createVehicle _pos;
_light setPos _pos;
// lights set[_i, _light]; //only commented works for spawning
missionNamespace setVariable ["hlight", _light];
};};

the lights spawned and is in place i want.

i try to delete with this command

if (!isNil {missionNamespace getVariable "hlight"}) then {for "_i" from 0 to count(lights) - 1 do{
                       deleteVehicle (lights select _i); // i try also _light , hlight, no luck
                       }; hlight = nil;};

but nothing happened......

if uncomment the line "lights set" in my light.sqf i get this error

Error in expression <createVehicle _pos;

_light setPos _pos;

lights set[_i, _light];

missionNamespac>

Error position: <lights set[_i, _light];

missionNamespac>

Error Undefined variable in expression: lights

how can i define the spawn of my light as one "word" ,so after i can deleted from another script?

Share this post


Link to post
Share on other sites

Hey there,

no need to bother with missionNamespace, just use an array.

[color="#FF0000"]hlight_array = []; // Define once[/color]

if ([color="#FF0000"](count hlight_array) == 0[/color]) then
{
_ang = 0;
_rad = 6.5; //radius
_bcount = 6; //number of lights
_inc = 360/_bcount; 
for "_i" from 0 to _bcount do
{
_a = (_target_pos select 0)+(sin(_ang)*_rad);
_b = (_target_pos select 1)+(cos(_ang)*_rad);
_pos = [_a,_b,_target_pos select 2];
_ang = _ang + _inc;
_light = "Land_runway_edgelight" createVehicle _pos;
_light setPos _pos;
// lights set[_i, _light]; //only commented works for spawning
[color="#FF0000"]hlight_array set [(count hlight_array), _light]; // Add to array[/color]
};};

Then you can delete them:

[color="#FF0000"]{ hlight_array = hlight_array - [_x]; deleteVehicle _x; } forEach hlight_array;[/color]

The hlight_array = []; // Define once part should be done somewhere else though, in your init.sqf for example. We just need to create the array once per mission.

Edited by sxp2high

Share this post


Link to post
Share on other sites

can i define the

hlight_array = []; // Define once
in the script that spawn the lights?

i want to remove this action (spawning) after the light script ends. (another sqf call the remove of lights)

also i want the ability if isnil, because maybe the lights not exist when the other script run and call the delete of the lights

also if exists i dont want to respaun again if the player run again the addaction for the lights creation

---------- Post added at 15:58 ---------- Previous post was at 15:25 ----------

i figure out and works

if (isNil {missionNamespace getVariable "hlight"}) then
{
hlight_array = []; // Define once
if ((count hlight_array) == 0) then
{
_ang = 0;
_rad = 6.5; //radius
_bcount = 6; //number of lights
_inc = 360/_bcount; 
for "_i" from 0 to _bcount do
{
_a = (_target_pos select 0)+(sin(_ang)*_rad);
_b = (_target_pos select 1)+(cos(_ang)*_rad);
_pos = [_a,_b,_target_pos select 2];
_ang = _ang + _inc;
_light = "Land_runway_edgelight" createVehicle _pos;
_light setPos _pos;
hlight_array set [(count hlight_array), _light]; // Add to array
missionNamespace setVariable ["hlight", hlight_array];
};};};

and the remove command is

if (!isNil {missionNamespace getVariable "hlight"}) then {{ hlight_array = hlight_array - [_x]; deleteVehicle _x; } forEach hlight_array; hlight = nil;};

so , no duplicate lights !! :-))

thank you !

Edited by dragonsyr

Share this post


Link to post
Share on other sites

The (count hlight_array) will have the same effect as an isNil check.

In the delete code it removes them from the array, so after that the array will be empty.

{
hlight_array = hlight_array - [_x]; // Remove each light from the array
deleteVehicle _x; // And delete it
} forEach hlight_array;

Imagine it like:

if ((count hlight_array) == 0) then {

hint "no lights!";

} else {

hint "lights are already spawned!";

};

Edit:

Yep you got it, looks alright too. :)

---------- Post added at 03:11 PM ---------- Previous post was at 03:02 PM ----------

Now your using setVariable, so you have a bit of unneeded code in there.

Here this is cleaner and makes proper use of set and get variable:

if (isNil {missionNamespace getVariable "hlight"}) then {

_array = [];

_ang = 0;
_rad = 6.5; //radius
_bcount = 6; //number of lights
_inc = 360/_bcount;

for "_i" from 0 to _bcount do {
	 _a = (_target_pos select 0)+(sin(_ang)*_rad);
	 _b = (_target_pos select 1)+(cos(_ang)*_rad);
	 _pos = [_a,_b,_target_pos select 2];
	 _ang = _ang + _inc;
	 _light = "Land_runway_edgelight" createVehicle _pos;
	 _light setPos _pos;
	 _array set [(count _array), _light]; // Add to array
};

missionNamespace setVariable ["hlight", _array];

};

if (!isNil {missionNamespace getVariable "hlight"}) then {
_array = missionNamespace getVariable "hlight";
{ deleteVehicle _x; } forEach _array;
missionNamespace setVariable ["hlight", nil];
};

Share this post


Link to post
Share on other sites

i cant delete this way.... i keep the working one ...

i have one more question

in here i add one chem light in the center of the object..

if (isNil {missionNamespace getVariable "hlight"}) then
{
hlight_array = []; // Define once
if ((count hlight_array) == 0) then
{
_ang = 0;
_rad = 6.5; //radius
_bcount = 9; //number of lights
_inc = 360/_bcount; 
for "_i" from 0 to _bcount do
{
_a = (_target_pos select 0)+(sin(_ang)*_rad);
_b = (_target_pos select 1)+(cos(_ang)*_rad);
_pos = [_a,_b,_target_pos select 2];
_ang = _ang + _inc;
_light = "Land_runway_edgelight" createVehicle _pos; //Chemlight_green  //Land_runway_edgelight
_light setPos _pos;
_lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1]; //is defined also as chemlight
hlight_array set [(count hlight_array), _light]; // Add to array
missionNamespace setVariable ["hlight", hlight_array];
};};};

on delete all lights desapear except the chemlight on center.....how can i delete this also ?

Share this post


Link to post
Share on other sites

Just add it to the array as well, this should work:

_chemlight = _lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1]; //is defined also as chemlight
hlight_array set [(count hlight_array), _light];
hlight_array set [(count hlight_array), _chemlight];

Share this post


Link to post
Share on other sites

Thank you Sir !! working perfect...

:-))))

---------- Post added at 17:21 ---------- Previous post was at 16:56 ----------

how about if i can add another outer radius with more lights? i tried but no luck

if (isNil {missionNamespace getVariable "hlight"}) then
{
hlight_array = []; // Define once
if ((count hlight_array) == 0) then
{
//_lightc=_lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1];
_chemlight = _lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1]; 
_ang = 0;
_rad = 6.5; //radius
_outrad = 15;
_bcount = 9; //number of lights
_outbcount =9;
_inc = 360/_bcount; 
for "_i" from 0 to _bcount do
{
_a = (_target_pos select 0)+(sin(_ang)*_rad);
_b = (_target_pos select 1)+(cos(_ang)*_rad);
_a1 =(_target_pos select 3)+(sin(_ang)*_outrad);
_b1 = (_target_pos select 4)+(cos(_ang)*_outrad);
_pos = [_a,_b,_target_pos select 2];
_pos1 = [_a1,_b1,_target_pos select 2];
_ang = _ang + _inc;
_light = "Land_runway_edgelight" createVehicle _pos; //Chemlight_green  //Land_runway_edgelight
_light setPos _pos;
_light1 setPos _pos;
hlight_array set [(count hlight_array), _light]; // Add to array
hlight_array set [(count hlight_array), _chemlight];
hlight_array set [(count hlight_array), _light1];
//missionNamespace setVariable ["hlight", hlight_array];
};
for "_i" from 0 to _outbcount do
{
_a1 =(_target_pos select 0)+(sin(_ang)*_outrad);
_b1 = (_target_pos select 1)+(cos(_ang)*_outrad);
_pos1 = [_a1,_b1,_target_pos select 2];
_ang = _ang + _inc;
_light1 = "Land_runway_edgelight" createVehicle _pos; //Chemlight_green  //Land_runway_edgelight
_light1 setPos _pos;
hlight_array set [(count hlight_array), _light1];
missionNamespace setVariable ["hlight", hlight_array];
};};};

this give me no errors , but no outer light circle

Share this post


Link to post
Share on other sites

Are not missionNamespace and a global variable the same thing?

hlight = "hello";
hlight == missionNamespace getVariable "hlight" // is true

So no need to keep swapping results between a local and missionNamespace/global variable just

//create
if (isNil "hlight") then {
hlight = [];
_rad = 6.5;
_bcount = 9;
_inc = 360/_bcount; 
for "_ang" from 1 to 360 step _inc do {
	_a = (_target_pos select 0)+(sin(_ang)*_rad);
	_b = (_target_pos select 1)+(cos(_ang)*_rad);
	_pos = [_a,_b,_target_pos select 2];
	_light = "Land_runway_edgelight" createVehicle _pos;
	_light setPos _pos;
	hlight set [(count hlight), _light];
};
_centerLight = _lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1];
hlight set [(count hlight), _centerLight];
};


//delete
if ( !(isNil "hlight") ) then {
{
	deleteVehicle _x;	
}forEach hlight;
hlight = nil;
};

how about if i can add another outer radius with more lights?

//two circles
if (isNil "hlight") then {
hlight = [];
{
	_rad = _x;
	_bcount = 9;
	_inc = 360/_bcount; 
	for "_ang" from 1 to 360 step _inc do {
		_a = (_target_pos select 0)+(sin(_ang)*_rad);
		_b = (_target_pos select 1)+(cos(_ang)*_rad);
		_pos = [_a,_b,_target_pos select 2];
		_light = "Land_runway_edgelight" createVehicle _pos;
		_light setPos _pos;
		hlight set [(count hlight), _light];
	};
}forEach [6.5, 15];
_centerLight = _lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1];
hlight set [(count hlight), _centerLight];
};

Edited by Larrow

Share this post


Link to post
Share on other sites

ok i got it

if (isNil {missionNamespace getVariable "hlight"}) then
{
hlight_array = []; // Define once
if ((count hlight_array) == 0) then
{
_chemlight = _lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1]; 
_ang = 0;
_rad = 6.5; //radius
_outrad = 15;
_bcount = 9; //number of lights
_outbcount =9;
_inc = 360/_bcount; 
for "_i" from 0 to _bcount do
{
_a = (_target_pos select 0)+(sin(_ang)*_rad);
_b = (_target_pos select 1)+(cos(_ang)*_rad);
_a1 =(_target_pos select 0)+(sin(_ang)*_outrad);
_b1 = (_target_pos select 1)+(cos(_ang)*_outrad);
_pos = [_a,_b,_target_pos select 2];
_pos1 = [_a1,_b1,_target_pos select 2];
_ang = _ang + _inc;
_light = "Land_runway_edgelight" createVehicle _pos; //Chemlight_green  //Land_runway_edgelight
_light setPos _pos;
hlight_array set [(count hlight_array), _light]; // Add to array
hlight_array set [(count hlight_array), _chemlight];
};
for "_i" from 0 to _outbcount do
{
_a1 =(_target_pos select 0)+(sin(_ang)*_outrad);
_b1 = (_target_pos select 1)+(cos(_ang)*_outrad);
_pos1 = [_a1,_b1,_target_pos select 2];
_ang = _ang + _inc;
_light1 = "Land_runway_edgelight" createVehicle _pos1; //Chemlight_green  //Land_runway_edgelight
_light1 setPos _pos1;
hlight_array set [(count hlight_array), _light1];
missionNamespace setVariable ["hlight", hlight_array];
};};};

working

---------- Post added at 17:38 ---------- Previous post was at 17:34 ----------

//two circles
if (isNil "hlight") then {
   hlight = [];
   {
       _rad = _x;
       _bcount = 9;
       _inc = 360/_bcount; 
       for "_ang" from 1 to 360 step _inc do {
           _a = (_target_pos select 0)+(sin(_ang)*_rad);
           _b = (_target_pos select 1)+(cos(_ang)*_rad);
           _pos = [_a,_b,_target_pos select 2];
           _light = "Land_runway_edgelight" createVehicle _pos;
           _light setPos _pos;
           hlight set [(count hlight), _light];
       };
   }forEach [6.5, 15];
   _centerLight = _lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1];
   hlight set [(count hlight), _centerLight];
};

i ll try that , but right now i want the circles separate, because i want to change the type of lights in each circle ....

---------- Post added at 17:45 ---------- Previous post was at 17:38 ----------

working perfect

Thank you all :-)

Share this post


Link to post
Share on other sites
but right now i want the circles separate, because i want to change the type of lights in each circle

//two circles
if (isNil "hlight") then {
hlight = [];
//[rad, bcount, lightClass]
_circles = [
	[6.5, 9, "Land_runway_edgelight"],
	[15, 9, "Land_runway_edgelight"]
];
{
	_rad = _x select 0;
	_bcount = _x select 1;
	_lightClass = _x select 2;
	_inc = 360/_bcount; 
	for "_ang" from 1 to 360 step _inc do {
		_a = (_target_pos select 0)+(sin(_ang)*_rad);
		_b = (_target_pos select 1)+(cos(_ang)*_rad);
		_pos = [_a,_b,_target_pos select 2];
		_light = _lightClass createVehicle _pos;
		_light setPos _pos;
		hlight set [(count hlight), _light];
	};
}forEach _circles;
_centerLight = _lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1];
hlight set [(count hlight), _centerLight];
};

Share this post


Link to post
Share on other sites

thank you Larrow !! :-)

edit: i try that

_helitarget = pad;
_target_pos = getPos (_helitarget);
_lightType = "Chemlight_green";
if (isNil "hlight") then {
   hlight = [];
   //[rad, bcount, lightClass]
   _circles = [
       [6.5, 9, "Land_runway_edgelight"],
       [15, 9, "Land_runway_edgelight"]
   ];
   {
       _rad = _x select 0;
       _bcount = _x select 1;
       _lightClass = _x select 2;
       _inc = 360/_bcount; 
       for "_ang" from 1 to 360 step _inc do {
           _a = (_target_pos select 0)+(sin(_ang)*_rad);
           _b = (_target_pos select 1)+(cos(_ang)*_rad);
           _pos = [_a,_b,_target_pos select 2];
           _light = _lightClass createVehicle _pos;
           _light setPos _pos;
           hlight set [(count hlight), _light];
       };
   }forEach _circles;
   _centerLight = _lightType createVehicle [getPos _helitarget select 0, getPos _helitarget select 1,1];
   hlight set [(count hlight), _centerLight];
}; 

and i get

Error in expression <"hlight") then { hlight = []; //[rad, bcount, lightClass] _circle>

Error position: <//[rad, bcount, lightClass] _circle>

Error Invalid number in expression

Error in expression <"hlight") then { hlight = []; //[rad, bcount, lightClass] _circle>

Error position: <//[rad, bcount, lightClass] _circle>

Error Invalid number in expression

Edited by dragonsyr

Share this post


Link to post
Share on other sites

How are you running the code?

that error is due to the //comment line ,just remove the line and it should be ok.

Share this post


Link to post
Share on other sites

you r right.... working without the commented line... thank you sir !

---------- Post added at 13:24 ---------- Previous post was at 13:20 ----------

but how can i delete this? with this command i cant

if (!isNil {missionNamespace getVariable "hlight"}) then {{ hlight_array = hlight_array - [_x]; deleteVehicle _x; } forEach hlight_array; hlight = nil;};

Share this post


Link to post
Share on other sites

See post #8 //delete

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
Sign in to follow this  

×