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 ... 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
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.
Of course, it won't be 1s1s then. Dx
Offline
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
Subroutine
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?
Offline
Harakou wrote:
And by the way, I've noticed that you use the term "pointers." Have you used C or C++ before?
![]()
------------------------------------------------------------------------------------------------------------------------
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
Aw, I wish I had been here to answer this! Outposted... by 2 people. Of course, I wasn't there
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
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
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
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)
Offline
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.
Offline
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)
Offline
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
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
Offline
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.
Offline