• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

What's the reason you can't set storage to -1?

Status
Not open for further replies.

Shadowman321

Member
Joined
Mar 27, 2010
Messages
205
Reaction score
22
What is the reasoning for this if?
I understand, that default value of -1 is returned if there is no storage defined.
But you can freely set value to other negative negative numbers. But you can't set it to -1.

Is it just some kind of protection, to avoid issues while someone messes up e.g. some quest scripts and set value to -1? Or some more meaning behind this, such as broken logic in other places?

What if I want some specific system that allows negative storage values? I can freely set value like -10. But if someone has +9 points, and gets -10, their score is broken, because its erased when it gets to -1.
 
What if I want some specific system that allows negative storage values? I can freely set value like -10. But if someone has +9 points, and gets -10, their score is broken, because its erased when it gets to -1.

Lua:
current = player:getStorageValue(key) -- (value: -9)
newPoints = 10
player:setStorageValue(key, math.min(0, current + newPoints))
-- if current + newPoints < 0, the code will use 0 instead negative numbers.

--

if you want use negative values, just comment this part.
 
By default it's -1.

If you set it to -1, it's now classified as being in it's default state, so it get's 'erased', to free up clutter in ram/database.

Regardless of it's new number, -1, 10, 9, -18... you'll always get the correct value when you call on that storage using player:getStorageValue
There is no functional difference.

--
Think of how large a character would be in the database if we kept all storage numbers that are -1 in the database..
There's no reason to grab or store 4 billion storages per character, since we know if the storageMap[key] is null, we can assume it's value is -1
 
Is it just some kind of protection
Yes! But more than that, its the logic for a reason.
because its erased when it gets to -1.
Same would happen if the default was set for 0? Would it not? There has to be a default.
Its like xikini said
There's no reason to grab or store 4 billion storages per character
So yeah, is it limited because of the -1 issue you are facing, yeah. But other scenarios could be way worse.

Sorry buddy, but it kind of just is what it is. But that's best part of scripting, learning new methods to handle data for different situations. I'm sure you can find an acceptable workaround, if you need help, thats what otland is for :D
 
Lua:
current = player:getStorageValue(key) -- (value: -9)
newPoints = 10
player:setStorageValue(key, math.min(0, current + newPoints))
-- if current + newPoints < 0, the code will use 0 instead negative numbers.

--

if you want use negative values, just comment this part.
I want to use negative storage values. It works properly with all negative values except -1, which is used as default if storage is undefined. I just commented that -1 = erase if from player.cpp and it works as I like it to. But I wonder if it may cause some issues in any other places.
By default it's -1.

If you set it to -1, it's now classified as being in it's default state, so it get's 'erased', to free up clutter in ram/database.

Regardless of it's new number, -1, 10, 9, -18... you'll always get the correct value when you call on that storage using player:getStorageValue
There is no functional difference.

--
Think of how large a character would be in the database if we kept all storage numbers that are -1 in the database..
There's no reason to grab or store 4 billion storages per character, since we know if the storageMap[key] is null, we can assume it's value is -1
I know why its clearing database if its "default" value. If I'm using only 10 different storage, its not that much to keep all of them, even if they are back on default value of -1. Its not like allowing to keep storage at -1 forces me to keep all 4 billion storage values for each character. Only the one I use. So clearing it out does make sense. But if I disable clearing it out, it only affects like 1 storage I use for my score system. All other storages are undefined (so by default -1), and if its defined, its positive value. And thats it.
I just simply wonder, if allowing this logic to set storage to -1 instead of erasing it would have some negative effects on some other functions. Or it is just simply to keep it clean in database. And if its only for cleaning db, I can live with that, because I'm aware of which storages I'm using, and if needed, I can clear them myself.
Yes! But more than that, its the logic for a reason.

Same would happen if the default was set for 0? Would it not? There has to be a default.
Its like xikini said

So yeah, is it limited because of the -1 issue you are facing, yeah. But other scenarios could be way worse.

Sorry buddy, but it kind of just is what it is. But that's best part of scripting, learning new methods to handle data for different situations. I'm sure you can find an acceptable workaround, if you need help, thats what otland is for :D

What other scenarios? Are there any functions that are affected by that? I removed that if from player.cpp and my scoring system works just fine. All other systems works fine. All other storages works just fine. I only have to remember, that if I set storage to -1 now, it won't be erased from db. So if there is a lot of them, I would probably need to clean them up myself. But thats not an issue for me.
I know I could work around this. Just simply define positive score storage, negative score storage (which would have positive numbers in database), and then preform math on them to calculate overall score for player.
But I want to know, if there is any reason that it doesn't allow to set storage to -1 (default value). Or is it simply just to keep database clean.
 
-1 its just a common identifier to know when remove something (so to speak), so i believe on sources wont affect anything but only on data files since some scripts may be using player:getStorageValue(s) == -1 as check to know if the storage exists or not

In short, changing to -2 still will be fine (get & add)
 
I'm so confused by this thread?

Why does -1 change anything?

What function / script are you getting errors with default source?

Just for my own sanity, I went and tested.. Everything seems to work like normal?

Untitled.png

Lua:
local action = Action()

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local storageKey = 45050
    local storageValue = player:getStorageValue(storageKey)
    
    print(storageValue)
    
    player:setStorageValue(storageKey, 9)
    storageValue = player:getStorageValue(storageKey)
    print(storageValue)
    
    player:setStorageValue(storageKey, storageValue - 10)
    storageValue = player:getStorageValue(storageKey)
    print(storageValue)
    
    player:setStorageValue(storageKey, -10)
    storageValue = player:getStorageValue(storageKey)
    print(storageValue)
    
    player:setStorageValue(storageKey, storageValue + 9)
    storageValue = player:getStorageValue(storageKey)
    print(storageValue)
    
    return true
end

action:id(2173)
action:register()
 
Setting to -1 removes the storage, but it doesnt matter, cause getStorageValue will return -1, so you dont have to edit anything. The only thing you may notice is non existent entry in player_storages
 
I'm so confused by this thread?

Why does -1 change anything?

What function / script are you getting errors with default source?

Just for my own sanity, I went and tested.. Everything seems to work like normal?

View attachment 65910

Lua:
local action = Action()

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local storageKey = 45050
    local storageValue = player:getStorageValue(storageKey)
   
    print(storageValue)
   
    player:setStorageValue(storageKey, 9)
    storageValue = player:getStorageValue(storageKey)
    print(storageValue)
   
    player:setStorageValue(storageKey, storageValue - 10)
    storageValue = player:getStorageValue(storageKey)
    print(storageValue)
   
    player:setStorageValue(storageKey, -10)
    storageValue = player:getStorageValue(storageKey)
    print(storageValue)
   
    player:setStorageValue(storageKey, storageValue + 9)
    storageValue = player:getStorageValue(storageKey)
    print(storageValue)
   
    return true
end

action:id(2173)
action:register()
Try to check value in database. getStorageValue function does display -1 (default value) if its non existent in DB/memory. But if you save and check values in DB, its not there, as it is erased from DB.
Setting to -1 removes the storage, but it doesnt matter, cause getStorageValue will return -1, so you dont have to edit anything. The only thing you may notice is non existent entry in player_storages
Yes, it would work fine if I would only use getStorageValue and setStorageValue. But I want some statistics from some storages to be displayed for all players via some commands, on website, via discord command etc. So I take values from database directly in some places.
And then, I have to handle each case to display default value of -1 each time it didn't found any value. And also it doesn't give me any option to verify if its undefined or if its value is defined to -1. But I can simply remove that erase if from player.cpp and it works. And then I can have information that it has -1 as value, has some other value or is undefined. If I erase each -1 entry from DB (current tfs behaviour) I don't have this information.
That is why I ask, if this is only to keep db clean from storage entries with -1 value, or there are some other reasons. If its only to cleanup DB, I will simply remove that if to make my life easier in few places, and take care of cleaning up database.
But if it will break some other functionality somewhere, I would consider to rewrite this functionality, or to work on my system some other way.
Probably I just should use some additional table in db to keep those scores and work on it with my rules. I just started working on it based on storage values, and also figured out it would make it easier to get advantage of keeping those values in memory without copying player_storage functionality. But then during tests one player got -1 total score, and it failed to calculate his rankings, because value was erased from database.
 
Try to check value in database. getStorageValue function does display -1 (default value) if its non existent in DB/memory. But if you save and check values in DB, its not there, as it is erased from DB.

Yes, it would work fine if I would only use getStorageValue and setStorageValue. But I want some statistics from some storages to be displayed for all players via some commands, on website, via discord command etc. So I take values from database directly in some places.
And then, I have to handle each case to display default value of -1 each time it didn't found any value. And also it doesn't give me any option to verify if its undefined or if its value is defined to -1. But I can simply remove that erase if from player.cpp and it works. And then I can have information that it has -1 as value, has some other value or is undefined. If I erase each -1 entry from DB (current tfs behaviour) I don't have this information.
That is why I ask, if this is only to keep db clean from storage entries with -1 value, or there are some other reasons. If its only to cleanup DB, I will simply remove that if to make my life easier in few places, and take care of cleaning up database.
But if it will break some other functionality somewhere, I would consider to rewrite this functionality, or to work on my system some other way.
Probably I just should use some additional table in db to keep those scores and work on it with my rules. I just started working on it based on storage values, and also figured out it would make it easier to get advantage of keeping those values in memory without copying player_storage functionality. But then during tests one player got -1 total score, and it failed to calculate his rankings, because value was erased from database.
Couldn't you just do a default value as -1 for your queries?
SQL:
SELECT
    p.name,
    COALESCE(ps.value, -1)
FROM
    Players AS p LEFT JOIN
    Player_storage AS ps ON p.player_id = ps.player_id
ORDER BY ps.value DESC
LIMIT 10
(not sure about the table names or field names)
 
GUYS! You are not answering my question.

I KNOW I can write 10-30 lines of code with some logic in each place I want to use this storage for my system. But I can also remove 2 lines from source code, and have it easier in 5 different places.

I'm not asking for a workaround solution here. I'm asking if there is any reason to erase records with value = -1 from database, and if it affects any other function if I disable this option.

And you are just blabbing around about how I could work around that.
I'll most likely stay with this function (if -1 then erase) disabled.
Because it works, makes my life easier, doesn't seem to break anything, and allows me to distinguish between real -1 value, and undefined one.

If you want to comment further, please stay on topic, and provide information about any class/function that is broken if I keep -1 storage values in database.
If you want to give another workaround or clever solution, please don't :)
 
it sounds like you're trying to go around something that's easily fixable by just getting a default status but for some reason it triggers you
 
How is it easily fixable? How do you know if its undefined or -1?
Impossible with getStorageValue().
You can add even more logic in different places to check that. But how is it an easy fix and optimal in any way?

I can simply remove 2 lines of code from source, and it works. And it gives me additional information. And it is more optimized, as I don't have to preform several queries and go over additional logic to verify if its defined or not.

That is why it triggers me. I try to find a better solution, and all you guys are proposing is that I should work around that instead...
Because thats the default way people work here. Do stuff without any changes in source code? What is it? Some holly grail you're not supposed to touch and recompile?
Probably most people who comment in this thread don't even know what language is tfs written in...

And I have to explain and justify myself just to get some answers. Which I still didn't get...
 
I'm not asking for a workaround solution here. I'm asking if there is any reason to erase records with value = -1 from database, and if it affects any other function if I disable this option.
getStorageValue has default value -1 and when you set storage to this value, it's removed from database.
It's removed to reduce number of storages in database = make server/player save faster.

You can change it to version with defaultValue definied as you want for each 'get' query and add 'has' function:
Code:
player:getStorageValue(key[, defaultValue = -1])
player:setStorageValue(key, value)
player:hasStorageValue(key)
player:removeStorageValue(key)
There should be no problems.
 
Status
Not open for further replies.
Back
Top