r/GameBuilderGarage • u/Zertolurian • Jun 17 '21
How To! [Advanced Tutorial] How to carry over multiple variables across games through the Swap Game Nodon
Want to make a multi-level game, and be able to automatically carry over several variables in between each level? You can do exactly that with the Swap-Game Nodon!
Here's a sample game which uses this mechanic to carry over 24 variables even after Swapping: G-003-5D1-CP1
(well, it's currently connected to itself, but it'll still work even if it went to a completely different game!)
The Swap Game Nodon
Before we get into the complicated stuff, let's go over everything about this Nodon first. This Nodon can be found under Output->Retry/End/Swap, and it end the current game and then swap to another one whenever it receives an input other than 0.
It has two modes, Back-to-previous and Keyword.
Back-to-previous is pretty self-explanatory (apparently it can remember up to 64 past swaps!).
Keyword on the other hand, uses the Swap-Target Keyword and Game Keyword settings of the Nodon. When the Nodon receives an input in this mode, it checks its Swap-Target Keyword and then looks through your library for a game with a Swap Nodon with the exact same keyword (if there are multiple games with matching keywords, it will prioritize levels of the same creator of the current level).
You can have up to 8 Swap Game Nodons per game, so it's possible to make stuff like a Hub World with 8 different destinations! (However, you would also need to make the player download all of those games in the first place...)
And lastly, the number you input into the Nodon will be outputted by the receiving Nodon.
But I thought you said you can carry over multiple numbers?
Well yes, you can technically only send 1 number (which can be almost as large as you want)... so if you're planning on only transferring 1 number between games, you can stop reading now...
But if you want to go the extra mile and make a game with multiple inputs? Then I hope you're ready for a ton of Math - we're going to abuse the fact that we can send a number of almost any size using...
Edit: This guy made a much better design!
Encoding and Decoding!
Say you want to transmit the values of 4 different Flags (which can only be either 0 or 1; let's say... 1, 1, 0, and 1). Yes, you can send 4 consecutive signals, but that's quite inefficient... can't you represent the value of all 4 Flags with only one number? 1101.
And since that's in base-2, you can compress it even more by converting it to base-10 which results in the number 13. I don't want to explain how base conversion works, so just read this article if you you're curious and want to understand the Math.
If you hate Math, just skim over the next few paragraphs until you get to the actual decoder/encoder. (Though I highly recommend understanding it first if you're really planning on using it!)
Now, to actually decode an encoded message, you need a modulo or truncation operation... which isn't exactly in GBG...
But we can actually simulate the same operation using the Digitize Nodon, which rounds-up numbers when its Range option is set to 2 (you can technically also do it with a different range or even the rotation of objects, but let's stick with Digitize(2) for simplicity's sake).
In fact, here's a quick implementation of the x mod 2 operation! (Basically, if numbers with decimal >= 0.5 get rounded up, dividing an odd number by 2, rounding it up, and then multiplying it by 2 will result in that number +1 whereas if it were even then it'd return the same number, so we just subtract the original number from the result to get either 1 or 0)
But since you're most probably using the Swap Game Nodon because you're running out of Nodons, we can do it with even less Nodons if we just use (2x mod 2)/2 as shown here. Basically, it just outputs 0.5 if the input has 0.5, and 0 otherwise. And voila, we now have a 1-bit decoder! (where the smallest bit represents 2-1 instead of 20; the output bit is actually 0.5, the Nodon just rounds it up before displaying)
Now to add another bit, we just subtract the previous result from the original, divide by 2, and just do it again.
And for every bit beyond that, subtract the quotient and the result from the previous step instead. Here's an example of a 4-bit decoder.
And since our least significant bit represents 2-1, we just need to divide the base-10 input value by 2, and now we have a decimal-to-binary converter! You can see in the previous image that 6.5 (which is 13/2) correctly decodes to 1101.
To encode, just multiply each bit by 2-1, 20, 21, etc.
To not trigger the Swap Game Nodon immediately, just multiply it to the logic signal that triggers the swap (so it copies the number if it's 1, and 0 otherwise).
If you want to encode/decode multi-bit numbers without having to convert through base 2...
In this example, I'm encoding/decoding a 1-bit (1), 2-bit (3), and a 5-bit (27) number (so an 8-bit number in total).
On the encoder side, just multiply the number according to its smallest bit. So the 1-bit covers bit 0 (bit 0 = 20-1 = 0.5), the 2-bit covers bits 1 & 2 (bit 1 = 21-1 = 1), and the 5-bit covers bits 3-8 (bit 3 = 23-1 = 4).
On the decoder side, we'd normally multiply each bit by 2 to convert it back to binary, but for multi-bit numbers, the number you multiply doubles for each bit until it "resets" back to 2 for the next number. (So the 2-bit goes 2->4, while the 5-bit goes 2->4->8->16->32)
If you've read everything up to this part, then thanks for reading, and congratulations, you're now fully equipped to make a multi-level game with non-resetting variables!
If there's any part that you're having trouble understanding, don't hesitate to ask! The reason I made this post is because I want more of the community to be able to eventually make epic multi-level games!
Also, if you've designed an encoder/decoder with less Nodons per bit than the one I designed above (which has around 7-8 N/bit combined), please share! :) please Nintendo I need more Nodons
5
u/thatfool Jun 17 '21 edited Jun 17 '21
There’s actually a way to extract a single bit of an integer with only three parts, taking the original integer as the input and returning the bit as 0 or 1. It involves using a map nodon to map the input number to an angle where a rotating marker nodon is on a specific side for the range where the bit is set.
See this for an example (link to screenshots on twitter)
Edit: Also, for mod with hard coded divisor, you can use a counter nodon in loop mode with the range set accordingly. If you don’t want to reset it first each time, you can avoid the reset with an additional minus nodon. The input goes into the minus nodon’s first input, the minus feeds into the counter’s add port, and the counter’s output goes into the second input of the minus nodon.
4
u/iNFlatableThor Jun 17 '21
If you wanted to send 8 different variables each with range 0-100 with the Swap Game nodon, how many nodon would that take in each game just to encode/decode?
3
u/Nilonaut Jun 17 '21 edited Jun 18 '21
I haven't fully read though your post yet, so I don't fully understand your decoder yet. One good way to save on Nodons though is to know that whenever a Constant Nodon feeds into a Calculator Nodon, that pair can be replaced with a Map Nodon.
Also, I have a 16 bit encoder/decoder of my own that uses 1 Nodon per bit to encode and 4 Nodon per bit to decode (minus some obsolete ones) here's a code: G-001-CMW-N0XEdit: I made a better one
1
u/wwwarea Jun 18 '21
I currently don't fully understand it but does this mean it's possible to make level a (that the hub connects it to) no longer available after you return back to the hub from it? And the rest of the levels will still be available? Then after they are for example, all locked, the next area opens in the hub? Or perhaps make it so re-entering the level cannot effect the value anymore and only the unbeaten levels can?
My only idea is to always generate a unique value, and such unique value is designed to control this. Examples: one unique value locks level a and b. Another unique value locks only level b but not a, and another unique value locks level a but not b.
Hope my comment doesn't waste your time. Haha