Initialise all sprite local variables

To make debugging easier, I'd like to reset/initialize my sprite local variables to 0 at the start of a script but I can't find how to get a reference to them.

Any ideas Snappers?

Hi @cymplecy

Take a look at this test project I created with tools from "Create variables" library:

The trick is: you create a special list of sprite's variables names. The drawback: you have to create variables using special block that adds their names to this list

@pavelbel Like it :slight_smile:

I decided to try and see if there was anything in the Snap! API that could help but saw that you can only get hold of the names of the global vars at the moment

But then I noticed that @jens had added a function, last month, to return the whole project as xml :slight_smile:

And then I was VERY lucky when I just tried splitting the project xml by the name of my sprite (I was just trying to isolate the code for the sprite) and looked what popped out! :slight_smile:


I should be able to easily get all the sprite local vars out of this :slight_smile:

Look for the thing in between the 2 and 3 double qoute.


Mission accomplished :slight_smile:


Code wrapped up as custom block so it contains library dependencies

<blocks app="Snap! 6," version="1"><block-definition s="Initialise Local Variables" type="command" category="other"><header></header><code></code><translations></translations><inputs></inputs><script><block s="doForEach"><l>sprite local variable name</l><custom-block s="pipe %s $arrowRight %mult%repRing"><block s="reportTextSplit"><block s="evaluate"><block s="reportJSFunction"><list><l></l></list><l>var ide = world.children[0];&#xD;var project = ide.getProjectXML();&#xD;return (project);</l></block><list></list></block><block s="reportGet"><l><option>name</option></l></block></block><list><block s="reifyReporter"><autolambda><block s="reportKeep"><block s="reifyPredicate"><autolambda><block s="reportEquals"><custom-block s="substring of %txt from position %n to position %n inclusive"><l></l><l>1</l><l>6</l></custom-block><l>" var=</l></block></autolambda><list></list></block><l/></block></autolambda><list></list></block><block s="reifyReporter"><autolambda><block s="reportTextSplit"><l></l><l>"</l></block></autolambda><list></list></block><block s="reifyReporter"><autolambda><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportListItem"><l>3</l><l/></block></autolambda><list></list></block><l/></block></autolambda><list></list></block></list></custom-block><script><custom-block s="set var %s to %s"><block var="sprite local variable name"/><l>not set</l></custom-block></script></block></script></block-definition><block-definition s="substring of %&apos;str&apos; from position %&apos;start&apos; to position %&apos;end&apos; inclusive" type="reporter" category="operators"><comment x="0" y="0" w="297.3333333333333" collapsed="false">Reports the portion of the first input (string) starting at the position given by the second input (counting from 1, like LETTER n OF) and ending at the position given by the third input (also counting from 1).  If the third input is empty, reports the portion from the first position to the end of the string.&#xD;If a position number is negative, counts from the end backward, so -1 is the last character, -2 is the next to last, etc.</comment><header></header><code></code><translations>pt:o texto de _ entre as posições _ e _ , inclusive&#xD;</translations><inputs><input type="%txt"></input><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>str</l><l>start</l><l>end</l></list><l>if (end != "") {&#xD;    return str.slice(start-1,end);&#xD;} else {&#xD;    return str.slice(start-1);&#xD;}</l></block><list><block var="str"/><block var="start"/><block var="end"/></list></block></block></script></block-definition><block-definition s="pipe %&apos;value&apos; $arrowRight %&apos;pipe&apos;" type="reporter" category="control"><header></header><code></code><translations>pt:a aplicação a _ de _&#xD;</translations><inputs><input type="%s"></input><input type="%mult%repRing"></input></inputs><script><block s="doIf"><block s="reportListIsEmpty"><block var="pipe"/></block><script><block s="doReport"><block var="value"/></block></script></block><block s="doReport"><custom-block s="pipe %s $arrowRight %mult%repRing"><block s="evaluate"><block s="reportListItem"><l>1</l><block var="pipe"/></block><list><block var="value"/></list></block><block s="reportCDR"><block var="pipe"/></block></custom-block></block></script></block-definition><block-definition s="set var %&apos;name&apos; to %&apos;value&apos;" type="command" category="variables"><comment x="0" y="0" w="240.4931640625" collapsed="false">This block sets the given value (last input) to the variable named with the name givent (var input).&#xD;&#xD;It looks for that variable following the scope order &apos;script&apos; -&gt; &apos;sprite&apos; -&gt; &apos;global&apos; (the first match it finds).&#xD;&#xD;If that variable does not exist (in any scope) an error happens, stopping their script. You can check it before using the &quot;does var (name) exists?&quot; block. </comment><header></header><code></code><translations>ca:assigna a _ el valor _&#xD;es:asignar a _ el valor _&#xD;pt:altera _ para _&#xD;de:setze var _ auf _&#xD;</translations><inputs><input type="%s"><options>§_getVarNamesDict</options></input><input type="%s"></input></inputs><script><block s="doRun"><block s="reportJSFunction"><list><l>varName</l><l>value</l><l>proc</l></list><l>if ((typeof varName) != "string") {&#xD;    throw new Error("name isn&apos;t a string: " + varName);&#xD;}&#xD;proc.homeContext.variables.setVar(varName, value);</l></block><list><block var="name"/><block var="value"/></list></block></script></block-definition></blocks>

Means I can step thru my project and know when/what each var is set to rather that be confused with what state they were in on the last run


Ah, I thought it's kinda cheat to use JS directly :slight_smile:
If so, you don't have to export the whole project. Export just current sprite:
var ide = world.children[0]; var str = ide.serializer.serialize(this); return str;

I think using @jens official API is one use of JS that's allowable :slight_smile:

That would take me over the line :slight_smile:

I understand :slight_smile: But it is so simple... While exporting of the full project could be rather slow... :roll_eyes:

But we can't rely on using internal Snap! code - it could change at any time without warning

But is we only use the API, then we can ask for our money back if they change :slight_smile:

Re slow - since this is for debugging - speed isn't an issue as I'm single-stepping thru the project :slight_smile:

And maybe one day, @jens will add the ability in the API to just retrieve a sprites local variables as well as the global ones

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.