Jump to content
Omurice.

Freezing a variable in another variable.

Recommended Posts

I currently have a script that tracks the velocity and distance traveled by a bullet. I have it set up currently to tell me these things in a hint box. After firing the weapon it displays the _velocity variable and the _distance variable and they update in real time through the hint. I want to know the distance at which the bullet becomes transonic (When the bullet drops beneath 343m/s). I need to get the _distance variables value when _velocity is equal to or less than 343 (something like if (_velocity < 343) then {_transonic = _distance};). The only problem is that if I set the new _transonic variable to be equal to _distance then in the hint box the variable will continue to update with _distance.

 

I need a way to take the _distance variable at a given value of _velocity and take the exact value at that time and save it to another variable.

I do know that I could just end the tracking when the _velocity reaches 343 but then the distance would no longer be tracked beyond that time.

Share this post


Link to post
Share on other sites

One way:

_transonic = 10000; // initialization

then in your on each framed scope:

if (_velocity < 343) then {_transonic = _distance min _transonic}

Share this post


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

One way:

_transonic = 10000; // initialization

then in your on each framed scope:

if (_velocity < 343) then {_transonic = _distance min _transonic}

 

This would update _transonic every time the velocity is below 343, you'd need a lockout for that:

_transonic = 10000; // initialization
_lockout = false;
if (_velocity < 343 AND !_lockout) then {_transonic = _distance min _transonic;_lockout = true;}

Implementation depends on how exactly the OP is checking, EachFrame and fired EH could look something like this:

//init global projectile array
GOM_fnc_transsonicCheck = [];
GOM_fnc_transsonicResults = [];

//add this to every unit you want to check
player addEventHandler ["Fired",{
	params ["_unit","_weapon","_muzzle","_mode","_ammo","_mag","_projectile"];

	GOM_fnc_transsonicCheck pushBack [_ammo,_projectile,_unit];

	//remove invalid projectiles
	_remove = _projectile spawn {
		waitUntil {!alive _this};
		GOM_fnc_transsonicCheck = GOM_fnc_transsonicCheck - [_this];
	}
}];

//mission EH to check all projectiles, better to have one per mission than per projectile
addMissionEventHandler ["EachFrame",{

	_debugHint = "";
	{
		_x params ["_ammo","_projectile","_shooter"];
		_debugHint = _debugHint + str (vectorMagnitude velocity _projectile) + "\n";

		if (vectorMagnitude velocity _projectile <= 343) then {                                               
			_distance = _shooter distance _projectile;
			GOM_fnc_transsonicResults pushBack (format ["%1 went transsonic at %2m",_ammo,_distance]);
			GOM_fnc_transsonicCheck = GOM_fnc_transsonicCheck - [_x];
			copyToClipboard str GOM_fnc_transsonicResults;
			systemChat str GOM_fnc_transsonicResults;
		};

	} forEach GOM_fnc_transsonicCheck;
hintsilent _debugHint;
}];
/* returns:
[
"B_65x39_Caseless went transsonic at 928.242m",
"B_65x39_Caseless went transsonic at 942.844m",
"B_9x21_Ball went transsonic at 113.393m",
"B_9x21_Ball went transsonic at 113.504m",
"B_9x21_Ball went transsonic at 113.542m",
"B_9x21_Ball went transsonic at 113.619m"]
*/

 

Cheers

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

That's exactly what I'm looking for. Thank you Grumpy.

  • Like 1

Share this post


Link to post
Share on other sites

7uTdHZp.jpgWhat I ended up doing was something a little janky but it works. 

lockedout = false //init

transonic = 10000 //init

if (_velocity < 343 AND !lockedout) then {transonic = _dist; lockedout=true}

then in the event handler for when the gun was fired I reset the lockedout and transonic variables

_unit addEventHandler ["fired", {lockedout = false; transonic = 10000;}];

  • 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

×