SOFLAM Integration Core
Zoom on a handheld device
The same sight zoomed on the tripod and did nothing in the hand. The cause is not in the sight: a handheld device of the binoculars kind is aimed through a different system than a weapon, and three things that a weapon gets for free are missing.
The device in your hand is a gadget
The carried SOFLAM inherits the vanilla binoculars prefab. That prefab is an item with a binoculars gadget component and an optic component, not a weapon. Raising it is
gadget aiming, a different path from weapon aiming. It reaches the optic (OnSightADSActivated is called, the overlay and the vision modes work), and that is why nobody noticed anything wrong. But
whatever the game does only for weapon aiming does not happen. Three of those things matter for zoom.
| What a weapon gets | What the gadget gets | Fix |
|---|---|---|
| The zoom keys are active (a context that exists only for weapon aiming and for turrets) | No zoom keys at all | Put the two zoom actions into a context that is active for the gadget |
| A camera that follows the zoom step | The binoculars camera reads the field of view once, when it is switched on | A camera that asks again every frame |
| Steps from the sight’s own configuration | The same, if you provide them | Fixed steps in a small class of your own |
1. The zoom keys never arrive
The game switches the zoom actions on only when the character is aiming a weapon whose optic has more than one step (shortened from the vanilla character controller):
if (player && GetAnimationComponent().IsWeaponADSTag())
{
SightsComponent sights = GetWeaponManagerComponent().GetCurrentSights();
if (sights)
{
SCR_SightsZoomFOVInfo fovInfo = SCR_SightsZoomFOVInfo.Cast(sights.GetFOVInfo());
if (fovInfo && fovInfo.GetStepsCount() > 1)
am.ActivateContext("CharacterWeaponMagnificationContext"); // WeaponOpticsZoomIn / WeaponOpticsZoomOut live here
}
}
For a gadget there is no weapon, so the context is never activated. The sight itself does register listeners for the two actions when it is switched on (the vanilla 2D sights component does that in
OnSightADSActivated), so the only thing missing is that the actions are active. The Core adds them to the contexts that are active for the carried device, next to its own keys:
ActionContext CharacterGeneralContext {
ActionRefs +{
"GTG_SOFLAM_DesignatorAction"
"GTG_SOFLAM_RangefindAction"
"GTG_SOFLAM_TrackingAction"
"WeaponOpticsZoomIn"
"WeaponOpticsZoomOut"
}
}
// the same two lines in CharacterWeaponContext
2. The steps
Vanilla variable zoom divides the range between a base zoom and a maximum into equal steps. The SOFLAM wants five fixed ones: 2.5, 5 (the old picture), 10, 15 and 20. A small class derived from the vanilla zoom info replaces the step list; the keys, the interpolation and the reticle scaling stay vanilla.
class GTG_SOFLAM_ZoomFOVInfo : SCR_SightsZoomFOVInfo
{
static int GTG_ZoomLevels() { return 5; }
static float GTG_ZoomAt(int level)
{
if (level <= 0) return 2.5;
if (level == 1) return 5;
if (level == 2) return 10;
if (level == 3) return 15;
return 20;
}
override array<float> ZoomsToArray()
{
array<float> zooms = new array<float>;
for (int i = 0; i < GTG_ZoomLevels(); i++)
zooms.Insert(GTG_ZoomAt(i));
m_iStepsCount = zooms.Count();
return zooms;
}
protected override float GetZoom(int i, int count)
{
return GTG_ZoomAt(i);
}
}
It is named at the sight in the prefab, and the chosen level is shared with the other displays (GTG_SOFLAM_VisionShared), so the tablet shows the same zoom:
GTG_SOFLAM_SoflamSightsComponent "{64C93DB693B5481E}" {
SightsFOVInfo GTG_SOFLAM_ZoomFOVInfo "{64C93DB6A923F9F6}" {
m_fBaseZoom 5
}
}
3. The label changes, the picture does not
With the keys fixed the label in the sight went from 2.5X to 20X, and the picture stayed exactly the same. The binoculars camera is the cause: it reads the optic’s field of view once, when it is activated, and then sets that value in every frame (shortened from the vanilla camera):
class CharacterCameraBinoculars : CharacterCamera1stPerson
{
protected float m_fBinocFOV;
protected SCR_2DOpticsComponent m_Optics;
override void OnActivate(ScriptedCameraItem pPrevCamera, ScriptedCameraItemResult pPrevCameraResult)
{
super.OnActivate(pPrevCamera, pPrevCameraResult);
// ...
m_fBinocFOV = m_Optics.GetFovZoomed(); // once
}
override void OnUpdate(float pDt, out ScriptedCameraItemResult pOutResult)
{
super.OnUpdate(pDt, pOutResult);
pOutResult.m_fFOV = m_fBinocFOV; // the same value in every frame
pOutResult.m_bBlendFOV = false;
}
}
A binoculars with a fixed zoom needs nothing more. A zoom that changes while you look through it does. Two small pieces fix it. First a modded class that asks the optic again in every frame; for every other pair of binoculars the answer is the same as before, so nothing changes for them:
modded class CharacterCameraBinoculars
{
override void OnUpdate(float pDt, out ScriptedCameraItemResult pOutResult)
{
super.OnUpdate(pDt, pOutResult);
if (m_Optics)
pOutResult.m_fFOV = m_Optics.GetFovZoomed();
}
}
Second the sight sets the optic’s field of view whenever the zoom level changes. A negative value means “this magnification”: the optic turns it into an angle on the next call.
protected override void AdjustZoom(float value)
{
super.AdjustZoom(value);
// the vanilla count runs one past the end when zooming in; limit it here
int level = m_iSelectedZoomLevel;
SCR_SightsZoomFOVInfo info = GetSightsFovInfo();
if (info)
level = Math.Clamp(level, 0, info.GetStepsCount() - 1);
m_iSelectedZoomLevel = level;
GTG_SnapZoom();
GTG_SOFLAM_VisionShared.SetZoom(level);
}
protected void GTG_SnapZoom()
{
if (GTG_IsMounted())
return; // on the tripod the engine blends the field of view itself
SetFovZoomed(-m_fMagnification); // m_fMagnification was set by SelectZoomLevel
}
GTG_IsMounted() reads the parent chain: it is true when any entity above the device carries the tripod component. It is the same truth the beam and the aiming code ask, in one place.Do this for your own handheld optic
- Find out whether your device is a gadget or a weapon: does its prefab inherit from the binoculars prefab? Then it is a gadget.
- Put
WeaponOpticsZoomInandWeaponOpticsZoomOutinto a context that is active for the carried device, in your own input configuration or the Core’s. - Give the optic a zoom info class that carries your steps.
- Add the modded camera class above (one copy is enough: if the Core is loaded, it is already there), and set
SetFovZoomed(-magnification)when your zoom level changes. - Test in the hand and on the tripod. The two paths differ, and a fix for one can break the other.