Pigfoot_Nosebeggar Posted March 18, 2014 Share Posted March 18, 2014 Hi, I'm fairly familiar with scripting MTA, but there is one problem I just cannot wrap my head around. An easy example. Let's say I have this: local k = 0 while k<=10 do k=k+1 --<---This initalizes a global variable outputChatBox(k) end All I want is to change the value of the local instance. Same problem as: local a = math.random(1,5) local b = math.random(1,5) if a>b then local k = 12 else local k = 13 end outputChatBox(k) Of course this does not give me anything, and I wouldn't wanna do this: local k = false local a = math.random(1,5) local b = math.random(1,5) if a>b then k = 12 else k = 13 end outputChatBox(k) I'm quite cautious with global variables in serverside scripts. However the script I am working on requires me to work serverside, and "k" can NOT be made global, as this would mess up everything for all other players. What is the best way for getting around this? I can not trigger serverevents from within a client loop or something, that would just be stupid. Link to comment
Spajk Posted March 18, 2014 Share Posted March 18, 2014 Are you sure that "k" becomes global? Link to comment
arezu Posted March 18, 2014 Share Posted March 18, 2014 (edited) Hi,An easy example. Let's say I have this: local k = 0 while k<=10 do k=k+1 --<---This initalizes a global variable outputChatBox(k) end All I want is to change the value of the local instance. That will change the value of the local instance, it wont create a global variable. Here is how the scope works in lua: a = 2 -- Access to this variable is possible anywhere from all files in this resource local b = 2 -- local variable, but global for this file, meaning that everything defined in this file has access -- to this variable -- function 'test' behaves like the variable 'a', all files in this resource have access to it function test() end -- function 'test2' behaves like the variable 'b', only this file have access to it local function test2() end function scope() local c = 0 -- Everything defined inside this function has access to this variable d = 0 -- Global variable, behaves like like the variable 'b'. Dont do this.. will only cause bugs. local b = 3 -- Creates a new local variable, the variable 'b' on line 2 still exists but just not -- in this function when writing 'b' if(c == 0)then c = 3 -- Changes the variable 'c' that was defined on line 16 b = 4 -- Changes the value of the variable 'b' defined on line 18 _G["b"] = 7 -- _G is a table with all global variables, and you can get access to global variables instead -- of variables defined here by using it by doing -- _G.variableName or _G["variableName"] -- On line 23, the value of the variable defined on line 2 is changed to 7 end -- Variable 'c' declared on line 16, and varible 'b' declared on line 18 are both -- removed from lua here (and may right away be removed from memory) end Edited March 18, 2014 by Guest Link to comment
Pigfoot_Nosebeggar Posted March 18, 2014 Author Share Posted March 18, 2014 Thank you so much for this explanation arezu. Very helpful! Exactly what I was looking for! Link to comment
Spajk Posted March 19, 2014 Share Posted March 19, 2014 I also have a question about this. If I have this: local a = 5 a = nil a = 3 So the question is, will on line 3 a global variable be created or it will change the value of local? Link to comment
Pigfoot_Nosebeggar Posted March 19, 2014 Author Share Posted March 19, 2014 I also have a question about this.If I have this: local a = 5 a = nil a = 3 So the question is, will on line 3 a global variable be created or it will change the value of local? Good question, gonna test that today. Link to comment
Moderators IIYAMA Posted March 19, 2014 Moderators Share Posted March 19, 2014 It stays local at that block of the code. a= 10 if a then local a = 3 a = 3 end outputChatBox(a)--10 But before you wrote the local: a= 10 if a then a = 3 local a = 3 end outputChatBox(a)--3 Link to comment
Tete omar Posted March 19, 2014 Share Posted March 19, 2014 @Pigfoot_Nosebeggar This can be useful as well: http://www.lua.org/pil/23.1.1.html 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