• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

How can I access&change variable between lua files?

Exeus

Advanced OT User
Joined
Oct 8, 2012
Messages
859
Reaction score
198
Hi!

I need to access variable that has to be shared between two lua scripts


talkactions/a.lua

Code:
for key, player in ipairs(players) do
player:sendTextMessage(MESSAGE_STATUS_DEFAULT, tostring(GLOBAL_CURRENT_SOMETHING));
end

creaturescripts/b.lua

Code:
onLogin(player)
player:sendTextMessage(MESSAGE_STATUS_DEFAULT, tostring(GLOBAL_CURRENT_SOMETHING));
GLOBAL_CURRENT_SOMETHING += 1;

global.lua
Code:
GLOBAL_CURRENT_SOMETHING = 1

unfortunely in a.lua it's always 1, meanwhile in b.lua it changes its value

how can I share that value between those files?

I tried using

local globals = require("global");

or

local globals = require("../../../global");

in both of them, but both fail module 'global' not found:
 
Last edited:
Solution
This absolutely does not belong in this section, you've been here since 2012, you should know if you're asking for help it belongs in support board by now. To answer the question, if you're using any version pre-1.0, this is impossible, because each interface has its own environment which makes _G un-editable cross-interface. Otherwise, the previous answers should suffice, you simply just do not define the variable as local and it is inserted into _G.
just don't write "local" and it will be global automatically (unless it was declared as local before or is function argument)

if you need to load it before other scripts, either go to data/global.lua or declare it in data/lib

there is no += operator in lua 5.1 and 5.2 as far as I know (you have to do it the "v = v+1" way)
 
@zbizu

But in data/global.lua I didn't use local keyword at all

it just GLOBAL_CURRENT_SOMETHING = 1;
 
Last edited:
then your variable is either misspelled or overwritten somewhere else because on 1.3 it works fine
1577244027488.png

global.lua
Code:
testglobal = 0
some talkaction script
Code:
testglobal = testglobal+1
print(testglobal)
some creaturescript onLogin
Code:
testglobal = testglobal+1
print(testglobal)
 
This absolutely does not belong in this section, you've been here since 2012, you should know if you're asking for help it belongs in support board by now. To answer the question, if you're using any version pre-1.0, this is impossible, because each interface has its own environment which makes _G un-editable cross-interface. Otherwise, the previous answers should suffice, you simply just do not define the variable as local and it is inserted into _G.
 
Solution
Back
Top