Moderators IIYAMA Posted November 28, 2012 Moderators Share Posted November 28, 2012 I don't understant when I need to use "local" Wiki: A local variable only exists within the scope it is declared. The scope is the 'level' the variable is visible for the script, containing the value that was assigned to it. You should use local variables anytime it is possible, when you don't need to access them globally. This saves you from having troubles with duplicate variable names. Plus, access to local variables is faster.It is thus also good practice to declare as local any variables that are to exist in any scope within a script file, by declaring it using local outside any blocks. The main reason to make a variable global in a resource is when it must be accessed by more than one script file in the resource. Since all script files within a resource share the same global environment, it makes sense to use a global variable here. The health variable only exists within the if-condition block, that is between the 'then' and the 'end'. They mean this? function epic () if 7 > 3 then--starts? local P = 1 if P then outputChatBox("this works") end end--ends? end I know what they mean, but I don't know when I need to use it: Their samples: local playermoney = getPlayerMoney ( source ) local health = getElementHealth(player) Do I need local for math? or will it only make lagg. number1 = 1 number2 = 2 number3 = number1 + number2 outputChatBox(number3) Do I need local for tables? number3 = {[3] = 1,[2] = 2,[1] = 3} I hope you can explane to me when I do need to use local and when I don't need to use it. THX Link to comment
Moderators IIYAMA Posted November 28, 2012 Author Moderators Share Posted November 28, 2012 Lol nobody? Link to comment
AhmadQTR Posted November 28, 2012 Share Posted November 28, 2012 Lol nobody? Well i just know that 'local' defines something like local playername = getPlayerName (source) -- gets the player name local root = getRootElement() --gets root element function output () outputChatBox ( playername.."Has Joined the server!", root, 255, 255, 100, true ) end addEventHandler ( "onPlayerJoin", root, output ) Link to comment
myonlake Posted November 28, 2012 Share Posted November 28, 2012 local means it will only be shared by the function it is underneath. If I make a function and make a local in it, it means it will not be working outside the function. If I didn't make it local, as in, keep it global, it could be fetched by any other function. Link to comment
Moderators IIYAMA Posted November 28, 2012 Author Moderators Share Posted November 28, 2012 thank you, just one more question. local H = 1 -- set H to 1 local G = 0 -- set G to 0 function normal () if H == 1 then -- check if H is 1 local G = 1 -- make local end if G == 1 then -- check if G is 1 outputChatBox ("G") -- will it output G? end end normal() --trigger because they say at wiki: local showHealth = true outputChatBox("This is your status:", player) if showHealth == true then local health = getElementHealth(player) outputChatBox("Health: "..tostring(health), player) end The health variable only exists within the if-condition block, that is between the 'then' and the 'end'. Link to comment
Anderl Posted November 28, 2012 Share Posted November 28, 2012 local means it will only be shared by the function it is underneath. If I make a function and make a local in it, it means it will not be working outside the function. If I didn't make it local, as in, keep it global, it could be fetched by any other function. To be more accurate, a local variable is shared ONLY in the scope it was defined. Example: function myFunction( ... ) --blabla my code if ( ... ) then local varArgs = { ... }; outputChatBox( varArgs[1] == "I like cookies." and "..............................." or varArgs[2] == "I love them." and "No, you don't.", root, 255, 255, 255, false ); end if ( not varArgs ) then outputChatBox( "varArgs not defined in this scope.", root, 255, 255, 255, false ); end end Output: ............................... / No, you don't. varArgs not defined in this scope. Link to comment
Moderators IIYAMA Posted November 28, 2012 Author Moderators Share Posted November 28, 2012 so: function myFunction( ... ) if ( ... ) then local varArgs = { ... }; -- it can be used here end if ( not varArgs ) then -- it can't be used here. end end right? Link to comment
Anderl Posted November 28, 2012 Share Posted November 28, 2012 The statement with "not varArgs" condition is not needed, it was just to show you that it would not work outside of the scope ( in this case, out of the if statement ). So: A local variable out of anything -> not shared with other files; A local variable in a function -> not shared with the rest of the script, it's only shared in the function and ONLY if it's out of a statement or any function inside of that one. A local variable in a statement/function inside function -> not shared with its "main function" neither with the rest of the script. Link to comment
Moderators IIYAMA Posted November 28, 2012 Author Moderators Share Posted November 28, 2012 Thank you all, Your explanations were very clear. Link to comment
myonlake Posted November 28, 2012 Share Posted November 28, 2012 @Anderl: COMPETITION (not really) You're welcome 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