Page 1 of 1
[Q] Global variable for all plugins, scripts, etc.
Posted: Thu Apr 16, 2020 5:27 pm
by Sam
Is it possible to create a universal variable that will be available from all plugins and modules? So that I can control the plugin variable from another plugin.
For example:
Syntax: Select all
# global
_G:dict
# plugin1.py
tmp = _G.copy()
tmp['test'] = 0xDEED
_G.update(tmp)
# plugin2.py
print(_G.get('test', None))
Re: [Q] Global variable for all plugins, scripts, etc.
Posted: Thu Apr 16, 2020 6:08 pm
by L'In20Cible
You could simply create a package into ../addons/source-python/packages/custom/ and import it into both plugins so that they share the same dictionary. Not using an external package would just make it rather difficult, as you would need to ensure the plugins are loaded, etc. before picking into each other's.
Re: [Q] Global variable for all plugins, scripts, etc.
Posted: Thu Apr 16, 2020 6:10 pm
by satoon101
You mean like:
Syntax: Select all
from plugin1 import plugin1
print(plugin1.tmp)
Re: [Q] Global variable for all plugins, scripts, etc.
Posted: Thu Apr 16, 2020 7:36 pm
by Sam
You do not understand me. I would like to implement `globals()` for everything.
When executing the plugin, I cannot interact with `dict` from outside the plugin environment.
Syntax: Select all
# plugin1.py
from threading import Thread
from time import sleep
def set():
sleep(5)
_G['test_var1'] = 288
th = Thread(target=set)
th.daemon = True
th.start()
Syntax: Select all
# plugin2.py
from threading import Thread
from time import sleep
def get():
while True:
print('[i] test_var1 -> {}'.format(_G.get('test_var1', 0))
sleep(1)
th = Thread(target=get)
th.daemon = True
th.start()
Re: [Q] Global variable for all plugins, scripts, etc.
Posted: Thu Apr 16, 2020 8:05 pm
by L'In20Cible
Sam wrote:You do not understand me. I would like to implement `globals()` for everything.
When executing the plugin, I cannot interact with `dict` from outside the plugin environment.
Syntax: Select all
# plugin1.py
from threading import Thread
from time import sleep
from mypackage import _G
def set():
sleep(5)
_G['test_var1'] = 288
th = Thread(target=set)
th.daemon = True
th.start()
Syntax: Select all
# plugin2.py
from threading import Thread
from time import sleep
from mypackage import _G
def get():
while True:
print('[i] test_var1 -> {}'.format(_G.get('test_var1', 0)))
sleep(1)
th = Thread(target=get)
th.daemon = True
th.start()
Syntax: Select all
# ../addons/source-python/packages/custom/mypackage.py
_G = dict()
Code: Select all
sp plugin load plugin1;sp plugin load plugin2
[SP] Loading plugin 'plugin1'...
[SP] Successfully loaded plugin 'plugin1'.
[SP] Loading plugin 'plugin2'...
[i] test_var1 -> 0
[SP] Successfully loaded plugin 'plugin2'.
[i] test_var1 -> 0
[i] test_var1 -> 0
[i] test_var1 -> 0
[i] test_var1 -> 0
[i] test_var1 -> 288
[i] test_var1 -> 288
[i] test_var1 -> 288
[i] test_var1 -> 288
Re: [Q] Global variable for all plugins, scripts, etc.
Posted: Fri Apr 17, 2020 4:34 am
by Sam
I didn’t know that it worked like that. I thought that if import a module into two plugins, then each will have its own `dict`.
Thanks you.
Re: [Q] Global variable for all plugins, scripts, etc.
Posted: Sat Apr 18, 2020 12:51 pm
by Cat
i like when all modules(plugins) in one big project which reflect play mode on the server(zombie, jail, public) so any such plugin(module) knows about state of each other, and then there is no problem, maximum knowledge and convenience