Another Newbie Q: FIRST use of a global object & survival

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
jwka
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Another Newbie Q: FIRST use of a global object & survival

Post by jwka » Wed Dec 01, 2010 6:11 pm

With the eg.globals.myvar = 100 statement I could set a global var.

But what if I would like to compare it against another value I have and I have not yet set the global var to any value?

in PHP there is the is_set() function. Anyway to do similar in python like eg.globals.myvar.exist = true?

Next question would be:

How can I make sure a defined and well set variable (or object in general) would be available next time I start EG? Do I need to "unload" into and "load" from a file?

Thanks!
jwka

stottle
Plugin Developer
Posts: 636
Joined: Sun Apr 26, 2009 10:59 pm

Re: Another Newbie Q: FIRST use of a global object & survival

Post by stottle » Wed Dec 01, 2010 8:42 pm

In terms of checking for variables, that depends on the variable's type. The most generic method is simply to use a try/except block:

Code: Select all

try:
    x = SomeClass.myVar
except:
    pass #whatever you need to do if the variable doesn't exist
eg.globals (which Bitmonster tells people never to use BTW) is a kind of strange type, like a modified dictionary. For dictionaries, you could use has_key, or the get method. So an example (again because eg.globals is strange) would be:

Code: Select all

eg.globals.__dict__.has_key("myvar")
Saving variables between sessions uses eg.PersistentData. Check out this thread.

Brett

jwka
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Re: Another Newbie Q: FIRST use of a global object & survival

Post by jwka » Thu Dec 02, 2010 12:32 am

Thanks, Brett, will follow the links & try out your suggestions.

And ... you mentionned that bitmonster strongly recommends NOT to use eg.globals, what would be the alternative to that defining variables / objects to be used in multiple scripts?

rgds
jwka

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Another Newbie Q: FIRST use of a global object & survival

Post by Pako » Thu Dec 02, 2010 8:11 am

stottle wrote:eg.globals (which Bitmonster tells people never to use BTW) ...
That is utter nonsense, it's completely different:
Bitmonster wrote:No plugin should ever assign something to eg.globals or change anything in it. Never!

The eg.globals namespace is exclusive for the user's Python scripts and commands.
If your plugin wants to publish variables to the user, the preferred way is to implement getter actions, that simply return the value. This way the user can also easily see all informations your plugin can provide.
Pako

jwka
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Re: Another Newbie Q: FIRST use of a global object & survival

Post by jwka » Thu Dec 02, 2010 8:28 am

Thanks Pako for making that clear.

I guess Brett might have mixed things up a bit.

jwka

stottle
Plugin Developer
Posts: 636
Joined: Sun Apr 26, 2009 10:59 pm

Re: Another Newbie Q: FIRST use of a global object & survival

Post by stottle » Thu Dec 02, 2010 3:09 pm

My bad, I do more plugin development than scripts, and I remembered the "Never!" of Bimonster's comment, not that it was specific to plugins. Sorry for the confusion.

Brett

Post Reply