Message Passing in Godot

One of the more useful features of Unity, that I’ve been missing since I stopped using that mess of a thing (not due to the latest upset; they burned me away before that), was its support of Smalltalk/Objective-C style message passing (Send/BroadcastMessage). Godot’s signals are a perfectly good way to decouple code within the same node subgraph, letting you “pull” messages from a known node, which is great for most of the times one would use message passing in Unity — and I’d bet more performant to boot. What it’s less great for is “pushing” messages to nodes unknown at scripting time.

For example, consider a scene having a child node that’s responsible for handling an actor’s health. It may have a signal which is emitted when the actor dies, and a function for applying damage. Now let’s also consider an exploding barrel, which needs to find anything that can take damage within its blast range (an Area2D, for the sake of example) and apply a set amount of damage. Signals do not work well for this, because it doesn’t make sense to inject and remove a subscription to an exploded signal as a separate step.

It also doesn’t work very well to call damage on whatever’s returned by get_overlapping_bodies, since then you have to introduce scripting to every child of CollisionBody2D to either track health there, or forward it to whatever’s managing health. It’s also not great to look for a HealthComponent directly underneath the body, since that may not be where the designer put it. It probably does not make sense to make a health node the child of a body, unless that is the root of the actor (if it is, that strikes me as a bad idea). A better way, it seems to me, to handle this sort of case would be to send a message like we did in Unity.

Godot’s documentation, and stuff like the forums, doesn’t really talk about it much, but there is actually a way to do this pattern, built into Godot. Enter: Node.propogate_call!

This function recursively calls a specified function (if it exists) in all of its children (and optionally the node itself). This is even a bit more powerful than I remember the SendMessage being in Unity, since it walks down the tree from the specified node. Pair this with the root node in the scene tree and you’ve got a replacement for BroadcastMessage.

Why do web pages not stop loading?

You click on a link and it opens up a new tab. So it starts loading, and the screen stays white while it drinks the 60MB of CSS. And then it gets done with that, and you start seeing content poof into being. Except, it isn’t really content. It’s these grey boxes with scanlines running across. After a beat or two, your browser’s finished drinking its 100MB of JavaScript and the images and text finally loads. You breathe a relieved sigh, the page seems to be ready for you after only a couple of seconds.

How does this make you feel?

Except then, shock and horror, the page goes jank. The ads are loading. Not to worry, after a few more seconds, the ads finish loading. You breathe a relieved sigh. Now it’s time for you to look at the page.

You have a very pleasant read of about 6 sentences. Then you decide to scroll so you can read more past the oversized ads and images that really didn’t need to be there. To your overwhelming shock (and even a little bit of horror), you find that the page has yet again gone jank. It’s those grey boxes again. A moment later, they’ve lazily loaded again (thanks to the marvel of an additional 20MB of JavaScript). You breathe a somewhat less relieved sigh. The page will jank whenever you scroll, you’ve accepted, but at the very least your connection is fast and it’ll only be a few seconds each time. In the time you wait for your page, you meditate upon the impermanence of all things.

The next chunk of the page has loaded yet again, and you read that. So far you’d describe your experience as ‘tolerable’. With less shock than you expected, you realize that your fans have kicked on. The ads each change, shifting the page around. You breathe a tranquil sigh. Your machine glows from heat.

You eventually finish reading the article, and all things considered, you liked it well enough. It doesn’t occur to you that this is at all unusual, any of what just took place. You see a related article you might want to check out later, so you save the link somewhere for you to read later.

A day or so later you need to kill time. So you pull out your phone and open that article you see. You load it up and see a white screen. It was then that you remember how slow cell service can be (and usually is).

Needless to say, by the time you’ve read that article, you’ve achieved enlightenment. You now understand that you are the Buddha, everyone is, and always have been.

How the web went from something where you’ve fetch a document and then go away to this, though. Now that’s a mystery beyond even yourself.