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

#1 2010-08-03 01:08:06

MabonBaladevaKain
Scratcher
Registered: 2010-07-25
Posts: 80

Extra Logic Flow Control

Hello again, hope you're not tired of me already :p

I'm curious if there is a way to send the "code pointer" to another section of code and then have it return after running that code. For example, I'd like to have the flow of logic jump to an if statement that is outside the current one, run that, and then return the flow of logic so that it focuses on the first piece of if code and continues down its flow.

The reason I want such a thing is basically so if I were to make a long script, there would be no need for duplicate code in different placings to allow for greater control.
I could just have it point back to the original piece of code instead and keep the script from being too long and still have that nice control factor.

My first attempt at what I'm making was such a long script that Scratch cut it off after awhile and wouldn't show any more of it. The code still was there and played right, but because it was cut off the only way to continue work on it would have been to take it apart, work at the sections one at a time, then remember where they all went and piece them back into the main script! ... LOL!

Why am I even trying a one script one sprite one scene project when it's my first project in Scratch?
To test Scratch's strengths and weaknesses of course  smile  ... probing it to see how it fares and getting an overall feel for the program. So far I find it a bit slow and clunky at times, but it's excellent for learning the right way to code.
Scratch is going to give me a better grasp of how all this logic works before I jump into the real languages.

Offline

 

#2 2010-08-03 01:58:22

Jonathanpb
Scratcher
Registered: 2008-07-25
Posts: 1000+

Re: Extra Logic Flow Control

Try splitting those parts of the script into their own little script, and use the [broadcast [ ] and wait] block to activated the script while the 'main' script waits for it.  smile

Of course, it won't be 1s1s then. Dx


"Human beings... must have action; and they will make it if they cannot find it.
-Charlotte Brontë

Offline

 

#3 2010-08-03 09:20:44

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Extra Logic Flow Control

Yup, use the "broadcast _ and wait" block combined with a "When I receive _" hat block on top of your secondary script. (Subroutine, if you will) Here's generally what it will look like:

Main Script
http://i31.tinypic.com/24v0jeg.gif

Subroutine
http://i32.tinypic.com/14aaety.gif

Also, if you wish to pass parameters to the subroutine, you will have to create a variable.

And by the way, I've noticed that you use the term "pointers." Have you used C or C++ before?  big_smile


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#4 2010-08-04 08:04:31

MabonBaladevaKain
Scratcher
Registered: 2010-07-25
Posts: 80

Re: Extra Logic Flow Control

Harakou wrote:

And by the way, I've noticed that you use the term "pointers." Have you used C or C++ before?  big_smile

------------------------------------------------------------------------------------------------------------------------

No, but I have read through portions of C++ programming books and a few others.
I own several books but haven't yet actually tried to code anything with them.
I'm quite sure that I can do any example in the books (while i'm reading it)
but I also know that making a nice game is no easy task. If I can make a few things
in Scratch, GameMaker, The Games Factory, Multimedia Fusion 2, Klik & Play,
Construct, and the other klik-interface programs that I find, and do well on
coding, design, and story aspects, then I'll finally feel ready to tackle some
of the "real" coding languages like C, C++, Java, etc.

I have messed around with some scripting programs though but only to make
macros for mmorpgs and to fight long boss battles as a rogue/assassin sorta
class that just stays hidden in a corner the whole time throwing daggers etc.

I look at it this way; If I'm not going to be able to code a game or an application
in it, then why learn it when there are easier things I could master first?  ...
I might one day get more into the technical aspect of things and play with code
conversions or something nearly impossible like that, but until then
this philosophy holds true.

------------------------------------------------------------------------------------------------------------------------

Offline

 

#5 2010-08-04 08:24:34

Cnor
Scratcher
Registered: 2010-01-29
Posts: 1000+

Re: Extra Logic Flow Control

Slightly off topic but your posts are really long. But thats good  big_smile

Harakous suggestion should work  smile


See that post up there ^^^
That post was done by me  wink

Offline

 

#6 2010-08-04 09:02:32

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Extra Logic Flow Control

Aw, I wish I had been here to answer this! Outposted... by 2 people. Of course, I wasn't there  smile

The "broadcast" blocks do exactly what you want, and they can transfer between sprites. The "broadcast [] and wait" block will wait until all scripts started by the broadcast will finish, and then continue on, whereas the "broadcast []" block will not wait at all.

Offline

 

#7 2010-08-04 09:32:09

MabonBaladevaKain
Scratcher
Registered: 2010-07-25
Posts: 80

Re: Extra Logic Flow Control

Those will work, but I'm sort of curious about the efficiency.
Do the slowdowns have something to do with the amount of scripts used total
or is it only the flow of logic that matters (the scripts currently initiating code)?

Offline

 

#8 2010-08-04 09:35:30

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Extra Logic Flow Control

MabonBaladevaKain wrote:

Those will work, but I'm sort of curious about the efficiency.
Do the slowdowns have something to do with the amount of scripts used total
or is it only the flow of logic that matters (the scripts currently initiating code)?

I'm not sure I understand you here... Are you referring to the waits provided by the "broadcast [] and wait" block?

Offline

 

#9 2010-08-04 10:56:01

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

Re: Extra Logic Flow Control

MabonBaladevaKain wrote:

Those will work, but I'm sort of curious about the efficiency.
Do the slowdowns have something to do with the amount of scripts used total
or is it only the flow of logic that matters (the scripts currently initiating code)?

Believe it or not, a lot of the slowdown in Scratch is intentional -- there is a fraction-of-a-second delay between executions of scripts inside of a loop. This is great for beginners (and everyone else, too), because can create animation that feels natural by putting motion blocks inside of loops without worrying that Scratch will execute it as quickly as possible, rendering the animation pointless. The downside comes when you try to put just about anything else in a loop, because the delay makes things like drawing complex shapes with the pen and checking each item in a list much slower than it needs to be. I think there is a similar delay between broadcasting and receiving.

The other slowdown (which you might not be seeing in a 1s1s project) is simply the strain of Scratch trying to execute a few hundred of your meticulously crafted scripts, and failing miserably. Personally, I can't decide which of these slowdowns is more aggravating...the one I can control, or the one I can't.

Scratch on!

Last edited by fullmoon (2010-08-04 10:58:13)


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

Offline

 

#10 2010-08-05 11:21:53

Locomule
Scratcher
Registered: 2009-08-24
Posts: 500+

Re: Extra Logic Flow Control

But you can completely forgo broadcasts by using what I call trigger variables. One script sets the variable A to 1 then waits until A = 2 or sits in an if a=2 loop. The other script/loop executes when A=1 and when it finishes, sets a = 2.

You could test Scratch for a million years and never figure out all the fast/slow options. For example, the Show block often gets used in such a way that it gets executed every loop. A solution would be to use a variable that gets reset every time the sprite is Shown or Hidden. But the very nature of using that variable may result in more slowdown than just repeating the Show block. Of course you can test such things but keep in mind, your results may be more or less correct as your project grows with more scripts and sprites.


aka Pain from DragonSpires, Delrith Online, BotBattle, Urban Dead etc etc lol

Offline

 

#11 2010-08-05 11:53:49

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Extra Logic Flow Control

Locomule wrote:

But you can completely forgo broadcasts by using what I call trigger variables. One script sets the variable A to 1 then waits until A = 2 or sits in an if a=2 loop. The other script/loop executes when A=1 and when it finishes, sets a = 2.

Keep in mind, forever loops slow down Scratch more than broadcasting. If you want proof of that, try playing my game. Back when I made it, I used a ton of forever loops using trigger variables, essentially exactly like your suggestion. And as you can see, it is horrendously slow online as a result.

Last edited by Harakou (2010-08-05 11:55:27)


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#12 2010-08-05 12:36:53

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Extra Logic Flow Control

Harakou wrote:

Locomule wrote:

But you can completely forgo broadcasts by using what I call trigger variables. One script sets the variable A to 1 then waits until A = 2 or sits in an if a=2 loop. The other script/loop executes when A=1 and when it finishes, sets a = 2.

Keep in mind, forever loops slow down Scratch more than broadcasting. If you want proof of that, try playing my game. Back when I made it, I used a ton of forever loops using trigger variables, essentially exactly like your suggestion. And as you can see, it is horrendously slow online as a result.

Yes, forever loops slow projects down more than any other block. Because by the end, you're having it check dozens of things, constantly, when it doesn't need to - essentially, you have about 20 scripts that you don't need running. Broadcasts makes only the scripts that you need to run, run, so... crisis averted!

Offline

 

#13 2010-08-06 12:05:55

Locomule
Scratcher
Registered: 2009-08-24
Posts: 500+

Re: Extra Logic Flow Control

I didn't say anything about using a bunch of forever loops. I use one per sprite and all my triggering gets done inside that. I further minimize the effect by using as few sprites as possible, each with many costumes. And everything slows down projects, the question is whether it is still fast enough to run effectively or not.

Thanks for the tips though! I am definitely going to try not using any Forever loops. The reason I have always avoided broadcasts was because when I was new, people convinced me that they were disastrous to online project functionality. I sure didn't mean trigger variables were the best option, just an alternative. >=P


aka Pain from DragonSpires, Delrith Online, BotBattle, Urban Dead etc etc lol

Offline

 

#14 2010-08-06 18:16:40

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Extra Logic Flow Control

Locomule wrote:

I didn't say anything about using a bunch of forever loops. I use one per sprite and all my triggering gets done inside that. I further minimize the effect by using as few sprites as possible, each with many costumes. And everything slows down projects, the question is whether it is still fast enough to run effectively or not.

Thanks for the tips though! I am definitely going to try not using any Forever loops. The reason I have always avoided broadcasts was because when I was new, people convinced me that they were disastrous to online project functionality. I sure didn't mean trigger variables were the best option, just an alternative. >=P

They do work better in some situations (such as projects where you want as few scripts as possible), they just have a tendency to slow projects down.


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

Board footer