-
Notifications
You must be signed in to change notification settings - Fork 149
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 'CBA_fnc_addPlayerEventHandler' #328
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* ---------------------------------------------------------------------------- | ||
Function: CBA_fnc_currentUnit | ||
|
||
Description: | ||
Returns the currently controlled unit. | ||
Different from "player" when remote controlling units via zeus. | ||
|
||
Parameters: | ||
None | ||
|
||
Returns: | ||
Currently controlled unit <OBJECT> | ||
|
||
Author: | ||
commy2 | ||
---------------------------------------------------------------------------- */ | ||
#include "script_component.hpp" | ||
SCRIPT(currentUnit); | ||
|
||
missionNamespace getVariable ["bis_fnc_moduleRemoteControl_unit", player] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* ---------------------------------------------------------------------------- | ||
Function: CBA_fnc_addPlayerEventHandler | ||
|
||
Description: | ||
Adds a player event handler. | ||
|
||
Possible events: | ||
"unit" - player controlled unit changed | ||
"weapon" - currently selected weapon change | ||
"loadout" - players loadout changed | ||
"vehicle" - players current vehicle changed | ||
"turret" - position in vehicle changed | ||
"visionMode" - player changed to normal/night/thermal view | ||
"cameraView" - camera mode changed ("Internal", "External", "Gunner" etc.) | ||
"visibleMap" - opened or closed map | ||
|
||
Parameters: | ||
_type - Event handler type. <STRING> | ||
_function - Function to add to event. <CODE> | ||
|
||
Returns: | ||
_id - The ID of the event handler. <NUMBER> | ||
|
||
Examples: | ||
(begin example) | ||
_id = ["unit", {systemChat str _this}] call CBA_fnc_addPlayerEventHandler; | ||
(end) | ||
|
||
Author: | ||
commy2 | ||
---------------------------------------------------------------------------- */ | ||
#include "script_component.hpp" | ||
SCRIPT(addPlayerEventHandler); | ||
|
||
params [["_type", "", [""]], ["_function", {}, [{}]]]; | ||
|
||
private _id = switch (toLower _type) do { | ||
case ("unit"): { | ||
[QGVAR(unitEvent), _function] call CBA_fnc_addEventHandler // return id | ||
}; | ||
case ("weapon"): { | ||
[QGVAR(weaponEvent), _function] call CBA_fnc_addEventHandler // return id | ||
}; | ||
case ("loadout"): { | ||
[QGVAR(loadoutEvent), _function] call CBA_fnc_addEventHandler // return id | ||
}; | ||
case ("vehicle"): { | ||
[QGVAR(vehicleEvent), _function] call CBA_fnc_addEventHandler // return id | ||
}; | ||
case ("turret"): { | ||
[QGVAR(turretEvent), _function] call CBA_fnc_addEventHandler // return id | ||
}; | ||
case ("visionmode"): { | ||
[QGVAR(visionModeEvent), _function] call CBA_fnc_addEventHandler // return id | ||
}; | ||
case ("cameraview"): { | ||
[QGVAR(cameraViewEvent), _function] call CBA_fnc_addEventHandler // return id | ||
}; | ||
case ("visiblemap"): { | ||
[QGVAR(visibleMapEvent), _function] call CBA_fnc_addEventHandler // return id | ||
}; | ||
default {-1}; | ||
}; | ||
|
||
// add loop for polling if it doesn't exist yet | ||
if (isNil QGVAR(pollingLoopAdded) && {_id != -1}) then { | ||
GVAR(pollingLoopAdded) = true; | ||
|
||
GVAR(oldUnit) = objNull; | ||
GVAR(oldWeapon) = ""; | ||
GVAR(oldLoadout) = []; | ||
GVAR(oldLoadoutNoAmmo) = []; | ||
GVAR(oldVehicle) = objNull; | ||
GVAR(oldTurret) = []; | ||
GVAR(oldVisionMode) = -1; | ||
GVAR(oldCameraView) = ""; | ||
GVAR(oldVisibleMap) = false; | ||
|
||
addMissionEventHandler ["EachFrame", { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep track of the EachFrame handler id so that the polling function can be removed if/when all player event handlers are removed again. |
||
private _player = call CBA_fnc_currentUnit; | ||
if !(_player isEqualTo GVAR(oldUnit)) then { | ||
GVAR(oldUnit) = _player; | ||
[QGVAR(unitEvent), [_player]] call CBA_fnc_localEvent; | ||
}; | ||
|
||
private _data = currentWeapon _player; | ||
if !(_data isEqualTo GVAR(oldWeapon)) then { | ||
GVAR(oldWeapon) = _data; | ||
[QGVAR(weaponEvent), [_player, _data]] call CBA_fnc_localEvent; | ||
}; | ||
|
||
_data = getUnitLoadout _player; | ||
if !(_data isEqualTo GVAR(oldLoadout)) then { | ||
GVAR(oldLoadout) = _data; | ||
|
||
// we don't want to trigger this just because your ammo counter decreased. | ||
_data = + GVAR(oldLoadout); | ||
|
||
{ | ||
private _weaponInfo = _data param [_forEachIndex, []]; | ||
if !(_weaponInfo isEqualTo []) then { | ||
_weaponInfo set [4, _x]; | ||
_weaponInfo deleteAt 5; | ||
}; | ||
} forEach [primaryWeaponMagazine _player, secondaryWeaponMagazine _player, handgunMagazine _player]; | ||
|
||
if !(_data isEqualTo GVAR(oldLoadoutNoAmmo)) then { | ||
GVAR(oldLoadoutNoAmmo) = _data; | ||
[QGVAR(loadoutEvent), [_player, GVAR(oldLoadout)]] call CBA_fnc_localEvent; | ||
}; | ||
}; | ||
|
||
_data = vehicle _player; | ||
if !(_data isEqualTo GVAR(oldVehicle)) then { | ||
GVAR(oldVehicle) = _data; | ||
[QGVAR(vehicleEvent), [_player, _data]] call CBA_fnc_localEvent; | ||
}; | ||
|
||
_data = _player call CBA_fnc_turretPath; | ||
if !(_data isEqualTo GVAR(oldTurret)) then { | ||
GVAR(oldTurret) = _data; | ||
[QGVAR(turretEvent), [_player, _data]] call CBA_fnc_localEvent; | ||
}; | ||
|
||
_data = currentVisionMode _player; | ||
if !(_data isEqualTo GVAR(oldVisionMode)) then { | ||
GVAR(oldVisionMode) = _data; | ||
[QGVAR(visionModeEvent), [_player, _data]] call CBA_fnc_localEvent; | ||
}; | ||
|
||
_data = cameraView; | ||
if !(_data isEqualTo GVAR(oldCameraView)) then { | ||
GVAR(oldCameraView) = _data; | ||
[QGVAR(cameraViewEvent), [_player, _data]] call CBA_fnc_localEvent; | ||
}; | ||
|
||
_data = visibleMap; | ||
if !(_data isEqualTo GVAR(oldVisibleMap)) then { | ||
GVAR(oldVisibleMap) = _data; | ||
[QGVAR(visibleMapEvent), [_player, _data]] call CBA_fnc_localEvent; | ||
}; | ||
}]; | ||
}; | ||
|
||
_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* ---------------------------------------------------------------------------- | ||
Function: CBA_fnc_removePlayerEventHandler | ||
|
||
Description: | ||
Removes a player event handler. | ||
|
||
Parameters: | ||
_type - Event handler type. <STRING> | ||
_id - The ID of the event handler. <NUMBER> | ||
|
||
Returns: | ||
None | ||
|
||
Examples: | ||
(begin example) | ||
["unit", _id] call CBA_fnc_removePlayerEventHandler; | ||
(end) | ||
|
||
Author: | ||
commy2 | ||
---------------------------------------------------------------------------- */ | ||
#include "script_component.hpp" | ||
SCRIPT(removePlayerEventHandler); | ||
|
||
params [["_type", "", [""]], ["_id", -1, [0]]]; | ||
|
||
switch (toLower _type) do { | ||
case ("unit"): { | ||
[QGVAR(unitEvent), _id] call CBA_fnc_removeEventHandler; | ||
}; | ||
case ("weapon"): { | ||
[QGVAR(weaponEvent), _id] call CBA_fnc_removeEventHandler; | ||
}; | ||
case ("loadout"): { | ||
[QGVAR(loadoutEvent), _id] call CBA_fnc_removeEventHandler; | ||
}; | ||
case ("vehicle"): { | ||
[QGVAR(vehicleEvent), _id] call CBA_fnc_removeEventHandler; | ||
}; | ||
case ("turret"): { | ||
[QGVAR(turretEvent), _id] call CBA_fnc_removeEventHandler; | ||
}; | ||
case ("visionmode"): { | ||
[QGVAR(visionModeEvent), _id] call CBA_fnc_removeEventHandler; | ||
}; | ||
case ("cameraview"): { | ||
[QGVAR(cameraViewEvent), _id] call CBA_fnc_removeEventHandler; | ||
}; | ||
case ("visiblemap"): { | ||
[QGVAR(visibleMapEvent), _id] call CBA_fnc_removeEventHandler; | ||
}; | ||
default {nil}; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either shorten to one sentence or separate with empty line for possible future improvements to documentation generation.