Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Broadcast block equivalent in other languages?
- wert7
-
2 posts
Broadcast block equivalent in other languages?
Hi everyone. First-time-poster (IIRC), but looooooong-time (since 1.3!) user. Like plenty of others, I first learnt how to program on Scratch and, as a result, my approach to programming has been heavily influenced by it. Since spending my grade-school years in Scratch, I've graduated to Java, C#, shell scripting, and a little assembly, but I was wondering:
Do other languages have some equivalent to the broadcast block? It's really been the cornerstone for how I've made projects on this site for years, and I still think of programming in other languages in terms of how I approached things in Scratch.
I know that something similar to broadcasting an event can be accomplished with using some global Boolean variable and having each object check that variable constantly, but using an IF/ELSEIF statement on every frame at runtime feels wrong. Does there exist some efficiency-friendly equivalent of the broadcast block in other laguages?
Do other languages have some equivalent to the broadcast block? It's really been the cornerstone for how I've made projects on this site for years, and I still think of programming in other languages in terms of how I approached things in Scratch.
I know that something similar to broadcasting an event can be accomplished with using some global Boolean variable and having each object check that variable constantly, but using an IF/ELSEIF statement on every frame at runtime feels wrong. Does there exist some efficiency-friendly equivalent of the broadcast block in other laguages?
Last edited by wert7 (Feb. 22, 2020 15:03:15)
- TheAspiringHacker
-
100+ posts
Broadcast block equivalent in other languages?
You should look into concurrency. However, how Scratch encourages you to design your programs with broadcasts isn't necessarily good programming practice. (Scratch projects tend to be rife with race conditions.)
- _nix
-
1000+ posts
Broadcast block equivalent in other languages?
welcome to the forums! 
largely it depends on the language youre using, but one thing that ive seen a few languages implement is “events”. the function is somewhat different from scratch broadcast blocks, but in essence they are the same: one area of your code may activate responses in other areas by referring to a name shared across all the areas.
a very simple example in node.js might look like this:
by passing the “broadcast” variable to functions in other files as an argument, you can make it available anywhere.
youve clearly got a lot of practice programming so ideas like this might already be familiar to you, but – there are two distinct advantages to events in javascript & other languages that you dont get in scratch:

largely it depends on the language youre using, but one thing that ive seen a few languages implement is “events”. the function is somewhat different from scratch broadcast blocks, but in essence they are the same: one area of your code may activate responses in other areas by referring to a name shared across all the areas.
a very simple example in node.js might look like this:
const EventEmitter = require('events');
const broadcast = new EventEmitter();
// attach an event listener:
broadcast.on('word of the day', function() {
const words = ['Apple', 'Banana', 'Armadillo'];
console.log(words[Math.floor(Math.random() * words.length)]);
});
// run all attached event listeners:
broadcast.emit('word of the day');
youve clearly got a lot of practice programming so ideas like this might already be familiar to you, but – there are two distinct advantages to events in javascript & other languages that you dont get in scratch:
- rather than using a global variable, you can pass data directly into a broadcast:
broadcast.on('exlaim', function(message) {
console.log(message.toUpperCase());
});
broadcast.emit('exclaim', 'Yee haw!'); - you dont need to have just one “broadcast” variable shared across the entire program – you can have multiple made for specialized purposes, or even make your own subclasses extending from the EventEmitter class!
- wert7
-
2 posts
Broadcast block equivalent in other languages?
Thank you for the info and for the welcome! Something like EventEmitters in JS was exactly what I was looking for - plus, it helps that I literally have a tutorial on JavaScript open in the other tab.
- Discussion Forums
- » Advanced Topics
-
» Broadcast block equivalent in other languages?