Jump to content
Sign in to follow this  
goldenfiver

Help needed: Player is too close to another player

Recommended Posts

Searched the forums but couldn't find exactly what I am looking for

I want to write a small script / create an addon that will help new players know if they are standing too close to eachother.

I want to create an indicator- a small circle which will be displayed on the bottom right corner of the screen. The circle will be painted green when the player stands more than 10 meters away from other players, yellow when he stands less than 10 and red when he stands less than 5.

The thing is , I need to figure out the script and I have no idea how it's done

Any help would be appriciated

Share this post


Link to post
Share on other sites

Okay, after some meddling with configs and numerous curses I finally managed to put something together which is actually working :)

Put this code into a file named "displayDistance.sqf" and put it into your missions folder:

disableSerialization;

cutRsc ["DistanceDisplay", "PLAIN"];
waitUntil {!isNull (uiNamespace getVariable ["distanceDisplayRsc", displayNull]);};

_distanceDisplayRsc = uiNamespace getVariable ["distanceDisplayRsc", displayNull];
_soldierRsc = _distanceDisplayRsc displayCtrl 1;

_distance = 0;
_middleRangeFound = false;
_green = [0.196078,0.803921,0.196078,0.5];
_orange = [1, 0.547058, 0, 0.5];
_red = [1, 0, 0, 0.5];
_playerSide = side player;
_unitSide = objNull;

while {alive player} do
{
   _distance = 100;
_middleRangeFound = false;
   {	
	_distance = player distance _x;
	_unitSide = side _x;
	if (_unitSide == _playerSide && _x != player && _distance < 5) exitWith 
	{
		_soldierRsc ctrlSetTextColor _red;
		_middleRangeFound = false;
	};
	if (_unitSide == _playerSide && _x != player && _distance < 10) then
	{
		_middleRangeFound = true;
	};
	if (_unitSide != _playerSide) then
	{
		_distance = 10;
	};
   } forEach playableUnits;

   if (!_middleRangeFound && (_distance >= 10 || _distance == 0)) then
   {
       _soldierRsc ctrlSetTextColor _green;
   }
else
{
	if (_middleRangeFound) then
	{
		_soldierRsc ctrlSetTextColor _orange;
	}; 
};

   sleep 0.2; 
};

And the following piece of code into your init.sqf:

if (!isServer) then
{
   execVM "displayDistance.sqf";
};

Okay to show some image you need to create a .paa-file with transparency. I made myself an image of a soldier which gets colored, so you're free to use it if you want :)

Now look into your missions folder and check if there is a file named "description.ext". If there isn't any file like that, create it!.

In this file, put this code somewhere:

class RscTitles {
class DistanceDisplay
{
	idd = -1;
	movingEnable = 0;
	duration = 86400;
	onLoad 	= "uiNamespace setVariable [""distanceDisplayRsc"", _this select 0];";

	class controlsBackground {
	};

	class controls {
		class playerIcon
		{
			idc = 1;
			type = 0;
			style = 48;
			colorBackground[] = {0,0,0,0};
			colorText[] = {0.196078,0.803921,0.196078,0.5};
			font = "Bitstream";
			sizeEx = 0;
			lineSpacing = 0;
			fixedWidth = 0;
			shadow = 0;
			x = "0.948958 * safezoneW + safezoneX";
			y = "0.9 * safezoneH + safezoneY";
			w = "0.0514583 * safezoneW";
			h = "0.095926 * safezoneH";
			text = "img\soldier.paa";
		};
	};
};
};

Important: If you had a description.ext in your missions folder and the class "RscTitles" is already present, just insert the class "DistanceDisplay" into it, effectively merging the classes into RscTitles (you can't define more than one of those...).

Confused? Just download the files and place them into your missions folder or merge them if you'd like.

PS: If you want to check the distances to other players excluding AIs, just replace "allUnits" with "playableUnits".

Edited by XxAnimusxX

Share this post


Link to post
Share on other sites

Thank you very much XxanimusxX- it works :)

Sometimes when 2 players stand 10 meters away from one another (orange) and move together towards another player- it wont turn red even if one of them gets close (will only turn red if one of them will break formation ).

Is it possible to display the names of the players who are standing close to me next to the image? (only when they stand less than 12-15 meters)?

Share this post


Link to post
Share on other sites
Sometimes when 2 players stand 10 meters away from one another (orange) and move together towards another player- it wont turn red even if one of them gets close (will only turn red if one of them will break formation ).

Hmm thats strange, I have to admit though that I just used some randomly placed AI's to test the script and it seemed to work. Could you upload a short video showing that? I think that would be the fastest way to see the problem.

If thats too much of a hassle for you, maybe you could explain the exact scenario in which this did happen so I'd be able to recreate that.

Is it possible to display the names of the players who are standing close to me next to the image? (only when they stand less than 12-15 meters)?

If you wouldn't mind using the hint in the upper right hand side corner of the screen, it would be pretty easy :D

If you want to show the names near the image, you'd have to insert some more GUI-controls into your description.ext and to be blunt - you'd have to limitate the number of names you'll be showing because you can't dynamically create GUI-controls.

Share this post


Link to post
Share on other sites

The problem is if it finds a unit less than 10 meters in never checks for one less than 5 meters away.

try chaning this

  if (_x != player && _distance < 10) exitWith {};

to this

  if (_x != player && _distance < 5) exitWith {};

Share this post


Link to post
Share on other sites

Okay, I had to change some things but now it should work.

Thanks for F2k Sel giving me the right pointers, the problem was as following:

The cycle didn't prioritize the "red state" and didn't differentiate between them, causing the cycle to return whenever someone less than 10m was detected.

To solve this problem, you have to check the whole array and exit the cycle prematurely just in case someone with less than 5m is detected, else cycle through the rest of the array and take a note whenever someone in the range of 5 ≤ x ≤ 10 for later use.

I edited my code in my first post above, I didn't test it though but it should work considering the logics.

Edited by XxAnimusxX

Share this post


Link to post
Share on other sites

Thanks alot guys !. I have one problem still, when you respawn the script wont work correctly (stuck on the last color displayed before death).

Should I change:

if (!isServer) then

{

execVM "displayDistance.sqf";

};

and add event handler for players to call the script?

Share this post


Link to post
Share on other sites

If you have a respawn-system, yes, you have to call that script again everytime the player is resurrected (keyword addEventHandler/"Respawn").

The code in the init.sqf ensures that newly connected players also get to execute that script, so I guess you'd want to add the eventhandler just under the execVM you meantioned.

Share this post


Link to post
Share on other sites
If you have a respawn-system, yes, you have to call that script again everytime the player is resurrected (keyword addEventHandler/"Respawn").

The code in the init.sqf ensures that newly connected players also get to execute that script, so I guess you'd want to add the eventhandler just under the execVM you meantioned.

Thanks, did just that (still needs testing but I guess it will work just fine).

Will making it a client side add-on be difficult? Also, how do I make it work only for players of the same faction?

*edit* got the basic functions to work client side,

Still having trouble with getting it to work after respawn. When I respawn I get this error (for both the normal script and the addon): variable _distancedisplayrsc does not support serialization call disableserialization in the current script

This got me confused, since the script starts with disableSerialization

Edited by goldenfiver

Share this post


Link to post
Share on other sites

Well that leaves me speachless as well :D

Can you post your init.sqf (or at least the part where you initially call the script + define the eventhandler)?

Maybe someone can figure out why you get an error message where none should occur :D

// Edit:

grrr, please put your code into PHP-Tags :D

Edited by XxAnimusxX

Share this post


Link to post
Share on other sites
Well that leaves me speachless as well :D

Can you post your init.sqf (or at least the part where you initially call the script + define the eventhandler)?

Maybe someone can figure out why you get an error message where none should occur :D

sure thing (addon version):

init.sqf

waituntil {alive player};

[0] execVM "distance\displaydistance.sqf";

player addEventHandler ["respawn", {_this exec "distance\displaydistance.sqf"}]; 

config.cpp

class CfgPatches
{
   class Display_Distance
   {
       units[] = {};
       weapons[] = {};
       requiredVersion = 0.1;
       requiredAddons[] = {};
   };
};

class Extended_PreInit_EventHandlers {
 Display_Distance_Init = "Display_Distance_Init_Var = [] execVM ""\distance\Init.sqf""";
};

class RscTitles 
{

class DistanceDisplay
{
	idd = -1;
	movingEnable = 0;
	duration = 86400;
	onLoad 	= "uiNamespace setVariable [""distanceDisplayRsc"", _this select 0];";

	class controlsBackground {
	};

	class controls {
		class playerIcon
		{
			idc = 1;
			type = 0;
			style = 48;
			colorBackground[] = {0,0,0,0};
			colorText[] = {0.196078,0.803921,0.196078,0.5};
			font = "Bitstream";
			sizeEx = 0;
			lineSpacing = 0;
			fixedWidth = 0;
			shadow = 0;
			x = "0.948958 * safezoneW + safezoneX";
			y = "0.9 * safezoneH + safezoneY";
			w = "0.0514583 * safezoneW";
			h = "0.095926 * safezoneH";
			text = "distance\img\soldier.paa";
		};
	};
};

};

script version:

init:

execVM "displayDistance.sqf";

player addEventHandler ["respawn", {_this exec "displaydistance.sqf"}];

description:

class RscTitles 
{

class DistanceDisplay
{
	idd = -1;
	movingEnable = 0;
	duration = 86400;
	onLoad 	= "uiNamespace setVariable [""distanceDisplayRsc"", _this select 0];";

	class controlsBackground {
	};

	class controls {
		class playerIcon
		{
			idc = 1;
			type = 0;
			style = 48;
			colorBackground[] = {0,0,0,0};
			colorText[] = {0.196078,0.803921,0.196078,0.5};
			font = "Bitstream";
			sizeEx = 0;
			lineSpacing = 0;
			fixedWidth = 0;
			shadow = 0;
			x = "0.948958 * safezoneW + safezoneX";
			y = "0.9 * safezoneH + safezoneY";
			w = "0.0514583 * safezoneW";
			h = "0.095926 * safezoneH";
			text = "img\soldier.paa";
		};
	};
};

};

both scripts work fine, the only problems I get is when players respawn.

*edit*- its it in php now :) Oh and by the way, what should I change for it to work only for players on the same side?

Edited by goldenfiver

Share this post


Link to post
Share on other sites

_this exec "displaydistance.sqf"

I thought it was execvm with sqf

Share this post


Link to post
Share on other sites

ahahah, not able to see the wood for the trees... thanks F2k Sel again, didn't even see it xDD

goldenfiver, there's your problem :D Please report back if its working after the change.

Share this post


Link to post
Share on other sites

Yeah it does :) Thank you very much.

Last thing I need is to make it work only for players on the same side , so It won't be used to cheat on pvp scenarios.

This is the script:

disableSerialization;

cutRsc ["DistanceDisplay", "PLAIN"];
waitUntil {!isNull (uiNamespace getVariable ["distanceDisplayRsc", displayNull]);};

_distanceDisplayRsc = uiNamespace getVariable ["distanceDisplayRsc", displayNull];
_soldierRsc = _distanceDisplayRsc displayCtrl 1;

_distance = 0;
_green = [0.196078,0.803921,0.196078,0.5];
_orange = [1, 0.547058, 0, 0.5];
_red = [1, 0, 0, 0.5];

while {alive player} do
{
   _distance = 100;
   {
	_distance = player distance _x;
	if (_x != player && _distance < 10) exitWith {};
   } forEach playableUnits;

   if (_distance >= 10 || _distance == 0) then
   {
       _soldierRsc ctrlSetTextColor _green;
   }
else
{
	if (_distance < 5) then
	{
		_soldierRsc ctrlSetTextColor _red;
	}
	else
	{
		_soldierRsc ctrlSetTextColor _orange;
	}; 
};

   sleep 0.2; 
};

I know I should change 'playableunits' to playable units on the same side, but I don't know how

Share this post


Link to post
Share on other sites

okay it's sloppy but it seems to work (did a quick test).

I updated my first post and the code, you can just copy and paste it :)

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  

×