MaxSt33le 0 Posted September 26, 2023 So I think I'm way in over my head on this one. What I'm trying to accomplish is a timer running off of an interactable object in the game using addAction. The timer should run for a predetermined number of seconds (5 in my example), register whenever a weapon is fired by anyone in that time, and output the total number of shots fired, as well as the time in seconds and one decimal of when the latest shot was fired. Once the timer reaches it's end it should stop counting and output the number of shots and time in some appropriate way (I've been working with hint, but anything works). I've tried looking for a script that does this, but haven't found one, so I decided to try to make it myself. If it does happen to exist already, I would be more than happy to use that instead. Here's where I am so far. I've mostly spliced together different pieces of code, and always felt like I was getting one step closer (which I probably wasn't at all), but I've hit a brick wall. If anyone could point me in the right direction, that would be great. Quote waitUntil {player == player}; RangePlayer = player; publicVariableServer "RangePlayer"; RangePlayer = nil; RBW_fnc_ShotTimer = { //Reset all values to 0 at start so old results don't interfere. timer = 0; shotNumber = 0; shotTimer = 0; VARNAME =[] spawn { while {timer < 5} do { timer = custom_timer + 0.1; hint str timer; uiSleep 0.1; RangePlayer = ""; "RangePlayer" addPublicVariableEventHandler { (_this select 1) addEventHandler ["Fired", { shotNumber = shotNumber + 1; shotTimer = timer }]; }; }; }; hintSilent format ["Number of shots fired:", shotNumber, "Time:", shotTime]; }; this addAction ["5 sec timer", call RBW_fnc_ShotTimer]; Share this post Link to post Share on other sites
Joshua9797 38 Posted September 26, 2023 It's just an idea, and nothing has been tested (it probably won't work this way), but I thought that an approach involving calculations with 'time' and using an array could work. Here's a version using the variable 'time' (time since the start of the mission) and an array. inside initServer.sqf JFR_ShotsTime = []; publicVariable "JFR_ShotsTime"; JFR_fnc_ShotTimer = { params ["_thePlayer"]; _initialTime = time; _index = _thePlayer addEventHandler ["Fired", { params ["_unit", "_weapon", "_muzzle", "_mode", "_ammo", "_magazine", "_projectile", "_gunner"]; JFR_ShotsTime pushBack time; publicVariable "JFR_ShotsTime"; }]; sleep 5; _thePlayer removeEventHandler ["Fired", _index]; _result = "Number of shots fired: " + str (count JFR_ShotsTime) + " timestamps: "; { _result = _result + (str (_x - _initialTime)) + "sec, "; } forEach JFR_ShotsTime; JFR_ShotsTime = []; // reset publicVariable "JFR_ShotsTime"; hint _result; }; publicVariable "JFR_fnc_ShotTimer"; inside init of Obj with addAction: this addAction ["5 sec timer", { params ["_target", "_caller", "_actionId", "_arguments"]; [_caller] call JFR_fnc_ShotTimer; }]; I am still a beginner in SQF, so please take my statements with a grain of salt. 2 Share this post Link to post Share on other sites
Joshua9797 38 Posted September 26, 2023 I just tested the code and updated it in my previous post, and as they say, it works on my machine.😉 Share this post Link to post Share on other sites
MaxSt33le 0 Posted September 27, 2023 21 hours ago, Joshua9797 said: It's just an idea, and nothing has been tested (it probably won't work this way), but I thought that an approach involving calculations with 'time' and using an array could work. Here's a version using the variable 'time' (time since the start of the mission) and an array. inside initServer.sqf JFR_ShotsTime = []; publicVariable "JFR_ShotsTime"; JFR_fnc_ShotTimer = { params ["_thePlayer"]; _initialTime = time; _index = _thePlayer addEventHandler ["Fired", { params ["_unit", "_weapon", "_muzzle", "_mode", "_ammo", "_magazine", "_projectile", "_gunner"]; JFR_ShotsTime pushBack time; publicVariable "JFR_ShotsTime"; }]; sleep 5; _thePlayer removeEventHandler ["Fired", _index]; _result = "Number of shots fired: " + str (count JFR_ShotsTime) + " timestamps: "; { _result = _result + (str (_x - _initialTime)) + "sec, "; } forEach JFR_ShotsTime; JFR_ShotsTime = []; // reset publicVariable "JFR_ShotsTime"; hint _result; }; publicVariable "JFR_fnc_ShotTimer"; inside init of Obj with addAction: this addAction ["5 sec timer", { params ["_target", "_caller", "_actionId", "_arguments"]; [_caller] call JFR_fnc_ShotTimer; }]; I am still a beginner in SQF, so please take my statements with a grain of salt. Thank you! I will give it a try! Share this post Link to post Share on other sites