Even easier solution with no need for a tick event:
MACHINE "ShieldPreserve"
STATE ShieldPreserve
RULE event In
:action
SelectEx(s.shield,
EnableActivateEvents(s.this, 1);
Debug("enabled 'activate' events for shield", s.this, "on", s.this:owner);
);
:end
END
RULE event DeviceActivated
:action
if(e.device:shield,
e.device:owner:ShieldIntegrity := e.device:owner:LastShieldIntegrity;
Debug("Activated shield,", E.device, "on", E.device:owner, "( restored last integrity", E.device:owner:LastShieldIntegrity, ")");
);
:end
END
RULE event DeviceDeactivated
:action
if(e.device:shield,
e.device:owner:LastShieldIntegrity := e.device:owner:ShieldIntegrity;
Debug("Deactivated shield", E.device, "on", E.device:owner, "with last integrity at", E.device:owner:LastShieldIntegrity);
);
:end
END
END
END
When the shield gets deactivated (manually, per mission script or ship behaviour), it saves the ship's last known shield integrity in an object variable called "LastShieldIntegrity". Upon activating the shield again, this old shield integrity simply gets restored again. The script won't work for ships and/or shields, that aren't present at the start of the mission, though. You have to call EnableActivateEvents manually for each shield device, that gets spawned during the course of the mission - or else the script will simply ignore these additional shields.