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
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
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
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…
Offline
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
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)
Offline
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
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
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).
Offline
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
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.
Offline