Moderators IIYAMA Posted December 30, 2012 Moderators Share Posted December 30, 2012 I am trying to use local functions but the value still exit the script. Like as sample: script 1 exits()--trigger local function exits() myValue = true end script2 function checkexit() if myValue then outputChatBox("exit the script") end end addCommandHandler ( "read", checkexit ) ------- It will output for me: "exit the script" What I am doing wrong? Does a local function do something else? This will solve it, but it isn't a local function. local myValue = true-- out of the function exits()--trigger local function exits() myValue = true end Link to comment
Castillo Posted December 30, 2012 Share Posted December 30, 2012 You are triggering the function before it's creation, or that code isn't the real one? Link to comment
Moderators IIYAMA Posted December 30, 2012 Author Moderators Share Posted December 30, 2012 I have 2 client scripts. Here I set the value in a local function. exits()--trigger local function exits() myValue = true end But the value will be active in other client scripts. Only when I make the value local. local myValue = true It will stay inside the client file. You can try this sample: script1 local function exits() myValue = true end addEventHandler ( "onClientResourceStart", resourceRoot, exits) script2 function checkexit() outputChatBox("read") if myValue then outputChatBox("exit the script") end end addCommandHandler ( "read", checkexit ) Link to comment
Anderl Posted December 30, 2012 Share Posted December 30, 2012 Values inside functions will ever be global if you don't declare them as local. Local functions mean they aren't shared outside of the file ( but any global variables declared within them will be global ), the same happens with any other kind of variable. Declaring a function is the same as declaring a variable because a function is a variable. local foo = function() end; -- this is a variable declaration just like the code below local foo2 = 23; -- this is a variable declaration too but 'foo' is a function variable and 'foo2' is an int variable. Declaring: local foo = function() end; Is the same as declaring this: local function foo() end; Link to comment
Moderators IIYAMA Posted December 30, 2012 Author Moderators Share Posted December 30, 2012 ok, thank you But when should you use a local function, when the data inside stay's global? Does this really matter in mta lua? (stopping the line) ";" Link to comment
Anderl Posted December 30, 2012 Share Posted December 30, 2012 You use local functions when, for example, you are creating a library. E.g. in C++, PHP and Java( I don't know about this one, never learned it ) there are classes and you can declare functions as private or publics so that programmers just use public functions to manage the whole thing and private functions do the job. In Lua, it's the same. You create local functions to do the job for you and the global functions will be the "interface". Well, this is just an example of what you can use local functions for, but there are a lot more. As for the ";" in the end, it doesn't matter. I just liked it since I started with PHP and C++. Link to comment
Moderators IIYAMA Posted December 30, 2012 Author Moderators Share Posted December 30, 2012 Ok thank you again. It was very clear. 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