Jump to content
🛡️FORUMS ARE IN READ-ONLY MODE Read more... ×
Sign in to follow this  
airfell

Check if object exists before creating it problem

Recommended Posts

Hey, first new thread, this looks like the right place.

I've got an addaction on a vehicle that is supposed to deploy a tower, but first it checks the players side and then it should check if the tower itself exists so we don't have multiples. I've tried many different isNil, isNull type things, but nothing seems to work. I've looked around but can't find a working example to work off of. Any help would be much appreciated!

_vehUnit = _this select 3 select 0;
_trkSide = _this select 3 select 1;
_towerNum = _this select 3 select 2;

if (_trkSide == playerside) then{
if (isNull _tower) then {
	_dir = getDir _vehUnit;
	_dir = _dir + 90;
	_pos = position _vehUnit;
	_yPos = _pos select 1;
	_pos set [1, _yPos - 25];
	_newPos = [_pos, 0, 20, 5, 0, 15, 0] call BIS_fnc_findSafePos;
	_tower = createVehicle [format ["Land_Cargo_Tower_V1_No%1_F", _towerNum], _newPos, [], 0, "None"];
	_tower setDir _dir;
	hint format ["Tower %1 has been built!", _towerNum];
} else {
	hint "Tower has already been built!";
};
} else {
hint "You do not belong to this faction!";
};

Share this post


Link to post
Share on other sites

Declare a global towernum for the side and set it at 0 at init. Then compare with 0 to allow building and increase towernum with 1.

Share this post


Link to post
Share on other sites

Lol, a counter was my original fix, however this is a MP mission and each side uses this script, you just put the side in the passed variables on init. I guess I could copy this script and make one for each side, but it seems like isNil or isNull should work somehow, I just can't seem to find the right example or haven't poked around enough still.

towerCounter = 0;
towerCounter = towerCounter + 1;

Not sure if this is what you mean.

Share this post


Link to post
Share on other sites

if(Isnil "_tower")then{};

or

waituntil{sleep 1; !Isnil "_tower"};

isnil checks for the variable to be defined, always state this before you use an isnull

isnull just checks for the object that is linked to the variable and will throw up errors if the variable isn't defined

also, and I have no idea if you are creating the tower on the same node you are running this code on, however if you are not then you need to network the variable that defines the object. example being..........

Txu_Tower = "classname" createvehicle .................

waituntil {alive Txu_Tower};

Publicvariable "Txu_Tower";

when you don't want the tower to exist as the variable, change the variable value

Txu_Tower = objnull; Publicvariable "Txu_Tower";

Edited by Terox

Share this post


Link to post
Share on other sites
Lol, a counter was my original fix, however this is a MP mission and each side uses this script, you just put the side in the passed variables on init. I guess I could copy this script and make one for each side, but it seems like isNil or isNull should work somehow, I just can't seem to find the right example or haven't poked around enough still.

towerCounter = 0;
towerCounter = towerCounter + 1;

Not sure if this is what you mean.

So a side can have more than one tower?

You could track it with an array

init.sqf

if (isNil "towerCounter") then {
   towerCounter = [0,0,0,0]; // EAST, WEST, INDEP, CIV
};

your script

_idx = [EAST,WEST, INDEPENDENT, CIVILIAN] find (side player);
_oldVal = towerCounter select _idx;
// do an if on _oldVal to decide if to spawn or not

//later, once spawned
towerCounter set [_idx, _oldVal + 1];
publicVariable "towerCounter";

Share this post


Link to post
Share on other sites

Oh thankyou guys so much. I've been fighting with this script for a few days before I broke down and asked for help.

Some background on what I'm doing:

Its kind of a "towerception": I've got a HEMTT box truck for each side running an addAaction to construct a tower (tower #1), after that tower is built, they can then build 3 more towers (2,3 and 4) off of it.

I'm at work right now, but I'll give some of these suggested methods a try and report back. Thankyou guys again.

Share this post


Link to post
Share on other sites

Sorry for double post, but I've found a new issue:

In my init (this is for a dedicated server) I state each counter (one for each side)

if (isDedicated) then {
West_TWR1_Counter = 0;
publicVariable "West_TWR1_Counter";
} else {
//other stuff....
};

and then where the if statement is:

if (West_TWR1_Counter == 0) then {
//tower build stuff...
West_TWR1_Counter = 1;
publicVariable "West_TWR1_Counter";
} else {
hint "Tower already exists!";
};

I have this code for each side(West,East,Guer) on a switchDo to execVM the script.sqf... The west one works just fine, but East and Guer give me an error in the RPT: Type Bool, expected number, string, array ect....

I've checked and checked, but the script is the exact same for each side with the exception of side based naming conventions. What gives?

Share this post


Link to post
Share on other sites

The exact error from .rpt and the related script would help :)

Share this post


Link to post
Share on other sites

Oh man, I'm a retard. Noticed this when I was getting ready to send the code...

West_TWR1_Counter = 0;
West_TWR2_Counter = 0;
West_TWR3_Counter = 0;
West_TWR4_Counter = 0;
publicVariable "West_TWR1_Counter";
publicVariable "West_TWR2_Counter";
publicVariable "West_TWR3_Counter";
publicVariable "West_TWR4_Counter";

East_TWR1_Counter = false;
East_TWR2_Counter = false;
East_TWR3_Counter = false;
East_TWR4_Counter = false;
publicVariable "East_TWR1_Counter";
publicVariable "East_TWR2_Counter";
publicVariable "East_TWR3_Counter";
publicVariable "East_TWR4_Counter";

Guer_TWR1_Counter = false;
Guer_TWR2_Counter = false;
Guer_TWR3_Counter = false;
Guer_TWR4_Counter = false;
publicVariable "Guer_TWR1_Counter";
publicVariable "Guer_TWR2_Counter";
publicVariable "Guer_TWR3_Counter";
publicVariable "Guer_TWR4_Counter";

You spend all this time pouring over one bit of code, you loose sight of the project as a whole, and miss a detail elsewhere. Originally I was going to make it a t/f bool but didn't for some reason later on down the road.

Share this post


Link to post
Share on other sites
Sign in to follow this  

×