Page 1 of 1

def load() doesnt work across multiple files?

Posted: Mon Mar 19, 2018 8:55 am
by decompile
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.

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

Posted: Mon Mar 19, 2018 11:45 am
by satoon101
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.

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

Posted: Mon Mar 19, 2018 12:09 pm
by decompile
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()?

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

Posted: Mon Mar 19, 2018 4:59 pm
by satoon101
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.

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

Posted: Mon Mar 19, 2018 5:43 pm
by Ayuto
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.