Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add weapon eventhandler framework #829

Merged
merged 10 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addons/events/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class CfgFunctions {
PATHTO_FNC(addMarkerEventHandler);
PATHTO_FNC(removeMarkerEventHandler);
PATHTO_FNC(registerChatCommand);
PATHTO_FNC(weaponEvents);
};
};
};
21 changes: 21 additions & 0 deletions addons/events/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ class RscDisplayChat {
false\
);
};

//#define DEBUG_MODE_FULL
#ifdef DEBUG_MODE_FULL
class CfgWeapons {
class arifle_MX_Base_F;
class arifle_MX_F: arifle_MX_Base_F {
class EventHandlers {
fired = "_this call CBA_fnc_weaponEvents"; // this weapon eventhandler is required!
};
class CBA_weaponEvents {
handAction = "gestureNo"; // hand animation, from CfgGesturesMale\States
sound = "Alarm"; // from CfgSounds
soundLocation = "LeftHandMiddle1"; // Where the sound is played. Selection on the soldier, not the weapon! Alternative: RightHandMiddle1
delay = 0.5; // delay for sound and hand animation, in seconds
onEmpty = 0; // 1: play sound and action defined above on the last round, 0: don't, default 1; the sound below is played anyway
soundEmpty = ""; // sound played on the last round
soundLocationEmpty = ""; // Where the sound for the last round is played.
};
};
};
#endif
80 changes: 80 additions & 0 deletions addons/events/fnc_weaponEvents.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_weaponEvents

Description:
Execute weapon events framework.

class MyWeapon: MyWeapon_base {
class EventHandlers {
fired = "_this call CBA_fnc_weaponEvents";
};
};

Parameters:
_unit - Unit that fired the weapon <OBJECT>
_weapon - The weapon fired by the unit <STRING>

Returns:
Nothing

Examples:
(begin example)
fired = "_this call CBA_fnc_weaponEvents";
(end)

Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params ["_unit", "_weapon"];

private _config = configFile >> "CfgWeapons" >> _weapon >> "CBA_WeaponEvents";

private _isEmpty = _unit ammo _weapon == 0;
private _onEmpty = true;

if (isNumber (_config >> "onEmpty")) then {
_onEmpty = getNumber (_config >> "onEmpty") == 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default returns 0, so can probably skip isNumber?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I want 1/true to be the default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not CBA_fnc_getConfigEntry?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overhead.

};

private _fnc_soundSource = {
private _soundSourceName = format [QGVAR(soundSource_%1), _soundLocation];
private _soundSource = _unit getVariable [_soundSourceName, objNull];

if !(_soundSource in attachedObjects _unit) then {
_soundSource = "Building" createVehicleLocal [0,0,0];
Copy link
Member

@jokoho48 jokoho48 Dec 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using #particlesource is faster

0.0093 ms

Cycles:
10000/10000

Code:
JK_test pushBack ("#particlesource" createVehicleLocal [0,0,0]);

VS

Result:
0.25208 ms

Cycles:
3967/10000

Code:
JK_test pushBack ("Building" createVehicleLocal [0,0,0]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How long does it live?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

until you delete it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not use #soundsource? You know, because it's supposed to be a soundsource?

Copy link
Member

@jokoho48 jokoho48 Dec 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result:
0.0075 ms

Cycles:
10000/10000

Code:
"#particlesource" createVehicleLocal [0,0,0]

vs

Result:
0.143308 ms

Cycles:
6978/10000

Code:
"#soundsource" createVehicleLocal [0,0,0];  

i think there is more stuff run in the background

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, seems like those don't work with createVehicleLocal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does not work with createVehicleLocal?

_soundSource attachTo [_unit, [0,0,0], _soundLocation];
_unit setVariable [_soundSourceName, _soundSource];
};

_soundSource
};

if (isEmpty) then {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing _

private _sound = getText (_config >> "soundEmpty");
private _soundLocation = getText (_config >> "soundLocationEmpty");

if (_sound != "") then {
(call _fnc_soundSource) say3D _sound;
};
};

if (!_isEmpty || _onEmpty) then {
private _handAction = getText (_config >> "handAction");
private _sound = getText (_config >> "sound");
private _soundLocation = getText (_config >> "soundLocation");
private _delay = getNumber (_config >> "delay");

[{
params ["_unit", "_handAction", "_sound", "_soundSource"];

if (local _unit) then {
_unit playAction _handAction;
};

if (_sound != "") then {
_soundSource say3D _sound;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sound object dont get delete anywhere so they would stack up. or did i miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No point in deleting it, if you can recycle it instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See L45

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha i oversaw that sorry :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but maybe delete it when the unit dies? because else when some units would use that and die they would eather way stack up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question is if the gain would be worth the effort. I have my doubts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it would. else you could take a look at what i did within the ammo cookoff. Object Pooling would be the perfect thing for this i think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to reuse the sound source from the corpse. But I still don't want to delete them after use.

};
}, [_unit, _handAction, _sound, call _fnc_soundSource], _delay] call CBA_fnc_waitAndExecute;
};