Page 1 of 1

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

Posted: Wed Dec 01, 2010 6:11 pm
by jwka
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

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

Posted: Wed Dec 01, 2010 8:42 pm
by stottle
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

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

Posted: Thu Dec 02, 2010 12:32 am
by jwka
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

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

Posted: Thu Dec 02, 2010 8:11 am
by Pako
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

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

Posted: Thu Dec 02, 2010 8:28 am
by jwka
Thanks Pako for making that clear.

I guess Brett might have mixed things up a bit.

jwka

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

Posted: Thu Dec 02, 2010 3:09 pm
by stottle
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