*DELETED*

All other Source.Python topics and issues.
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

[Q] Global variable for all plugins, scripts, etc.

Postby Sam » Thu Apr 16, 2020 5:27 pm

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))
Last edited by Sam on Thu Apr 16, 2020 5:48 pm, edited 1 time in total.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [Q] Global variable for all plugins, scripts, etc.

Postby L'In20Cible » Thu Apr 16, 2020 6:08 pm

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.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: [Q] Global variable for all plugins, scripts, etc.

Postby satoon101 » Thu Apr 16, 2020 6:10 pm

You mean like:

Syntax: Select all

from plugin1 import plugin1

print(plugin1.tmp)
Image
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

Re: [Q] Global variable for all plugins, scripts, etc.

Postby Sam » Thu Apr 16, 2020 7:36 pm

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()
Last edited by Sam on Thu Apr 16, 2020 7:36 pm, edited 1 time in total.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [Q] Global variable for all plugins, scripts, etc.

Postby L'In20Cible » Thu Apr 16, 2020 8:05 pm

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
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

Re: [Q] Global variable for all plugins, scripts, etc.

Postby Sam » Fri Apr 17, 2020 4:34 am

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.
Last edited by Sam on Fri Apr 17, 2020 4:35 am, edited 1 time in total.
Cat
Junior Member
Posts: 4
Joined: Mon Mar 23, 2020 11:27 pm

Re: [Q] Global variable for all plugins, scripts, etc.

Postby Cat » Sat Apr 18, 2020 12:51 pm

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

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 38 guests