'LinKin Posted September 5, 2014 Share Posted September 5, 2014 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
xXMADEXx Posted September 6, 2014 Share Posted September 6, 2014 viewtopic.php?f=148&t=75501 (In the "variables" section) Only allows that variable or function be called in the block of code that it's in. local functions work the same way as local variables. Link to comment
'LinKin Posted September 6, 2014 Author Share Posted September 6, 2014 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
Saml1er Posted September 6, 2014 Share Posted September 6, 2014 Functions are like variables ( talking about access ). If you add local before a function then you can't access it from another file even if it's the same resource. It's lua so doesn't matter if it's server or client side. It's the same for both. Link to comment
ixjf Posted September 6, 2014 Share Posted September 6, 2014 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
rain_gloom Posted September 6, 2014 Share Posted September 6, 2014 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. Link to comment
Moderators IIYAMA Posted September 6, 2014 Moderators Share Posted September 6, 2014 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
ixjf Posted September 6, 2014 Share Posted September 6, 2014 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
'LinKin Posted September 6, 2014 Author Share Posted September 6, 2014 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 IIYAMA Posted September 6, 2014 Moderators Share Posted September 6, 2014 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now