otland / forgottenserver

A free and open-source MMORPG server emulator written in C++
https://otland.net
GNU General Public License v2.0
1.6k stars 1.07k forks source link

Direct access to Storage Values from Lua #3982

Open gesior opened 2 years ago

gesior commented 2 years ago

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.

ranisalt commented 2 years ago

Cool, I want to try and fix this