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.

Help "memory flag"

If you have a question or need help, this is the place to be.
Post Reply
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Help "memory flag"

Post by krambriw »

Hi, this simple example shows how I try to keep a memory in run-time, but it doesnt work :oops:

I only want to print the first time the script runs through the while loop

Code: Select all

i = True

def MyFunc():
    if i == True:
        print "ok to print"
        i = False


ind = 1
while ind < 5:
    MyFunc()
    ind = ind + 1
Thanks a lot, Walter
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Help "memory flag"

Post by Pako »

Maybe like: http://www.eventghost.org/forum/viewtopic.php?f=2&t=496

First script (in Autostart for example):

Code: Select all

eg.globals.i=True
def MyFunc():
    if eg.globals.i == True:
        print "ok to print"
        eg.globals.i = False
        
eg.globals.MyFunc = MyFunc
And second script:

Code: Select all

ind = 1
while ind < 5:
    eg.globals.MyFunc()
    ind = ind + 1
Pako
User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Re: Help "memory flag"

Post by Bitmonster »

Everytime you execute a script ... well... you actually execute it. So if you write "i = True", then you set "i" to True on every execute, so the result isn't a surprise. But all global variables in the script live as long as the script lives and that is as long a you didn't edit or delete it.

To get the result you want, you can also use another Python trick. The first time your script starts execution, there is surely no "i" variable defined, but on the next execution "i" will be defined.

So you can write:

Code: Select all

try:
    i
except NameError:
    i = 0

print i
i += 1
The first reference to "i" (a line with just "i" will only reference this variable and nothing more) will fail on the first execution, because "i" is not defined, so Python will raise a NameError exception. On the catching code we will then set "i" to the initialization value. The next time the script is executed, "i" is defined and no exception will be raised. So "i" will still hold its last value.
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Help "memory flag"

Post by krambriw »

Once again, thanks!

Walter
Post Reply