global declaration

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

global declaration

Postby decompile » Thu Apr 09, 2020 6:06 pm

Hey,

Im having a problem with declaring a global variable across modules.

Example:

globals.py

Syntax: Select all

test_variable = None

def set_test_variable(value):
global test_variable
test_variable = value


test.py

Syntax: Select all

from listeners import OnLevelInit
from .globals import set_test_variable

@OnLevelInit
def on_level_init(map_name):
set_test_variable(123)


And when I debug/print the value of the test_variable (after on_level_init), all im getting is None.

Could you please help me?
Last edited by decompile on Thu Apr 09, 2020 6:54 pm, edited 1 time in total.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: global declaration

Postby Ayuto » Thu Apr 09, 2020 6:51 pm

Could you please post the original test code of your test.py? The posted one cannot work, because set_test_variable is not defined in that scope (it should be globals.set_test_variable and that should work fine). My guess is that you did a from globals import * previously?

Nevertheless, I would try to avoid global variables. There are many articles that explain why:
https://www.quora.com/How-can-I-avoid-u ... ython-code
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: global declaration

Postby decompile » Thu Apr 09, 2020 6:56 pm

Ayuto wrote:You could please post the original test code of your test.py? The posted one cannot work, because set_test_variable is not defined in that scope (it should be globals.set_test_variable and that should work fine). My guess is that you did a from globals import * previously?


Oh my bad, I just wrote it in forums.

I imported set_test_variable from globals by:

Syntax: Select all

from .globals import set_test_variable


Syntax: Select all

from listeners import OnLevelInit
from .globals import set_test_variable

@OnLevelInit
def on_level_init(map_name):
set_test_variable(123)



And even doing globals.set_test_variable doesnt overwrite test_variable aswell.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: global declaration

Postby Ayuto » Thu Apr 09, 2020 6:59 pm

And where do you print it?
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: global declaration

Postby decompile » Thu Apr 09, 2020 7:04 pm

in test.py

Syntax: Select all

from events import Event
from .globals import test_variable

@Event('round_start')
def round_start(GameEvent):
print(test_variable)
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: global declaration

Postby decompile » Thu Apr 09, 2020 7:06 pm

If I do the above check in globals.py, it prints the updated value.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: global declaration

Postby Ayuto » Thu Apr 09, 2020 7:22 pm

Syntax: Select all

from .globals import test_variable
If you do this, you have a copy of the value that will be stored in test.test_variable. But your function updates the value at globals.test_variable. Doing this will work:

Syntax: Select all

from events import Event
import globals

@Event('round_start')
def round_start(game_event):
print(globals.test_variable)
It always retrieves the current value from globals.test_variable.

Nevertheless, you should avoid writing to global variables.
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: global declaration

Postby decompile » Thu Apr 09, 2020 7:41 pm

Works now, thank you.

I actually didnt know it only copies the value, instead of getting the "reloaded" one.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: global declaration

Postby Ayuto » Thu Apr 09, 2020 7:59 pm

It's exactly the same like accessing attributes of a class instance.

Syntax: Select all

class Player(object):
def __init__(self):
self.health = 100

player = Player()

# Prints 100
print(player.health)

# Store the player health
current_player_health = player.health

# Set player's health to 50
player.health = 50

# Prints 50
print(player.health)

# Print the stored health (100)
print(current_player_health)

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 16 guests