Jump to content
Denholm Reynholm

How to detect and display which player activated a trigger

Recommended Posts

Hi,

I am writing a small script to display player's times in a race environment. The script is as follows

time_start = diag_tickTime;
time_running = true;
0 = [] spawn
{
   while {time_running} do
    {
      _playerCalling =  name vehicle player;
      _time = [(diag_tickTime - time_start),"MM:SS.MS"] call BIS_fnc_secondsToString;
      hintSilent format ["%1's Time: %2", _playerCalling, _time] call BIS_fnc_MP;
      sleep 0.001;
   };
};

This script is activated by a trigger placed on the start line and is partially working.

My problem is that in the case of 2 players (eg. A and B) when player A crosses the line the name displayed to player B is "Player B's Time : " and vice versa.

My understanding is that "name vehicle player" simply detects the local player.

 

Is there a way to detect what player activates the trigger and display it to all clients?  So that when player A activates the trigger player B sees "Player A's Time "

Share this post


Link to post
Share on other sites

I think you can use :

time_start = diag_tickTime;

time_running = true;
_playerCalling = _this select 1 (select 1 return to the person who activated the trigger or command, selecto 0 being the holder of the command, which here is the trigger, if Im not wrong)

0 = [] spawn

{

while {time_running} do

{

_time = [(diag_tickTime - time_start),"MM:SS.MS"] call BIS_fnc_secondsToString;

hintSilent format ["%1's Time: %2", _playerCalling, _time] call BIS_fnc_MP;

sleep 0.001;

};

};

Share this post


Link to post
Share on other sites

The engine does not provide precise way of detecting which player activated the trigger. The trigger has a scan frequency or 0.5 sec, so every 0.5 second it checks if there is anyone in the zone. There could be 100 people in the zone on the next scan, which one was the first? There is simply no data for this. You can make an imprecise guess by taking 1st person from "thisList" https://community.bistudio.com/wiki/thisList or "list" https://community.bistudio.com/wiki/list. Also you should do it on server only trigger. Again this will not guarantee that person first on the list is the one, but on balance will probably be the one.

  • Like 1

Share this post


Link to post
Share on other sites

Heyho,

as @killzone_kid already mentioned, there are some problems with your script. I will try to sketch a different solution for your problem that should work better.

Right now the biggest problem with your approach is, that the trigger is activated on start and finish but runs a loop that counts the time but there is no connection betweens the script instances, so how should the second script instance know which player activated it and what time he started the race. So here is my solution:

 

Trigger on the startline with activation everybody and multiple times. On Activation:

[thisTrigger] execVM "racetimer.sqf";

dann racetimer.sqf:

if(!isServer) exitwith {}; //We want to only run this on the hosting computer, otherweise we get a message for every player connected

params["_trigger"]; //The Trigger itself

{
	private _time = _x getvariable ["LapTime",-1];
	if(_time > 0) then {
		//There is already a starting time for this player stored, so we print the laptime
		private _printTime = [(diag_tickTime - _time),"MM:SS.MS"] call BIS_fnc_secondsToString;
		((name _x)+": "+_printTime) remoteExec ["systemchat"]; //remoteExec is prefered against bis_fnc_mp. Also systemchat is less annoying that hint, but you can change that if you want
	}
	//Store the new lapstarttime
	_x setvariable ["LapTime",diag_tickTime,false];

} foreach list _trigger;

So what is the difference: Basically everytime the trigger is activated, the current time is stored IN every player in the trigger list. If there is already a time stored (and not default -1 is returned) the laptime is calculated and broadcasted to all players. Then the current time (the new lap start time) is stored again.

 

This will give you some decent laptimes but unfortunatly not in realtime, which is a bit more complicated and would involve a bit more scripting to work properly. I just hope you have plan a mission where the player race multiple laps and my script makes sense.

 

Possible improvements would be:

  • Create the trigger by script, for example in init.sqf and use setTriggerInterval to make the trigger poll faster (default is 500ms). Also this way the trigger could be serverside only, which may improve the performance a bit (depending on what you also have running in your mission, this might not be needed anyway).
  • Make racetimer.sqf a function, so there is no file access with the first call of execVM

 

And disclaimer: As always I have not tested anything posted here. Just wrote it from the top of my head.

  • 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

×