Why does the FPS template use separate variables for the ammo and reloading state of the gun instead of just using the attributes (so that way it’s always synced)?
function BlasterController.new(blaster: Tool)
local self = {
ammo = blaster:GetAttribute(Constants.AMMO_ATTRIBUTE),
reloading = blaster:GetAttribute(Constants.RELOADING_ATTRIBUTE),
end
Instead of keeping it as a seperate variable, you can just access the attribute’s value whenever you need it. What is the point of handling it as a separate variable?
Yeah, I understand what you mean but there is a few advantages to using variables.
GetAttribute is slightly slower than reading from a local variable and in high performance FPS games where ammo can be checked every single frame the overhead could matter.
It’s cleaner because it allows them to make changes locally like deducting ammo by 1 and only push the update to the attribute when it is required - avoids constantly reading and writing to the Instance.