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

Doubts about efficiency in the use of local variables in lua

FuturoAspira

New Member
Joined
Dec 14, 2021
Messages
5
Reaction score
1
Location
Brazil
GitHub
luanluciano93
Hello, I have questions about the efficiency of using local variables, who can help me... for example, what is the best way to use a local variable...

Case 1:
Lua:
local var1 = true
local var2 = true

function Test()
    if var1 then
        if var2 then
            print("ok")
        end
    end
end

Case 2:
Lua:
function Test()
    local var1 = true
    local var2 = true
    if var1 then
        if var2 then
            print("ok")
        end
    end
end

Case 3:
Lua:
function Test()
    local var1 = true
    if var1 then
        local var2 = true
        if var2 then
            print("ok")
        end
    end
end

An example of this in TFS is creating a local variable in any file local var = print("ok"), and you can see that it is checked twice when the server starts. But if you put the variable inside the function, it is not checked when the server starts.

1646255731321.png

I would like to talk about this...
 
on case 1 you are setting a variable as local but its being stated as global internally since its not inside a function/condition, so var1 & var2 can be modified from anywhere

while on case2 & case3 the variable its inside function which means that variable will be only executed when the function is being called and can be only modified from inside the function itself

Example case1:
Lua:
local a = 0 -- local 'a' stated as global internally
function test()
    a = 1 -- new assigment to same variable
    return a
end

test() -- returns 1

Example case2 & case3:
Lua:
function test()
    local a = 1
    return a
end

a = 2 -- a second variable is being created (global)
test() -- returns 1 since local 'a' can only be modified inside the function itself

Thats the main reason why local var = print("ok") is being executed/read, if you move var inside a function it wont print until the function is called

EDIT: You can also read the manual for detailed info: Programming in Lua : 4.2 (https://www.lua.org/pil/4.2.html)
 
Last edited:
Lua:
local x = 1
function()

end

Is the best way if the value of local wont be changed. You want values to be created once generally. Its small enough that storing it in memory isn't really a problem.

If you are creating local values that will change then you can put them where you need them they have to be create and modified anyway.

If you create a local outside of the function scope then if you change it in the function it will stay changed until it gets changed back or changed to something else.

Lua:
local x = 1
function()
x = 2
end

x would be 2 anytime that file is accessed (like an action or movement).

The only time you do something like this
Lua:
function()
local x = 10
  if x then
    local y = 10
end
end

Is if the data stored in x is required to be read before y is created so no errors will happen. Usually something like this.

Lua:
function()
local player = getPlayerByName(name)
if player then
     local giveItem = player->giveItem()
     if giveItem then
         print("player has item")
     end
end
end

As you can see if the player is false if you tried to make the giveItem the value we did it would return an error. "player doesn't exist" or whatever.
 
Last edited:
Note that a script.lua is a separate block-scope from the global one, so if you declare a local variable on a file outside of any function or wrapper, this variable will be local to the file itself, not to the entire interpreter.

literally a .lua file is equivalent to a control block like the following:
simply this comes by default without the need to declare the structure do-end
Lua:
do
--do somethings
end

so it should be clear that if you create a file data/scripts/file.lua
and this file contains something like this:
Lua:
local variable = print("ok")

local talkAction = TalkAction("!test")

function talkAction.onSay(player, words, param, type)
    return false
end

--talkAction:accountType(ACCOUNT_TYPE_GOD)
--talkAction:access(true)
--talkAction:separator(" ")
talkAction:register()
then the local variables variable, talkAction will only be scoped to this file.
 
Rule of thumb: declare it as close as before where it is necessary.

Why? Maximizes readibility and efficiency*
It means after the variable stops being relevant you're already removing it from memory.

*might not be true for highly optimized codes where you want to enforce cache efficiency.
 
Thanks for all the answers ...

Rule of thumb: declare it as close as before where it is necessary.

Why? Maximizes readibility and efficiency*
It means after the variable stops being relevant you're already removing it from memory.

*might not be true for highly optimized codes where you want to enforce cache efficiency.
What does that mean? Did you mean that it is better for the local variable to stay inside the function, so it will be deleted from memory after the function is executed?
 
Thanks for all the answers ...


What does that mean? Did you mean that it is better for the local variable to stay inside the function, so it will be deleted from memory after the function is executed?
a start and end of instruction is called a block:


function -- start of function block
if -- start of if block

end -- end of if block
for -- start of for block

end -- end of for block
end -- end of function block


The best situation for a local variable is to be created exactly on the block it will be used. Whoever reads the code will have the declaration close to where it's being used and plus once the block closes the variable is deleted.


Try to do it like this:
Lua:
if true then
local message = "Finally I exist"
print(message)
end

print("Do I still exist?")
print(message)

Now if you want the "message" variable to be used after this if block, you'll send the message declaration to above the block:

Lua:
local message = "Finally I exist"
if true then
print(message)
end

print("Do I still exist?")
print(message)
 
Back
Top