def load() doesnt work across multiple files?

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:

def load() doesnt work across multiple files?

Postby decompile » Mon Mar 19, 2018 8:55 am

Hey,

Just noticed that my function on load, which is not the main project file, doesnt get called.

test.py

Syntax: Select all

from . import test_file

def load():
print('Hello World')


test_file.py

Syntax: Select all

def load():
print('Hello Server')


When I try to load the plugin..

Code: Select all

[SP] Loading plugin 'test'...
Hello World
[SP] Successfully loaded plugin 'test'.


Is this intended? Is there a workaround?

My only guess to work this around, would be to add:

test_file.py

Syntax: Select all

def load():
print('Hello Server')
load()


But if there would be a function in load() which gets called instead of print('...'), it would throw most likely a NameError, which would lead that it has to be added at the very end of a file, which looks bad in syntax.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: def load() doesnt work across multiple files?

Postby satoon101 » Mon Mar 19, 2018 11:45 am

That is absolutely intended. SP does not search through every single module to see if there is a load function, only the main one. The same is true with unload.

What you have is of course a workaround, but that will get called only on import of that module, which will happen prior to the primary module's load. You might take a look at GunGame to see how I handle those things to make sure they happen in the correct order instead of on import of each module.
https://github.com/GunGame-Dev-Team/Gun ... ame.py#L44

And yes, in your last example, if you call load prior to defining something that is called within load, it will obviously be a NameError. You would have to move the call itself to the end of the file.
Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: def load() doesnt work across multiple files?

Postby decompile » Mon Mar 19, 2018 12:09 pm

Hey,

Thank you for your answer! Makes sense what you wrote, and also checked out your plugin. (Awesome to see my Idle Manager in it :D)
I havent fully understood it out of the plugin, but you are creating a "on load" function in the import files and call them from the main file load()?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: def load() doesnt work across multiple files?

Postby satoon101 » Mon Mar 19, 2018 4:59 pm

decompile wrote:I havent fully understood it out of the plugin, but you are creating a "on load" function in the import files and call them from the main file load()?

Basically, yes. That way I can control the order in which each of those functionalities occur.
Image
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: def load() doesnt work across multiple files?

Postby Ayuto » Mon Mar 19, 2018 5:43 pm

Alternatively, you can use the OnPluginLoaded listener:
http://wiki.sourcepython.com/developing ... uginloaded

Syntax: Select all

from listeners import OnPluginLoaded
from plugins.manager import plugin_manager

@OnPluginLoaded
def on_plugin_loaded(plugin):
my_plugin = plugin_manager.get_plugin_instance(__name__)
if plugin is not my_plugin:
return

print('Loaded')
You only need to make sure that the file was imported from the main plugin file, so the listener gets registered.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 31 guests