This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-02-10 20:13:26

rpglurker
New Scratcher
Registered: 2013-01-17
Posts: 24

Persistent data within a function (like static)

Does Scratch/BYOB allow you to persist data local to a function (like a static data member)?  For example, I want to define a list within a function and then when that function is called again the content of the list is again accessible to the function...

Offline

 

#2 2013-02-10 20:52:47

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Persistent data within a function (like static)

Take advantage of lexical scope:

<script variables (a)>
<report <the script:
    -- do something with "a" when called
>>

Last edited by nXIII (2013-02-10 20:53:18)


nXIII

Offline

 

#3 2013-02-19 13:05:04

rpglurker
New Scratcher
Registered: 2013-01-17
Posts: 24

Re: Persistent data within a function (like static)

Can you provide an example?

Creating a local script variable does not allow the value to persist when I call the function again later.  I want to have the script retain the value a variable on subsequent calls...

Thanks

Offline

 

#4 2013-02-19 15:48:40

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Persistent data within a function (like static)

No programming language I know allows that kind of behaviour: when a function ends, the values of all variables declared within the function are trashed.
Likewise in Scratch/BYOB you will have to use local or global variables declared in the "variables" tab if you want the values to persist across function instances.

Offline

 

#5 2013-02-19 16:29:20

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Persistent data within a function (like static)

LS97 wrote:

No programming language I know allows that kind of behaviour: when a function ends, the values of all variables declared within the function are trashed.

Object properties (aka fields aka instance variables) and C's static variables come to mind…


nXIII

Offline

 

#6 2013-02-19 18:24:05

rpglurker
New Scratcher
Registered: 2013-01-17
Posts: 24

Re: Persistent data within a function (like static)

exactly....like a static variable in C....there is only one static copy of the variable used so each time you enter the function that same memory location continues to have the same value it had when the function exited...

This is actually why I mentioned "like static" in the title...

Offline

 

#7 2013-02-19 19:01:17

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Persistent data within a function (like static)

nXIII wrote:

Take advantage of lexical scope:

<script variables (a)>
<report <the script:
    -- do something with "a" when called
>>

This is the solution…

(or just use a global variable)


nXIII

Offline

 

#8 2013-02-20 06:08:57

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Persistent data within a function (like static)

What I understood from the OP was that rpglurker wants a variable to be declared inside a function that keeps it value for the next time it runs.

What I'm seeing here as a proposed solution is a variable that is declared inside a class and that is a single value that carries across instances and can be accessed from outside the class (like a static or Shared variable).

What did I miss?

Offline

 

#9 2013-02-20 17:01:04

rpglurker
New Scratcher
Registered: 2013-01-17
Posts: 24

Re: Persistent data within a function (like static)

unfortunately, if I have to declare a variable that is local to the class/sprite and not within the function that complicates things for me...I was really hoping for a static variable within a function that would retain its value for the next time the function was called.  But if that is only way to do it, I will create some "scratch pad" variables that can be used by the function and will persist between calls.

Is there a way to create an object that is not a sprite but just a container for associated methods and data members that can be passed to a function (like a class in other languages)?

Thanks for the input...

rpglurker

Offline

 

#10 2013-02-20 17:32:08

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Persistent data within a function (like static)

rpglurker wrote:

unfortunately, if I have to declare a variable that is local to the class/sprite and not within the function that complicates things for me...I was really hoping for a static variable within a function that would retain its value for the next time the function was called.  But if that is only way to do it, I will create some "scratch pad" variables that can be used by the function and will persist between calls.

Is there a way to create an object that is not a sprite but just a container for associated methods and data members that can be passed to a function (like a class in other languages)?

Thanks for the input...

rpglurker

In BYOB, use a closure (like in my example above).


nXIII

Offline

 

#11 2013-02-22 10:54:45

rpglurker
New Scratcher
Registered: 2013-01-17
Posts: 24

Re: Persistent data within a function (like static)

The problem with a closure is that I actually have to relinquish control, and not just to an outer function...There are two control programs which are called from a master control program.  Each control program needs to have independent data (local) and needs to "exit" at the end of its turn... when the MCP calls the control program again, it needs to be able to access persistent data.

I have used local data members and changed the way I am importing the sprites to make using local data members easier... I believe the problem is solved for now... Thanks for your input...

rpglurker

Offline

 

#12 2013-02-22 11:29:44

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Persistent data within a function (like static)

rpglurker wrote:

The problem with a closure is that I actually have to relinquish control, and not just to an outer function...There are two control programs which are called from a master control program.  Each control program needs to have independent data (local) and needs to "exit" at the end of its turn... when the MCP calls the control program again, it needs to be able to access persistent data.

I have used local data members and changed the way I am importing the sprites to make using local data members easier... I believe the problem is solved for now... Thanks for your input...

You can still use a closure. At the beginning, before you run your "control programs," set them to closures from a script with script variables. Each time you then CALL or RUN the closure, it will have access to those script variables in its scope.


nXIII

Offline

 

Board footer