Jump to content

Defining funcions as local


'LinKin

Recommended Posts

Hello,

Have you seen some scripts using functions such as:

local funcion name()

--Code

end

Firstly I thought it was for not letting other server/client script files call the function, so that it must be called within the same file where it's defined. But I was wrong.

As I define some functions that are called inside another functions, it outputs me an error saying that the function I am trying to call is 'nil'.

So the question is, what is the usage of these local functions?

Thanks.

Link to comment

But for instance, if I have a local function, and I try to call it from another function (in the same script fie) it won't work.

Probably it will work if I call it from outside any function in the script file.

In this case, how would you do it so that server-side functions (for example) can't be called from another server-side script file where they are not implemented?

Link to comment
But for instance, if I have a local function, and I try to call it from another function (in the same script fie) it won't work.

Probably it will work if I call it from outside any function in the script file.

In this case, how would you do it so that server-side functions (for example) can't be called from another server-side script file where they are not implemented?

You CAN access local functions from within the same file, just like a local variable (again, functions are stored in variables - function foo () end is syntatic sugar for foo = function () end, just like local function foo () end is syntatic sugar for local foo = function () end). Now, functions within functions are called closures, and local functions within another function are logically not accessible outside of it.

Link to comment
  • Moderators
  
function myFunction () end -- global access 
local myFunction = myFunction -- local access 
  
  
  

Well, you always can make more blocks with the same variable, can`t you?

Global access for other files and a both the function.

Local access for everything under the function.

Link to comment
Storing a function locally is not necessarily a safety choice. Local variable access is simply faster.

And afaik, there is no way to access a local variable from outside its scope.

Actually, there is, through getlocal from the debug library, but the function you want to query the locals from must be active.

Link to comment
addEventHandler("onResourceStart", resourceRoot, 
function () 
--[[Code... 
... 
...]] 
initComponents() 
end) 
  
local function initComponents() 
-- Code... 
end 

Error: Attempt to call 'initComponents' a nil value.

And they're in the same file.

EDIT: Uh, it just came to my mind, do I have to define the local functions BEFORE I call them within another function/event ?

So like:

local function initComponents() 
-- Code... 
end 
  
addEventHandler("onResourceStart", resourceRoot, 
function () 
--[[Code... 
... 
...]] 
initComponents() 
end) 
  

Link to comment
  • Moderators

Difine the variable as local, before creating the function. Local's will be active after they have been set.

local initComponents 
  
addEventHandler("onResourceStart", resourceRoot, 
function () 
    --[[Code... 
    ... 
    ...]] 
    initComponents() 
end) 
  
function initComponents() 
    -- Code... 
end 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...