Jump to content

Accessing local variable from the chunk above?


Recommended Posts

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
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 by Guest
Link to comment
  • Moderators

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

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...