4
votes

I have two scripts running, and I cannot merge both of the scripts into one. There has to be two running.

Script A is almost completely local but it does call on a global function a few times. These are defined in Script B. I would like to know if it is possible that the function use local variables inside of script A somehow.

It is like this:

--Script A
local lastUpdateID = 308
local var1,var2=6,7
_G.writeDefinition(var1,var2)

--Script B
function _G.writeDefinition(var1,var2)
  -- Right here, is it possible that we can alter the 
  -- variable lastUpdateID? < (This is my question)
end

I have tried looking into getfenv and setfenv but they do not show that the local variable exists. The whole point of this is so that when Script A calls writeDefinition, lastUpdateID is incremented by one. lastUpdateID must remain a local variable, however.

EDIT: Ryan Stein's solution worked, but I have come across another problem later in the scripts.

Now it is like this:

local f_count=1
local function sell_lox()
    local sellID=5
    _G.writeDefinition(sellID,sellID.." PX_lvs")
end

It is similar to the original problem. From what I can tell, the only thing I can get is the SellID when writeDefinition is called. Is there a way to increment f_count from this when writeDefinition is called?

1
Why can you not merge the scripts? What problem are you trying to solve? - Ryan Stein
I cannot edit Script A because I am writing a mod for a game. I can only edit Script B. If I could edit script A I could simply make it global, or pass the variable through the function. - user3187718
I really discourage this approach. The reason the variable is local to that module is that it is an implementation detail. Messing around with it will likely cause unexpected effects now or later (for example when you upgrade). You are better off finding a way to achieve the result you want a different way. Post as a separate question if you need help don't edit as it likely requires quite a different problem. - Oliver

1 Answers

1
votes

Considering your circumstances, this will only be possible through the debug library.

function writeDefinition(var1, var2)
  local i, k, v = 0
  repeat -- Iterate through the calling function's local variables.
    i = i + 1
    k, v = debug.getlocal(2, i)
  until k == 'lastUpdateID'
  debug.setlocal(2, i, v + 1) -- Increment lastUpdateID.
end