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

#1 2010-12-06 12:43:46

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

C loop block code?

Hi all,

Since I've started messing around with the smalltalk code, I think I've developed a small understanding of how to make blocks, which I know isn't much, but I've heard other people ask this question too, so thought I would post a thread for it.

how do you get a C block to do something after the contents has been run? Specifically, I want to write text to a file once a loop starts and again when the loop ends so that rather than a block called <html> with the code

self writeText: '<html>' toFile: 'C:\Program Files\output\output.html'

and a second block called <html /> with the code

self writeText: '<html />' toFile: 'C:\Program Files\output\output.html'

I can just have a loop block that writes the first message at the start, and the second when the loop finishes. Html is just an example, I plan to use it to create blocks for programming microships, but you get the idea  smile

any thoughts as to how this is done? Is it possible in Panther?


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#2 2010-12-06 14:05:06

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: C loop block code?

| block args |
block _ stackFrame expression.
args _ stackFrame arguments.
(args size = 1)
  ifFalse: [
    writeToFile: '<html>'.
    self pushStackFrame: (ScratchStackFrame new expression: (block firstBlockList)).
    stackFrame addArgument: 'temp'.]
  ifTrue: [
    writeToFile: '</html>'.
    self popStackFrame.]

Replace writeToFile: with your file writing method.

(This works, I used it in my mod of Web Blox)


/* No comment */

Offline

 

#3 2010-12-06 14:49:07

Daffy22
Scratcher
Registered: 2008-12-15
Posts: 500+

Re: C loop block code?

TheSuccessor wrote:

| block args |
block _ stackFrame expression.
args _ stackFrame arguments.
(args size = 1)
  ifFalse: [
    writeToFile: '<html>'.
    self pushStackFrame: (ScratchStackFrame new expression: (block firstBlockList)).
    stackFrame addArgument: 'temp'.]
  ifTrue: [
    writeToFile: '</html>'.
    self popStackFrame.]

Replace writeToFile: with your file writing method.

(This works, I used it in my mod of Web Blox)

So your modding web blox  tongue . Feel free to but please give credit.  big_smile


http://img201.imageshack.us/img201/1784/logosmalle.png
"Spectacular - 5 Star" -  CNET.com Editor.

Offline

 

#4 2010-12-06 15:25:49

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: C loop block code?

thanks! One problem, I tried it in Panther and it failed. (as a custom block)

here's my code:

Code:

| block args |
block _ stackFrame expression.
args _ stackFrame arguments.
(args size = 1)
  ifFalse: [
    writeText: 'if ', t1, 'then','
' ToFile: 'picaxe out.bas'.
    self pushStackFrame: (ScratchStackFrame new expression: (block firstBlockList)).
    stackFrame addArgument: 'temp'.]
  ifTrue: [
     writeText: 'endif'
' ToFile: 'picaxe out.bas'.
    self popStackFrame.]

have I made a mistake?


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#5 2010-12-08 11:48:37

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: C loop block code?

Oh. It won't work in CYOB I don't think, as it runs in Scratch-Execution Engine => ScratchThread, where the execution of blocks is actually done. This is where all C blocks have their code as they tend to affect the way the script runs.


/* No comment */

Offline

 

Board footer