Jump to content

Recommended Posts

Hi, i need help making a simple set/get variable onto a vehicle. What i'm trying to do is that only 1 vehicle is allowed to have a respawn point and not the rest.

I need put this _veh setVariable ["RWPoint",false]; somewhere so it's not being unDefined in the script when checking if it's false or true.

for "_i" from 1 to 5 do {

_veh = "Jeep" createVehicle [0,0,0];

if (_veh getVariable "RWPoint" == false) then { 

[west, _veh] call BIS_fnc_addRespawnPosition;

_veh setVariable ["RWPoint",true]; 
};

};

I also have a delete script when a vehicle is destroyed and here i want the vehicle that do have the respawnPoint to remove it when it's destroyed and put the variable to false.

Using a global variable like below does not work as intended and i can't get it to work with the set/get variable one.

Params ["_veh"];

deleteVehicle _veh;

if (RWPoint == true) then {

[_veh] call BIS_fnc_removeRespawnPosition;

RWPoint = false;

};

Suggestion?

Share this post


Link to post
Share on other sites

@Robustcolor, use this code:

_respawnPositionAdded = false;

for "_i" from 1 to 5 do {
    _veh = "Jeep" createVehicle [0,0,0];

    _veh setVariable ["RWPoint", false];

    if (!_respawnPositionAdded) then {
        [west, _veh] call BIS_fnc_addRespawnPosition;

        _veh setVariable ["RWPoint", true];

        _respawnPositionAdded = true;
    };
};

But in this code only the first vehicle will always be added to respawn positions. If you need that random vehicle will be added, then use this code:

_vehicles = [];

for "_i" from 1 to 5 do {
    _veh = "Jeep" createVehicle [0,0,0];

    _veh setVariable ["RWPoint", false];

    _vehicles pushBack _veh;
};

(selectRandom _vehicles) setVariable ["RWPoint", true];

Next, you should check vehicle variable before deleting vehicle itself:

params ["_veh"];

if (_veh getVariable ["RWPoint", false]) then {
    [_veh] call BIS_fnc_removeRespawnPosition;
};

deleteVehicle _veh;

 

  • Like 2

Share this post


Link to post
Share on other sites

if (_veh getVariable "RWPoint" == false) ... can lead to error if the variable is undefined.

if (isNil {_veh getVariable "RWPoint"} )   works as far as there is not yet variable "RWpoint" defined , no matter the further value

if ( _veh getVariable ["RWPoint",false])  will temporarily set "RWPoint" to false for _veh but the check is OK if this variable is true. So works also.

 

Now... with such code... You'll do the job at first respawn. Your respawned vehicle will not benefit of the same code.

  • Like 1
  • Confused 1

Share this post


Link to post
Share on other sites

Thanks, something like this should probarly work then.

 

Question, does the setVariable command remove/delete itself when the attached object is destroyed or deleted?

initServer.sqf

respawnPositionAdded = false;
Createvehicle.sqf

for "_i" from 1 to 5 do {

    _veh = "Jeep" createVehicle [0,0,0];

    if (!respawnPositionAdded) then {

        [west, _veh] call BIS_fnc_addRespawnPosition;

        _veh setVariable ["RWPoint", true];

        respawnPositionAdded = true;
    };
};
Deletevehicles.sqf

params ["_veh"];

if (_veh getVariable ["RWPoint", true]) then {

    [_veh] call BIS_fnc_removeRespawnPosition;
 
    respawnPositionAdded = false;
};

deleteVehicle _veh;

 

Share this post


Link to post
Share on other sites
2 hours ago, Robustcolor said:

Question, does the setVariable command remove/delete itself when the attached object is destroyed or deleted?

Yes, you don't need to delete variables using setVariable before deleting an object.

  • Like 1

Share this post


Link to post
Share on other sites

When you destroy a vehicle,all its variables stay on it (on the wreck).

But sometimes, in good scenario, for optimization of resource, the wreck is managed as garbage then deleted.

When an object is deleted, so the container of variable is (no more variable on a deleted object.... but...).

If you named the object, say bob in variable name of the object in editor, and passed variable on it (bob setVariable ["essai",true],

when destroyed:  isNil "bob" returns false;  isNil {bob getVariable "essai"}  returns false  (so the wreck stays the object, you changed its model/appearance)

when deleted: isNil "bob" returns false;  isNil {bob getVariable "essai"}  returns true;  but bob becomes an null object  isNull bob returns true. bob still exists as variable until you run: bob = nil;

  • Like 2

Share this post


Link to post
Share on other sites
On 3/16/2021 at 11:05 AM, Schatten said:

(selectRandom _vehicles) setVariable ["RWPoint", true];

When adding true/false after the name "RWPoint", does it affect if it's local or public variable? Or does it need to have setVariable ["RWPoint",true,false] for it to be local?

 

These are the examples from the wiki.

 

Changing the first line to false would make it local?

Why the extra [] in the second line?

_myTruck setVariable ["myPublicVariable", 123, true];

_myTruck setVariable ["myLocalVariable", ["321", _var], false];

 

Share this post


Link to post
Share on other sites

Read setVariable 

blahblah setVariable ["test",false,true]  broadcast the variable (so public it) no matter you set it to false or other value. If you omit the 3rd element (true) it's false by default, so the variable stays local (to the PC).  BIKI is clear.

_myTruck setVariable ["myLocalVariable", ["321", _var], false];  ........ false is not mandatory  and _myTruck getVariable "myLocalVariable" will return an array: ["321", _var value if any]

Share this post


Link to post
Share on other sites
On 3/16/2021 at 4:57 PM, pierremgi said:

if (_veh getVariable "RWPoint" == false) ... can lead to error if the variable is undefined.

if (isNil {_veh getVariable "RWPoint"} )   works as far as there is not yet variable "RWpoint" defined , no matter the further value

if ( _veh getVariable ["RWPoint",false])  will temporarily set "RWPoint" to false for _veh but the check is OK if this variable is true. So works also. 

 

I''ve been using the top one but as you said it could cause some error or nothing at all if variable is not set before when checking if the variable is true or false.

 

_veh setVariable ["RWPoint",true];

Since many vehicles don't have this variable i just want to check if a specific vehicle has the variable and that it's true.

 

Is this good or bad practice to check if the variable is true or false?

if (_veh getVariable ["RWPoint",false] == true) then {};

 

or is it enough to check it like this

if (_veh getVariable ["RWPoint",false]) then {}; but that will add a variable to it with false, Unnecessary?

Share this post


Link to post
Share on other sites
1 hour ago, Robustcolor said:

Is this good or bad practice to check if the variable is true or false?

if (_veh getVariable ["RWPoint",false] == true) then {};

 

or is it enough to check it like this

if (_veh getVariable ["RWPoint",false]) then {}; but that will add a variable to it with false, Unnecessary?

 

if (_veh getVariable ["RWPoint",false]) then {}; is the right syntax.

That means you need this variable to be true for the following code. If this variable doesn't exist, the variable returns false as default value but not set as is. So the variable will continue to be undefined until you give to it a value:

0= [] spawn {
  if (player getVariable ["RWPoint",TRUE]) then {hint "ok"}; //  hint "ok"
  sleep 2;
  if (isNil {player getVariable "RWPoint"}) then {hint "the variable doesn't exist yet"}; // the variable is still nil
};

 

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)
1 hour ago, Robustcolor said:

 

I''ve been using the top one but as you said it could cause some error or nothing at all if variable is not set before when checking if the variable is true or false.

 

_veh setVariable ["RWPoint",true];

Since many vehicles don't have this variable i just want to check if a specific vehicle has the variable and that it's true.

 

Is this good or bad practice to check if the variable is true or false?

if (_veh getVariable ["RWPoint",false] == true) then {};

 

or is it enough to check it like this

if (_veh getVariable ["RWPoint",false]) then {}; but that will add a variable to it with false, Unnecessary?

All you need is the second example you gave.  First one is redundant in the fact you are checking if(true) then {something}.  No need for == true. 

Also, you are NOT adding that variable to the _veh as you state.  You are ONLY giving a default answer if the _veh does NOT have the variable assigned via setVariable.  I.E. the returned value for if() is false.  Code will not run unless you if !().

Edited by panther42
posted at same time pierremgi did
  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, panther42 said:

Also, you are NOT adding that variable to the _veh as you state.  You are ONLY giving a default answer if the _veh does NOT have the variable assigned via setVariable.  I.E. the returned value for if() is false.  Code will not run unless you if !(). 

 

Why does this example below execute the code inside the if () then {}; even if _x has not the variable assigned. But the _veh variable above would not?

 {
  if (_x getVariable ["Test", false]) then {
    _x setVariable ["Test", true];
  };
} forEach allUnits;

This code below won't execute when checking == true.

{
  if (_x getVariable ["Test", false] == true) then {
    _x setVariable ["Test", true];
  };
} forEach allUnits;

What am i missing here?

Share this post


Link to post
Share on other sites

 

2 hours ago, Robustcolor said:

 

Why does this example below execute the code inside the if () then {}; even if _x has not the variable assigned. But the _veh variable above would not?

This code below won't execute when checking == true.


{
  if (_x getVariable ["Test", false] == true) then {
    _x setVariable ["Test", true];
  };
} forEach allUnits;

What am i missing here?

 

Normal... if not defined, so (false == true) .... is never true. 😋

Share this post


Link to post
Share on other sites

@Robustcolor How did you verify allUnits variable "Test" prior to running above code, and after running above code?  Did you verify none of them had previous variable "Test" assigned? 

The first code will run, but unless a unit variable "Test" has previously been set to "true", the code in then {} should not run.

 

Share this post


Link to post
Share on other sites

@panther42 Seems like i missed something, confused me abit why it would execute when it should not. It's working like you said 🙂


Question, adding a setvariable to each units not having it assigned and not repeating it on units which has it i guess adding true instead of false in the if ()?

{
  if (_x getVariable ["Test", true]) then {
    _x setVariable ["Test", true];
    _x setSkill 1;
  };
} forEach allUnits;

 

Share this post


Link to post
Share on other sites
7 minutes ago, pierremgi said:

Yes

if (_x getVariable ["Test", true])

Share this post


Link to post
Share on other sites

Not really.

If you are reading also my answers (I doubt), you can find the solution:
{ if ( isNil {_x getVariable "Test"}) then { _x setVariable ["Test", true]; _x setSkill 1; }; } forEach allUnits;
Or, as I guess you're scripting for a loop treating all spawned units:

0 = [] spawn {
  while [true} do {
    {
      _x setVariable ["test",TRUE}; // or any value, that doesn't matter
      _x setSkill 1;  // or any other code to be applied on all units
    } forEach (allUnits select { isNil {_x getVariable "test"} } );
    sleep 2;
  };
};

 

  • Like 2

Share this post


Link to post
Share on other sites
1 hour ago, pierremgi said:

(I doubt),

Never doubt, i'm just trying to understand the context in using the different codes 😎

 

if (_x getVariable ["Test", true]), my guess is that this would work and only once as your code does but maybe not the most efficient way.

 

Thanks @pierremgi

Share this post


Link to post
Share on other sites

As you say, not the most efficient way! (yours).

Mine was an example for treating any array of units/vehicles/objects..., just once, in a loop.

If I refer to your initial demand, I'm a little bit lost with your intention and/or your test with vehicles or units...

 

Sure things:

if (isNil {_x getVariable "test"}) // will check if the variable exists on _x . Nice isn't it?

 

if (_x getVariable ["test",true])  // will check if the variable is true or true by default if the variable doesn't exist. So, not really discriminant if the variable is never set to false! (or anything else than true). If you don't intend to set the variable to false (or else) somewhere, removing the condition is faster! 😎

 

if (_x getVariable ["test",false])  // will also check if the variable is true... (but false by default), so the non-existing variable cases or false (non true) cases will be skipped, with no error in case of non-existing variable. That's the common usage for true/false possibility. No real added value (on isNil way) for skipping already treated elements.

  • Like 1

Share this post


Link to post
Share on other sites

@Robustcolor, @pierremgi has what you should need above.  I was thinking more of running through once, and if (false) make the variable "true":

{
  if !(_x getVariable ["Test", false]) then {
    _x setVariable ["Test", true];
    _x setSkill 1;
  };
} forEach allUnits;

several ways to accomplish.  Above will skip the then {code} if already set to "true"

  • 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

×