forgottenserver icon indicating copy to clipboard operation
forgottenserver copied to clipboard

Direct access to Storage Values from Lua

Open gesior opened this issue 3 years ago • 1 comments

Someone complained about magic -1 value in storages: https://otland.net/threads/whats-the-reason-you-cant-set-storage-to-1.279950/

It looks like we still use storage system made 15 years ago.

Explanation of what you want to do that is currently impossible

Manage storages stored in database from Lua. Make storage with value -1 settable.

Current functions:

player:getStorageValue(key)
player:setStorageValue(key, value)

Desired functionality

Proposed functions (like in key-value databases: get/set/has/delete):

player:getStorageValue(key[, defaultValue = -1)
player:setStorageValue(key, value[, forceSave = false])
player:hasStorage(key)
player:removeStorage(key)

1 Make getStorageValue returns other defaultValue, if specified.

All scripts with counters (kill tasks etc.) use something like that:

local storage = player:getStorageValue(123)
if storage == -1 then
    storage = 0
end

or:

local storage = math.max(0, player:getStorageValue(123))

to make default value 0, not -1. After changes it would be:

local storage = player:getStorageValue(123, 0)

2 Make setStorageValue saves -1 in database, if forceSave = true specified.

player:setStorageValue(123, -1, true)

would write value -1 to storages map and make it save in database.

These changes are backward compatible. Setting value to -1 with forceSave = false (default) would still remove storage from database. We may consider removing forceSave parameter and make it always save -1 to database. Only drawback would be that old scripts would not remove storages from database. In case someone got script, that uses 1000 storages for temporary data and remove them onLogout, it would increase player save time.

gesior avatar Mar 01 '22 10:03 gesior

Cool, I want to try and fix this

ranisalt avatar Mar 09 '22 23:03 ranisalt