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

#1 2011-07-29 09:56:59

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

"Communicating to Scratch via ActionScript" variables problem

Hi,

I'm trying out the "Communicating to Scratch via ActionScript" code from http://wiki.scratch.mit.edu/wiki/Commun … tionscript

It works perfect with one sensor (variable) but with more it only catches the first value.

This is what I trace:

Code:

sensor-update "position" -166.0 "var2" -165.0 "var3" -164.0 
Scratch set position to -166.0
sensor-update "position" -164.0 "var2" -163.0 "var3" -162.0 
Scratch set position to -164.0
sensor-update "position" -162.0 "var2" -161.0 "var3" -160.0 
Scratch set position to -162.0

So "var2" and "var3" are completely ignored.

If I try this code:

Code:

result=sensor_num_pattern.exec(data)

if(result != null){
        trace("=== num_pattern");
        for(var i=0;i<result.length;i++)
        {    trace(i+" "+result[i]);
        }
        
        //updated(result[1],result[2])
}

I'll trace this:

Code:

sensor-update "position" -5.0 "var2" -4.0 "var3" -3.0 
=== num_pattern
0 sensor-update "position" -5.0
1 position
2 -5.0

I'm trying to understand the regular expressions, but thats kind of hard.
However this regular expression doesn't seem to work for more values:

Code:

var sensor_num_pattern:RegExp=/sensor-update\s\"(.*?)\"\s([-|\d|.]+)/;

I'm studying this code because I want to make a bridge to OSC (open sound control) with Processing.

Thanks in advance.

My test Scratch project:
http://scratch.mit.edu/projects/kka02/1950247
Regular expression test:
http://regexr.com?2ubg3

Offline

 

#2 2011-07-29 17:52:42

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

It isn't ignoring var2 and var3, it's just updating it on the same line.
This:

Code:

sensor-update "position" -5.0 "var2" -4.0 "var3" -3.0

Is the same as:

Code:

sensor-update "position" -5.0
sensor-update "var2" -4.0 
sensor-update "var3" -3.0

smile

Offline

 

#3 2011-07-30 12:03:45

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

Re: "Communicating to Scratch via ActionScript" variables problem

No, I see that its in the same line.

But when I read out the result array it only gives me the "position" variable values. So "var2" and "var3" are not stored in the array.

This is what I trace

Code:

=== num_pattern
0 sensor-update "position" -5.0
1 position
2 -5.0

And this something what I would expect:

Code:

=== num_pattern
0 sensor-update
1 position
2 -5.0
3 var2
4 -4.0
5 var3
6 -3.0

I now only can get -5.0 from the array, but where are -4.0 and -3.0 ?

Offline

 

#4 2011-07-30 13:33:44

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

If you are parsing the lines and having it display that stuff then you are only parsing the first section of the line ( sensor-update "position" -5.0 ) and ignoring the last bit ( "var2" -4.0 "var3" -3.0 ). Check your parser and see about modifying it to detect if it's 3 updates or just one.

Offline

 

#5 2011-07-30 17:37:00

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

Re: "Communicating to Scratch via ActionScript" variables problem

The problem that its not my parser and the Regular Expression stuff to parse everything is kind of hard to understand. Maybe the developer is active on this forum? Also to notify that it doesn't seem to work completely (or I can be wrong of course).

Offline

 

#6 2011-07-30 17:40:59

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

I'm not entirely sure, but I think Archmage made the ActionScript code for connecting to Scratch. Might want to ask him. ( http://scratch.mit.edu/users/archmage )

Offline

 

#7 2011-07-30 18:12:58

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

Re: "Communicating to Scratch via ActionScript" variables problem

Maybe a dumb question. But how can I contact someone? Because there don't seem any link to email, messagebox or something on the userpage. Or do I have to add a person as a friend.

Offline

 

#8 2011-07-31 03:13:23

johnnydean1
Scratcher
Registered: 2010-02-12
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

Just comment on one of their projects. Something like:
@Archmage
Can you help me, I am having problems with...


You can now reach me on Twitter @johnnydean1_

Offline

 

#9 2011-07-31 04:19:17

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

Re: "Communicating to Scratch via ActionScript" variables problem

kka02 wrote:

Maybe a dumb question. But how can I contact someone? Because there don't seem any link to email, messagebox or something on the userpage. Or do I have to add a person as a friend.

Not a dumb question at all!
Scratch's policy doesn't allow emails to be shared, and the Team will categorically refuse to host any chat rooms (If you like some reading, the reason's number 6 here).

So, either you arrange a chat on another site, or share Skype/Hamachi accounts (preferably not publicly) and chat there. Otherwise, for short stuff, you can use comments as JD1 pointed out.

Offline

 

#10 2011-07-31 08:04:41

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

Re: "Communicating to Scratch via ActionScript" variables problem

I've also tested the AS3 class but it has the same problem:
http://scratch.mit.edu/forums/viewtopic … 86#p106086

I'll contact Fullmoon and Chalkmarrow.

Offline

 

#11 2011-07-31 16:23:24

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

I see the problem...and it's a little trickier to solve than you might expect. Initially I thought I could solve the problem by eliminating regexes altogether and using a statement like this:

Code:

if( data.indexOf('sensor-update') == 0 )
{
  var bits:Array = data.slice('sensor-update'.length + 1).split(" ")
}

This would produce an array containing each bit of the sensor-update statement that is separated by a space. However, it would also catch any spaces inside of string literals that Scratch sends, so that's out. It appears I'll have to break out the AS3 syntax parsing library I wrote a while back...give me a little bit and I should be able to come up with a solution.


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#12 2011-08-01 04:43:49

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

Re: "Communicating to Scratch via ActionScript" variables problem

Thanks for your quick reply. Seems logical to split on spaces.

This is what I trace with more variables and a string.

Code:

ScratchSocket.evaluate(sensor-update "position" -5.0 "strt" "hi there" "var2" -4.0 "var3" -3.0 sensor-update "position" -7.0 "var2" -6.0 "var3" -5.0 sensor-update "position" -9.0 "var2" -8.0 "var3" -7.0 )

Another problem is that sometimes you get more sensor-updates in one string (like above).

Offline

 

#13 2011-08-01 09:23:57

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

After some experimenting with Python ( only other language I can program in  tongue  ) When you first connect to Scratch, it send the variables all on a single line. So, if you set it up, so when you click the green flag, it resets all the variables one by one, it should send the messages one by one. So you can get the correct results.  smile

Offline

 

#14 2011-08-01 13:51:36

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

Re: "Communicating to Scratch via ActionScript" variables problem

@Magnie: yes but I want it all the time, not after a reset.

@fullmoon
A friend helped me with the regex. Below a zip-file with a Processing Sketch we're the sensor-values seem to work fine. I've put my Scratch test application in it as well. Only broadcast messages don't work good yet. Hope this helps you for the Actionscript version.

The regex string needs to be converted in order to work with Flash..

[link]http://dl.dropbox.com/u/1147392/scratch_regex.zip[/link]

Offline

 

#15 2011-08-01 19:49:56

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

I'll see what I can do tonight! Sorry, I was gone most of yesterday.


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#16 2011-08-01 23:52:51

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

Here's your new getData() function:

Code:

function getData():void
{
    var data:String="";
    while (sock.bytesAvailable)
    {
        data+=sock.readUTF();
    }
    var broadcast_pattern:RegExp=/broadcast\s\"(.*)"/;
    var sensor_pattern:RegExp=/sensor-update\s\"(.*)\"\s\"(.*)\"/;
    var sensor_num_pattern:RegExp=/sensor-update\s\"(.*?)\"\s([-|\d|.]+)/;
    var result:Object;
    //See those parentheses in the regexes above? The values within those are actually stored
    //in the 'result' object below at indices 1,2,etc.
    result=broadcast_pattern.exec(data);
    if (result!=null)
    {
        //It's a broadcast! Call the broadcasted() function with the name of the broadcast
        broadcasted(result[1]);
    }
    
    if(data.indexOf('sensor-update') == 0)
    {
        var a:Array = P@rseUtil.split(" ",data.slice('sensor-update'.length + 1))
        for(var i:uint = 0;i<a.length - 1;i+=2)
        {
            var key:String = a[i];
            key = key.slice(1,key.length - 1)
            var val:String = a[i+1];
            if(val.charAt() == '"')
            {
                val = val.slice(1,val.length - 1)
            }
            updated(key,val)
        }
    }
}

You'll need the StringUtil and P@rseUtil classes (one is from as3corelib, the other is mine). You can download them here. Let me know how that works for you.

Edit: it appears that the filter doesn't like the word "p@rse" so I've replaced the "a" with an @ sign in several places.

Last edited by fullmoon (2011-08-01 23:55:58)


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#17 2011-08-08 10:22:49

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

Another thins that you can try (I did this) is to put a wait block that has a value of like a billionth of a second in between changing variables. It worked for me.


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#18 2011-08-15 12:44:54

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

Re: "Communicating to Scratch via ActionScript" variables problem

Thanks. I'll look into it. Just back from holiday.

Offline

 

#19 2011-08-23 17:26:00

kka02
Scratcher
Registered: 2010-08-30
Posts: 11

Re: "Communicating to Scratch via ActionScript" variables problem

Hi All,

I've created a Scratch-OSC (Open Sound Control) bridge in Processing. You can run it as a standalone application as well. See http://code.google.com/p/osc-tools/

I'll make a tutorial later.

@Fullmoon: I'll test your changed Flash application later.

Offline

 

#20 2012-04-25 04:46:56

comix6
Scratcher
Registered: 2011-12-20
Posts: 10

Re: "Communicating to Scratch via ActionScript" variables problem

Hi,

Can I ask a question? How do I get started understanding these posts?  What sort of compiler or whatever are you all talking about?

I got Flash CS4 and tried putting PlayerOnly.swf on the 'stage' so I could communicate with it by actionscript but this doesn't work (error 1009 stage is null, it starts running before added to stage but does not fire INIT or COMPLETE events).

So I presume you are not talking about using Flash CS4 to communicate with the .swf file

Are you communicating with the .swf file at all? Do you have an actionscript player for .sb files?

I just do not understand, and the wiki about it just says get something that can compile actionscript. It does not say how to interact with an .sb file.

Do I use PlayerOnly.swf to read the .sb file? If not, what is the first thing to do to get actionscript or Javascript to interact with the .sb file?

The aim is to be able to answer my kid's question, he wants his games to be levels in one game.

Offline

 

#21 2012-04-25 06:04:07

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

comix6 wrote:

Hi,

Can I ask a question? How do I get started understanding these posts?  What sort of compiler or whatever are you all talking about?

I got Flash CS4 and tried putting PlayerOnly.swf on the 'stage' so I could communicate with it by actionscript but this doesn't work (error 1009 stage is null, it starts running before added to stage but does not fire INIT or COMPLETE events).

So I presume you are not talking about using Flash CS4 to communicate with the .swf file

Are you communicating with the .swf file at all? Do you have an actionscript player for .sb files?

I just do not understand, and the wiki about it just says get something that can compile actionscript. It does not say how to interact with an .sb file.

Do I use PlayerOnly.swf to read the .sb file? If not, what is the first thing to do to get actionscript or Javascript to interact with the .sb file?

The aim is to be able to answer my kid's question, he wants his games to be levels in one game.

They're talking about communicating with the standalone (downloaded) Scratch program using remote sensor connections. I think they're trying to write Actionscript code that can connect to Scratch in this way.  smile

I don't think you can do what you're trying to do.  hmm


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#22 2012-04-25 06:07:11

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

If you're trying to take some Flash applications and string them together into a big Scratch program, sorry, you can't do that (yet! Scratch 2.0 will be written in Flash, so just maybe...).  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#23 2012-04-25 06:09:50

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

Also, if you're trying to do it the other way around: i.e. put Scratch games together into a big Flash project, then there are two ways. The "easy" way is to embed the Flash player in an HTML page with the projects plying in succession. I can do that for you. The "hard" way is to read the project's contents, then recreate it in Flash. You should ask blob8108 (the above poster) for help there, he's a file format whiz. Good luck!


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#24 2012-04-25 17:14:31

comix6
Scratcher
Registered: 2011-12-20
Posts: 10

Re: "Communicating to Scratch via ActionScript" variables problem

>with the projects plying in succession. I can do that for you.

Yes that is almost exactly it. I answered on the wiki but here I'll answer in more detail. I am feeling like Victor and his friends including dylly500 are in some sense 'trapped' on the Scratch website. I encouraged them to unhide file extensions, to use cmd and command line, and ftp, generally to get out from under Microsoft control and become members of the online community.

As it is, scratch games are sort-of trapped there on the scratch website, sort of getting trapped in the MIT community instead. They can more or less make javascript games too, but very constrained by problems typing and organizing text,  and yet there there is more creativity because if you have a div or  a textarea or whatever, it is not isolated, you can get the value or innerHTML whenever you like.

Once a scratch project is done, it is sort-of frozen and it can't talk to other scratch projects like how a div can talk to a div etcetera. Yet it seems that experts do get them to be able to communicate with stuff.

I want to be careful not to request any change in scratch that would turn it into a business tool, but it is not general purpose enough as a learning tool the way it is. And that is because scratch games can't communicate with each other.

What would be ideal would be if a kid could have two scratch projects on a website and when a sprite on one is clicked, a character on another says 'Hello World.'

So they are not so isolated one from the other.

Offline

 

#25 2012-04-26 02:43:41

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: "Communicating to Scratch via ActionScript" variables problem

comix6 wrote:

I am feeling like Victor and his friends including dylly500 are in some sense 'trapped' on the Scratch website. I encouraged them to unhide file extensions, to use cmd and command line, and ftp, generally to get out from under Microsoft control and become members of the online community.

Solution: use Ubuntu!  big_smile

What would be ideal would be if a kid could have two scratch projects on a website and when a sprite on one is clicked, a character on another says 'Hello World.'

So they are not so isolated one from the other.

Interesting idea. What advantage does it have, though? Why not just import one project into the other, and merge them that way?  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

Board footer