Jump to content

Recommended Posts

Hey guys! So I`m just trying to run a simple check to see if the player is a certain distance from a building:

_playerPos = position player;
_nearestBuilding = nearestBuilding player;
_meters = _playerPos distance _nearestBuilding;


if (_meters < 6.5) then {

systemChat format ["%1 is inside", name player];

sleep 0.1;

};

Unfortunately nothing shows up in system chat and I get no error messages. I executed the script from init.sqf. Anyone have an idea what I`m missing?

 

EDIT: When called from a radio trigger while conditions are met, the message pops up. However I would like the script to run continually and show the text when conditions are actively met.

Share this post


Link to post
Share on other sites

Your code isn't a loop! so, run once and bye bye! Too bad if there is no player closer than 6.5 m from the center of a building (yes, center).

 

nearestBuilding is not a fine command. Some map buildings are missing and all edited buildings are not returned by this command (use nearestObject instead)

 

If you want to quickly see what buildings work, just run:

 

addMissionEventHandler ["draw3D", {
  drawIcon3D ["A3\ui_f\data\map\VehicleIcons\iconexplosiveat_ca.paa", [1,0,0,1], nearestbuilding player ,1,1,360,"",0, 1,"EtelkaMonospacePro"]
}];


Watch for the red dot.

 

Share this post


Link to post
Share on other sites

So your script in an infinite loop could look something like this:

_distance = 6.5;

while {true} do {
    if (count (nearestObjects [player,["House","Building"],_distance]) > 0) then {
        systemChat format ["%1 is inside",name player];
    };
    sleep 0.1;
};

There are also different ways of checking whether a player is inside a building. Killzone Kid made a nice and much more accurate check for whether the player is in a house on this wiki page: lineIntersectsSurfaces

Share this post


Link to post
Share on other sites

If you want to save some performance while comparing 2 distances, then use distanceSqr instead of distance.

You'll just have to change 6.5 to its square.

 

That way the engine doesn't have to calculate the squareroot.

 

Use "distance" only if you need to show the actual distance to the player.

Share this post


Link to post
Share on other sites

Distance2d is a lot quicker than distance and marginally quicker than distancesqr and it doesn't require extra maths afterwards

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

×