Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Variable scopes

If you have a question or need help, this is the place to be.
Post Reply
easy
Posts: 46
Joined: Wed Dec 05, 2007 10:05 pm

Variable scopes

Post by easy »

Let's say there is code like this:

Code: Select all

class MyPlugin(eg.PluginClass):
    def __init__(self):
        self.AddAction(myAction)

    def __start__(self,myPluginVarFromConfigure):
        self.myPluginVarFromConfigure=myPluginVarFromConfigure

class myAction(eg.ActionClass):
    def __call__(self, myActionVarFromConfigure):
        self.myActionVarFromConfigure=myActionVarFromConfigure
        self.other=Other(self)

class myOtherAction(eg.ActionClass):
    def __call__(self, myOtherActionVarFromConfigure):
        self.myOtherActionVarFromConfigure=myOtherActionVarFromConfigure

class Other():
    def __init__(self):
        self.myOtherVar="test"
How does the datatree look like? It may be strange question, but I am already out of good ideas on how to access my data...
If I'd like to access myPluginVarFromConfigure in myAction I'd use self.plugin.myPluginVarFromConfigure. How do I access myPluginVarFromConfigure from within Other?
Is it true, that to access myOtherVar from within myAction I'd use self.other.myOtherVar?
And most important how do I access myOtherActionVarFromConfigure from within myAction?
User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Re: Variable scopes

Post by Bitmonster »

easy wrote: If I'd like to access myPluginVarFromConfigure in myAction I'd use self.plugin.myPluginVarFromConfigure. How do I access myPluginVarFromConfigure from within Other?
You did it partly right, by giving Other() a "self" as parameter. Other() is a completely free instance of a class, so you have to design yourself some parameter that can be used to access something else. The Other.__init__ is missing such parameter in the definition, so the right way would be:

Code: Select all

class Other:
    def __init__(self, action):
        self.action = action
        self.action.plugin.myPluginVarFromConfigure = "whatever"

class MyAction(eg.ActionClass):
    def __call__(self):
        self.other = Other(self)
easy wrote:Is it true, that to access myOtherVar from within myAction I'd use self.other.myOtherVar?
Yes.
easy wrote:And most important how do I access myOtherActionVarFromConfigure from within myAction?
Ok, this is more complicated and has actually changed some versions ago. I will write some note about it, if the new structure is more fixed. But most developers simply avoid it, by using self.plugin as a container to hold all variables that might need to be accessed by different actions (just like the self.counter example in the tutorial).
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!
Post Reply