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

#1 2012-12-23 19:50:12

Interest
Scratcher
Registered: 2011-12-25
Posts: 43

variable errors?

adding the following blockspecs:

Code:

            ('%v'                            r    reportVar: '')
            -
            ('set %v to %s'                    -    setVar:to: '' '0')
            ('change %v by %n'                -    changeVar:by: '' 1)

under variables causes the error:
http://i49.tinypic.com/nciclw.png
How do I fix this? I haven't edited my translator scripts, and only this bit causes the error.

Last edited by Interest (2012-12-25 22:10:46)

Offline

 

#2 2012-12-24 03:50:13

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

Re: variable errors?

hmm... it's probably something really obvious, but I don't see the problem since I too use a similar block spec. If you're sure that's the stuff that's causing the error, try replacing the empty strings '' with a dash -

Offline

 

#3 2012-12-24 20:33:24

Interest
Scratcher
Registered: 2011-12-25
Posts: 43

Re: variable errors?

doesn't work. It probably has something to do with

Code:

 defaultArgs:

but I don't know what it might be

Offline

 

#4 2012-12-24 20:44:00

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: variable errors?

This is often the cause of a variable being nil when it shouldn't be. In this case, it is calling the defaultArgs function on an object that doesn't have one. In the debugger, you can look through all of the functions and find what triggered it.


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#5 2012-12-24 21:57:30

Interest
Scratcher
Registered: 2011-12-25
Posts: 43

Re: variable errors?

Error: an attempt was made to send the given message but the receiver does not understand this message. This message is sent by the virtual machine when a message is sent to an object that does not define a method for the message selector."
    "Example: 3 width

Offline

 

#6 2012-12-24 23:07:02

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

Re: variable errors?

Here's how I would solve that:

I haven't looked at the code for #addGenericVariableBlocksTo:x:y:, but I can tell two things (of significance here) from that debugtrace:
1) The message #defaultArgs: was sent to an object that didn't understand it.
2) The object which did not understand it was an instance of Symbol.

Since adding those block specs seemed to cause the error, the Symbol instance which didn't understand #defaultArgs: was probably something in them (or something derived from them). Let's start with the simplest case: the Symbol instance was somewhere inside of those block specs. Scanning through, I see four symbol instances. I'm pretty sure those are valid blocks, so I can eliminate the three specifying the type of each block. That leaves the #- used as a separator. I'm pretty sure that this caused the error.

Time look a the code. I click "Debug" in the error window and get the expanded debugger. I select #addGenericVariableBlocksTo:x:y: from the list, and the editor shows me the error occurred in the first #do: loop. It looks like #blocksFor: is returning the separator symbol unchanged; when it gets passed to the block, it's calling #defaultArgs: and throwing the error.

I know that I've see #- used for separators before, so I'm going to look at how #blocksFor: is used elsewhere and see what's done there. I highlight it (double-click) and browse its senders (n). #viewerPageForCategory: sounds good, so I look through its source. In its #do: loop, it checks for and inserts separators for two symbols: #- for a half space and #~ for a full space. #viewerPageForCategory:'s loop is doing layout like #addGenericVariableBlocksTo:x:y:'s second #do: loop, so I copy that section there (and rename the variables).

That still won't solve the error, though, which was occurring in the first loop. #defaultArgs: just sets the blocks' default arguments; since separators don't have default arguments, I can simply add an #isKindOf: check around that statement. I accept my changes right from the debugger, and refresh the palette. Sure enough, the error's gone.

The final source of #addGenericVariableBlocksTo:x:y: looks like this for me: (yours will probably be different because I assume you're doing this in a mod)

addGenericVariableBlocksTo: page x: x y: startY
    "Add the generic variable blocks to the given page starting at the given y offset. Answer the new y."

    | y vName stage block varBlocks |
    y _ startY.

    "…"

    (self blocksFor: 'variables') do: [:b |
        (b isKindOf: Symbol) ifFalse: [b defaultArgs: (Array with: vName)].
        varBlocks add: b].

    varBlocks do: [:b |
        (b = #-) | (b = #~)
            ifTrue: [
                (b = #-) ifTrue: [y _ y + 15].  "insert a full space"
                (b = #~) ifTrue: [y _ y + 5]]  "insert a half space"
            ifFalse: [
                b color: self variableBlockColor.
                page addMorph: (b position: x@y).
                y _ b bottom + 3]].

    ^ y

EDIT: For future reference, the things you added aren't a script; they're a couple of elements in the blockSpecs array.

EDIT2: I copied and pasted to make this post shorter. In production code, you should abstract that into another method (it's also used in #viewerPageForMotion and possibly some others; I didn't check).

Last edited by nXIII (2012-12-24 23:13:32)


nXIII

Offline

 

#7 2012-12-25 21:16:50

Interest
Scratcher
Registered: 2011-12-25
Posts: 43

Re: variable errors?

doesn't work, creates error:
http://i46.tinypic.com/35a2h08.png
here is the image: http://bit.ly/WFanpS

Offline

 

#8 2012-12-25 23:55:03

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

Re: variable errors?

There's a comment in the middle of that snippet at the end: "…". An ellipsis indicates truncation. I truncated it for three reasons:
1) The method source was really long, and my post was already in danger of becoming a short novel.
2) My method source was based on the original ScratchSourceCode1.4 image. I assumed you had modified yours, so I just showed the relevant parts.
3) I didn't want you to copy-paste it. This is a trivial problem; I could have solved it in under two minutes and just posted the code. Instead, I posted a description of how I solved it in the hopes that next time you had a problem like this, you'd emulate that process and be able to debug something without asking about it on the forums.

If you read my post, you'll probably be able to integrate that snippet at the end into your own method's source code. If you're still not sure, I'd be happy to add screenshots or help you in some other way.  smile  I'm not going to just paste in code, though.

Last edited by nXIII (2012-12-25 23:56:26)


nXIII

Offline

 

#9 2012-12-29 17:47:34

Interest
Scratcher
Registered: 2011-12-25
Posts: 43

Re: variable errors?

1. yes, the #- separator caused the error: removing it solved the problem
2. this is the code, but I don't see the problem with defaultArgs:
http://i48.tinypic.com/24g3p6a.png->http://i49.tinypic.com/211t4p4.png
3. it seems to have disabled all my stage watchers except the normal variable ones.


updated image: http://bit.ly/WFanpS

Offline

 

#10 2012-12-29 19:35:16

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

Re: variable errors?

You forgot the isKindOf: Symbol check.


nXIII

Offline

 

#11 2013-01-03 00:20:30

Interest
Scratcher
Registered: 2011-12-25
Posts: 43

Re: variable errors?

doesn't work, if you look at my updated image fixing that causes a chain reaction, from color: to variableBlockColor

Offline

 

Board footer