About this blog
Thank you for showing interest in my personal blog! The idea of having my own blog has been in my head for a long time, and now I am finally getting to it!
On this blog, I primarily want to write about all things programming: Backend and frontend technologies, developer tools, processes, architectures, etc. I might throw in a subject of public interest from time to tome to show my point of view, but these kind of articles will not be the majority. Moreover, I have no clue on how often I will be writing here, so I recommend subscribing to the RSS-Feed instead of regularly checking the site on your own.
One last thing before this article really starts: This is my personal blog. It is in no way connected to my current employer, though I might add some personal experience from my worklife from time to time. The views I present here are entirely my own, so if you want to yell at someone, yell at me.
About this article
As this is my first article on my personal blog, it seems fitting to talk about how I started to get into programming. Or rather, what one might call programming. We’re gonna have a look at a basic, yet powerful programming model I learned as a kid and then later see what properties this model has from a professional software developer’s perspective. Don’t worry, though: Not all posts will be as nostalgic as this one 😉.
When I was a young nerd…
While I was still in elementary school, I already was a huge fan of the Sci-Fi television show Stargate. Especially, I liked the role of Samantha Carter, who used her brains to save the day instead of muscles (sorry Teal’c). She was not only a scientist, but also an engineer. As a kid with pale skin, I also was much of a gamer from early on. Sadly, my love for Stargate and gaming were never merged into a decent game, with many disappointments though the years, like the cancellation of Stargate: The Alliance and then later Stargate: Worlds.
One day, I must have been 12 or so, my brother showed me this Half Life 2 mod called GarrysMod (back then, it really was a mod, while now, it is it’s own standalone game just like Counter Strike). It was a sandbox game, where you could use all the props, characters and vehicles of Half Life 2 to build your own base or whacky vehicles, or fight an army of zombies. But the coolest thing for me, and the reason I still come back to this game every now and then, were the Stargate mods. Many Stargate fans got together and created mod packs that introduced all sort of working objects from the Stargate universe for GarrysMod and now, little nerdy me finally had the chance to create the Stargate stories I wanted in a game. It was fun.
After a while of playing the game, you sooner or later stumble across this thing called the Wiremod, another popular mod for GarrysMod. It adds objects and tools to the game to create circuits, like in electronics, that can be used to control all sorts of things (think of Minecraft’s Redstone, but on steroids). Here is a simple example that can be considered the “Hello World” of Wiremod: displaying a constant value:
Here, we’re using a Constant Value on the left and a Screen on the right, which can be used to display a value. When creating a Constant Value, you can set the value(s) it outputs in a menu. Wire objects like these can be found in the in-game menu after installing the mod and can be placed with a toolgun. You can see what inputs and outputs are available and their current value with the debugging tool:
Using the Wire Tool, you can connect the input of the Screen to the output of the Constant Value. The resulting connection is indicated by a grey connection with a white arrow, though this visualization is optional and in some cases should be omitted, like when connecting things that are far apart, as the wire would go across the entire map.
Let’s take a look at another example:
We’re outputting the current X coordinate of a GPS chip. But it looks like the number is too long for our screen, so we can put a rounding gate in between:
The nice thing is that Wiremod comes with a bunch of chips and sensors that output data and actuators that take data to manipulate the physical world in the game. Here’s a more complicated example: Opening a door with a hydraulic controller:
The rectangle plate in the middle is our door, which is connected via a hydraulic “rope” to another plate on the right. The hydraulic’s controller is located at the left of the door, where the rest of our wiring will be. To open or close the door, we have to set the length input of the hydraulic to 0 or some specific length respectively (in this case, about 140 in-game units). We can achieve this by using a toggling Button and an If-Then-Else gate:
The If-Then-Else gate (in the middle) takes three inputs:
- a boolean for the decision
- a value to output if the first boolean is truthy
- a value for a falsy value of the boolean.
In this case, the boolean is simply the output of a Button and the respective values are stored in a Constant Value. Whatever the output of this If-Then-Else gate is will be used as the length of the door. When all is wired together correctly and we press the button, the plate slides to the side and allows us to go through. Open Sesame:
What if we want to be able to open the door from the other side though? We can easily add another Button and connect the two Buttons with an Xor gate. The output of this Xor gate is then used as the first input to the If-Then-Else gate. Notice that one of the wires goes through the wall, as in GarrysMod, wires and ropes do not collide with the world properly. Now, the door open or closes respectively when one of the buttons is pressed.
Some other creations
The Wiremod comes with A TON of stuff to play around with, from the simple gates shown above to even complex computer systems with their own RAM-like memory chips. When you can’t find a gate that does what you need, you can create your own using a simple programming language called Expression2. Although I personally have not used this programming language until I was in college and already familiar with “real” programming languages.
The best part for me was that you can use Wiremod in combination with the Stargate mods, like this Asgard Transporter that can beam stuff around the game map (picture is taken from the Steam Workshop)
The Asgard Transporter takes coordinates as vectors for origin and destination, so by itself, it is of limited usefulness to the player. But this of course motivates nerdy pale kids! Here’s something I found on an old hard drive recently: I created some circuits around it to create a full transporter panel with which you can locate players, NPCs or props and transport them to another place on the map. Don’t expect a full explanation on how this thing works though. It’s pretty complicated and I’m not sure how I build back then either. Who would have thought that someone would need documentation? :^)
Sadly, I could not find the files for what I believe was the most impressing thing a nerdy, little kid could have done with no education in programming whatsoever. Fairly early after I begun to play GarrysMod, I created a flying ship that had 2 modes: Manual and automatic. In manual mode, you could sit behind the steering wheel and fly around the map. But when your “fuel” (basically just a counter) dropped below some threshold, it automatically went to autopilot, turned the ship around and flew to the edge of the map where a little pond was located. The ship then “refueled” once it arrived and when the tanks were full, it would switch back to manual mode and give the player control of the ship. Looking back at it, I think it is quite impressive that something as complicated as that can be made by a self-taught 13-year-old (who admittedly had some time on his hand and a certain natural talent as it would seem).
The visual programming model
I hope you made it this far, because now begins the really interesting part. As we have seen, in this programming model you use sensors, gates and actuators to manipulate the world. Let’s have a look on what properties this programming model has.
When you change one of the inputs of a Wire gate, it’s output is automatically updated and the change is propagated to dependent gates. Therefore, gates are basically observables. Most articles and books explain reactiveness with an Excel sheet, where updating a cell causes dependent cells to update automatically. I, to this day, still think of Wire gates when it comes to reactiveness. With the gates and wires approach, you not only define a pipeline where data flows through, they also make it quite visible. Visibility is something that even real programming languages that support observability are struggling with as well, as cause-and-effect relationships are not so easy to understand. Also, the setup of a subscription is quite easy with the toolgun.
The output of a gate is determined by it’s inputs. This is somewhat not really true, because some sensors like the GPS receiver do change their value regardless of their input when moving the sensor around. I consider sensors and actuators special cases and would like to focus less on them, but rather on those gates “in between”. You can compare this to Haskell, which has it’s IO Monad as a way to do input and output orthogonal to it’s otherwise pure programming model. Purity makes things of course much easier to understand and makes it impossible to have gates with unknown side effects.
The output of a Wire gate cannot be changed by anything other than itself when it’s own inputs change. So you could say that a Wire gate output is about as immutable as an observable. Still, I would say it counts as it promotes the idea that a value can be used and that value will not be changed by anyone else. By that, I mean that the output of a gate can be consumed by two different gates and while they can do whatever they which with their copy of the value, the other gate is not affected by it. It becomes even hard to think about how mutable outputs would be like when visualizing it with gates and wires. And you did not even have to explain how painfully bugs in mutable code can be.
Type safety (?)
Well yes, but actually no… The Wire gates know some basic types like Number, Vector, String and prevent you from wiring a string output to a number input, but that’s about it. It does not allow you to create custom types from these primitives in any way. There isn’t even a concept of a boolean really; it’s just zero or not-zero numbers, the same numbers that are also used for floating values.
You might be thinking now: “Well, that’s all great, Thomas, but what’s the point? So some game of your past had a feature where you could program stuff, but what’s the deal?”. Well, the thing that bothers me is the way kids are taught how to program these days. When I was a young teenager, I did not have Computer Science as a subject in school like many student have today. Today, a lot of kids are taught using stuff like Scratch, where programs look like this:
This programming model highly promotes imperative programming styles. And while some problems can be solved nicely and intuitively with an imperative programming style, recent trends in the industry show that functional programming paradigms gain more and more importance. It will probably become an advantage to have a functional way of thinking when building reactive, data-driven systems, but youngsters are already pushed into another way of thinking about computing problems in an early and critical age.
GarrysMod with the Wiremod shows that there is an approachable way to teach a data-driven, reactive programming paradigm without having to use complicated mathematical models, while still giving the ability to play around the environment and thus engage children to be creative and have fun. Of course, GarrysMod should not be used as an educational tool as it also includes guns that can be used against humans, but I do wish for a game where problems are mostly solved in a functional way.
So, in this post we have seen how the Wiremod for GarrysMod can be used to manipulate the world with gates and wires. We then had a look on what properties this kind of visual programming has. Lastly, I voiced my concerns about today’s approach of some games how to teach programming to kids.
This is the end of my first blog article. I hope you liked it! As I already mentioned in the beginning, this article is somewhat different of what I plan on doing in the future, so stay tuned for more stuff. Also, feel free to drop a comment below.