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

#1 2013-04-18 13:22:48

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Question about broadcasts

Can someone explain to me how the opcodeFrom: method of private-incoming commands under Scratch-Networking works?

A quick TL;DR version:

Can someone explain how this code works: Scratch-Networking>ScratchServer>private-incoming commands>opcodeFrom:

I'm confused mostly at the end, where it says '(aString copyFrom: 1 to: i - 1) asLowercase'.


I'm back.
Maybe.

Offline

 

#2 2013-04-20 04:44:40

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Question about broadcasts

Bump.

I ain't pro at Squeak here!


I'm back.
Maybe.

Offline

 

#3 2013-04-20 06:50:38

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

Re: Question about broadcasts

The method looks for the "op code" in a string.
The op code is a string of numbers at the beginning of aString followed by a space.

Here is a translated version of the code:

Code:

loop through each character in the string {
    if the current character is a space or we reached the end of aString {
       exit the loop and continue the method
    } otherwise {
       continue looking for the space
    }
}
return a substring of aString starting at the first character and ending on the character that the loop above exited at.

So here are some examples of putting strings through the method:

"1236 hello" becomes "1236"
"12 12 3" becomes "12"
"hello hello" becomes empty
" 123" (leading space) becomes empty
"126hello" becomes "126"

It's a pretty weird piece of code if you ask me... especially the choice of delimiter.

Offline

 

#4 2013-04-20 08:20:53

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Question about broadcasts

LS97 wrote:

The method looks for the "op code" in a string.
The op code is a string of numbers at the beginning of aString followed by a space.

Here is a translated version of the code:

Code:

loop through each character in the string {
    if the current character is a space or we reached the end of aString {
       exit the loop and continue the method
    } otherwise {
       continue looking for the space
    }
}
return a substring of aString starting at the first character and ending on the character that the loop above exited at.

So here are some examples of putting strings through the method:

"1236 hello" becomes "1236"
"12 12 3" becomes "12"
"hello hello" becomes empty
" 123" (leading space) becomes empty
"126hello" becomes "126"

It's a pretty weird piece of code if you ask me... especially the choice of delimiter.

I understood the loop, it was much like this Lua for loop:

Code:

for i = 1, string.len(str) do
--code
end

It was just the ending part that was REALLY puzzling me. Now I understand that it's done in an opposite format to Lua.


I'm back.
Maybe.

Offline

 

#5 2013-04-20 09:16:34

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

Re: Question about broadcasts

Well then next time it might help if you gave more information about what you actually understand and what you dont.

Offline

 

#6 2013-04-20 09:23:45

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Question about broadcasts

LS97 wrote:

Well then next time it might help if you gave more information about what you actually understand and what you dont.

Well, I did say:

"I'm confused mostly at the end, where it says '(aString copyFrom: 1 to: i - 1) asLowercase'."

I thought that would've been enough for people to understand that I didn't know what that part was, but the rest was OK. But it seems not. I'll try wording my sentences better next time.


I'm back.
Maybe.

Offline

 

Board footer