Recently, I’ve done some research on Fusion and I was interested in the Computed, Observer and Value implementations, though i believed they were too ‘in the way’ and were unnecessarily bloated. So, i decided to make a quick and simple module which demonstrates a way in which I believe is better for this type of logic to be handled, this way is more flexible and easier to understand.
local Values = require(game.ReplicatedStorage.Values);
local Health = Values.new(100);
Health.Changed:Connect(function(newValue: number)
print('Your health is now:', newValue)
end)
Health.Value = 10; --> your health is now: 10
local TotalApples = Values.new(10);
local ApplesEaten = Values.new(0);
local ApplesLeft = TotalApples - ApplesEaten;
ApplesLeft.Changed:Connect(function(newValue: number)
print('You have '.. tostring(newValue).. ' apples left.')
end)
ApplesEaten.Value += 1; --> 9 apples left
ApplesEaten.Value += 1; --> 8 apples left
TotalApples.Value -= 1; --> 7 apples left
ApplesLeft.Value -= 1; --> errors because we cannot set the value of a value which depends on different values
local Map = Values.new() --> keep map unassigned
local Spawnpoints = Map.Spawnpoints --> a reference to a member 'Spawnpoints' (currently just nil)
print(Spawnpoints.Value) --> nil, can be seen with this line.
Map.Value = NewMap; --> updates all dependent nodes
print(Spawnpoints.Value) --> Spawnpoints instance under NewMap
Have fun! I’ve attempted to type it as much as possible but i do not think it is possible to type references (such as the last example) consistently.
Get the module here