Kampfkarren / Roblox

Scripts and stuff I wrote for Roblox. Documentation is little to none as these are just stuff I took from my game that I thought I could share.
https://kampfkarren.github.io/Roblox/
Other
278 stars 74 forks source link

Pass previous value to function when calling :OnUpdate() #67

Open MattSchrubb opened 4 years ago

MattSchrubb commented 4 years ago

Using :OnUpdate() is great to check when a "key"'s value is changed, but what if we want to know what the previous value was? ie. To check how much the value changed when using Increment.

DataStore2("Gold", player):OnUpdate(function(newVal, prevVal)
    if newVal - prevVal > 10 then
         print("Now that's a lot of gold")
    end
end)

I'm not sure if there is a way to do this, but if not, then adding a way to access previously stored data easily would be the next step.

MattSchrubb commented 4 years ago

I actually did it myself. Only updating the :Set() functions and the :_Update() functions.

function DataStore:_Update(dontCallOnUpdate, prevVal)
    if not dontCallOnUpdate then
        for _,callback in pairs(self.callbacks) do
            callback(self.value, prevVal, self)
        end
    end

    self.haveValue = true
    self.valueUpdated = true
end

function DataStore:Set(value, _dontCallOnUpdate)
    local prevValue = self.value
    self.value = clone(value)
    self:_Update(_dontCallOnUpdate, prevValue)
end

function DataStore:Update(updateFunc)
        local prevVal = self.value
    self.value = updateFunc(self.value)
    self:_Update(nil, prevVal)
end

function CombinedDataStore:Set(value, dontCallOnUpdate)
    local tableResult = self.combinedStore:GetTable({})
    local prevVal = tableResult[self.combinedName]
    tableResult[self.combinedName] = value
    self.combinedStore:Set(tableResult, dontCallOnUpdate)
    self:_Update(dontCallOnUpdate, prevVal)
end

function CombinedDataStore:Update(updateFunc)
    local prevVal = self:Get()
    self:Set(updateFunc(self:Get()))
    self:_Update(nil, prevVal)
end

function CombinedDataStore:_Update(dontCallOnUpdate, prevVal)
    if not dontCallOnUpdate then
        for _, callback in pairs(self.onUpdateCallbacks or {}) do
            callback(self:Get(), prevVal, self)
        end
    end

    self.combinedStore:_Update(true)
end
Kampfkarren commented 4 years ago

Putting the previous value as the second argument is the most ergonomic, but it's a breaking change--we pass the data store as the second parameter. That seems like a mistake in hindsight, but I wonder if anyone relies on that behavior?

MattSchrubb commented 4 years ago

I haven't found a need to use the data store passed, but indeed it would be a breaking change for any game that uses that functionality.

MattSchrubb commented 4 years ago

Putting the previous value as the second argument is the most ergonomic, but it's a breaking change--we pass the data store as the second parameter. That seems like a mistake in hindsight, but I wonder if anyone relies on that behavior?

Can you add a function that would be able to do this? Or add a way to get the previous value?

Kampfkarren commented 4 years ago

I don't think it's that much of an issue to just have it as a last parameter, and if you don't need to use the data store argument to just _ it out.

dataStore:OnUpdate(function(newValue, _, oldValue)
MattSchrubb commented 4 years ago

Is this something that is planned on being added or are we supposed to just put it in ourselves? And if we have to put it in ourselves, is there a way that we can override the functions instead of needing to edit the code for every update?

Kampfkarren commented 4 years ago

You can submit a pull request :)