• 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!

TFS 1.X+ Doubt, local storage = 100 [inside/out in a function]

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,210
Solutions
35
Reaction score
206
what is better to use, and why?
1st)
LUA:
local itemQuest, itemQuant = 7867, 10
local storage = 19884

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if player:getStorageValue(storage) < 1 then
        player:addItem(itemQuest, itemQuant)
        player:sendTextMessage(25, "You received a quest item.")
        player:setStorageValue(storage, 1)
    else
        player:sendTextMessage(25, "You already get it.")
    end
  
    return true
end
2st)
LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local itemQuest, itemQuant = 7867, 10
    local storage = 19884

    if player:getStorageValue(storage) < 1 then
        player:addItem(itemQuest, itemQuant)
        player:sendTextMessage(25, "You received a quest item.")
        player:setStorageValue(storage, 1)
    else
        player:sendTextMessage(25, "You already get it.")
    end
  
    return true
end
 
From what I know, on the first one:

On server startup it will define itemQuest and storage locally (only visible for the functions on declared on this file), thus not having to declare it everytime the function gets executed. If thats true, it should be better than the second one.

The second one will declare locally (only visible inside the function) everytime the function executes.

Knowing that thoose values will not change, I think it is better to only declare it once on startup, so the first one.
 
3rd, seems like you won't reuse these values anywhere else so why waste memory.
LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if player:getStorageValue(19884) < 1 then
        player:addItem(7867, 10)
        player:sendTextMessage(25, "You received a quest item.")
        player:setStorageValue(19884, 1)
    else
        player:sendTextMessage(25, "You already get it.")
    end

    return true
end
 
3rd, seems like you won't reuse these values anywhere else so why waste memory.
LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if player:getStorageValue(19884) < 1 then
        player:addItem(7867, 10)
        player:sendTextMessage(25, "You received a quest item.")
        player:setStorageValue(19884, 1)
    else
        player:sendTextMessage(25, "You already get it.")
    end

    return true
end
Truly a gigantic waste of 192 bits of memory in the current year.

Also, the 1st as the 2nd redefines the variables upon each execution of onUse, and it generally looks better to have configurable values away from the main code.
 
Now compare 1st and 2nd option, such a difference, but well, he asked for the best option. Why are you going full Stackoverflow mentality right here?
Your reasoning was literally that it uses more memory (3 variables resulting in 192 bits when modern machines have 4+ gb on average) so configuration should be completely thrown out the window. Am I really the one with Stackoverflow mentality here?
 
1st is better, if these values are constant you dont want to define them each method call for no reason at all.
 
1st is better, if these values are constant you dont want to define them each method call for no reason at all.

So this is not right?
Also, the 1st as the 2nd redefines the variables upon each execution of onUse, and it generally looks better to have configurable values away from the main code.
 
it wasn’t the topic’s purpose to create this friction between some people.
I believe that using the first option in several codes will not cause me any problems, I will keep it.
I thank everyone who contributed
 
Yes, they will be declared only once, if you have variables in method scope so in body of onUse they will get declared each time you call that method. If you have them outside (above in this case) they will get redeclared when you use /reload method
 
Back
Top