3

My goal is to build a 5x5 grid of images. In the following code, row, col and rowcol were created as variables local to the sprite, and newcol, newrow and cats are global. (By the way, is it possible to tell which variables are local and which are global? It's easy to forget or make mistakes.)

code

The result is a 5x1 grid only, as seen here.

results

I am unclear as to the order of execution of these statements. Does when I start as a clone get called before or after add_cat gets called the second time? My tentative conclusion is that it gets called afterwards, yet the clone's global variables seem to contain their values from beforehand instead.

When I attempted to debug it with ask and say and wait commands, the results varied wildly. Adding such pauses in some places fixed the problem completely, resulting in a 5x5 grid. In other places, they caused a 1x5 grid.

The main question is: How to fix this so that it produces a 5x5 grid?

Jim K
  • 12,824
  • 2
  • 22
  • 51

1 Answers1

5

Explanation

Unfortunately, the execution order in Scratch is a little bizarre. Whenever you edit a script (by adding or removing blocks, editing inputs, or dragging the entire script to a new location in the editor), it gets placed at the bottom of the list (so it runs last).

A good way to test this out is to create a blank project with the following scripts:
two similar scripts

When you click the green flag, the sprite will either say "script one" or "script two", depending on which runs first. Try clicking and dragging one of the when green flag clicked blocks. The next time you click the green flag, the sprite will say whichever message corresponds to the script you just dragged.

This crazy order can make execution incredibly unpredictable, especially when using clones.

The solution

The only real solution is to write code that has a definite execution order built-in (rather than relying on the whims of the editor). For simpler scripts, this generally means utilizing the broadcast and wait block to run particular events in the necessary order.

For your specific project, I see two main solutions:

Procedural Solution This is the most straightforward script, and it's probably what I would choose to go with:
procedural solution
(row and col are both sprite-only variables)
Because clones inherit all sprite-only variable values when they are created, each clone will be guaranteed to have the correct row and col when it is created.

Recursive Solution This solution is a bit harder to understand than the first, so I would probably avoid it unless you're just looking for the novelty:
recursive solution

PullJosh
  • 735
  • 7
  • 21
  • 2
    Good explanation, setting me straight on a couple of points about how Scratch works. Something similar to your recursive solution worked in my project. Note that recursion offers more than just novelty. My actual project requires multiple branches which is not possible with a simple procedural approach. It could be done by using a list as a first-in-first-out stack, but that is at least as complex as using recursion, and less elegant. – Jim K Mar 24 '17 at 05:19