How do you pack multiple numbers into a single stat in a Rec Room circuit?
There are several ways to do this that vary based on how much ink they will cost, what kinds of constraints you're working with, and how many stats you're trying to pack. The following solution achieves the tightest packing and costs (n-1)*2 chips for both packing and unpacking, where n is the number of values to store.
Say you have 4 values to store: a, b, c, and d. Each value has the following ranges. You know they will never be greater than or equal to this value.
a: [0, 30)
b: [0, 13)
c: [0, 25)
d: [0, 3)
Importantly, this method assumes that a >= 0, b >= 0, c >= 0, and d >= 0; negative numbers will not work so you'll have to apply an offset before packing them and remove that offset after unpacking them.
Another important thing to check is that when multiplying the max values together, they do not exceed the maximum integer value of (2^31)-1. In my example, that comes out to 29250 which is much less than (2^31)-1 so it can be stored safely in both a player stat and a leaderboard stat.
So to pack these values, you follow the following procedure:
accumulator = 0
accumulator = accumulator * 30 + a // this could be simplified to just "accumulator = a"
accumulator = accumulator * 13 + b
accumulator = accumulator * 25 + c
accumulator = accumulator * 3 + d
In Rec Room circuits, this looks as follows and can be investigated here:
Now to unpack you have to do the inverse:
d = accumulator % 3; accumulator = accumulator / 3
c = accumulator % 25; accumulator = accumulator / 25
b = accumulator % 13; accumulator = accumulator / 13
a = accumulator % 30; accumulator = accumulator / 30 // can be simplified to "a = accumulator; accumulator = 0"
In Rec Room circuits that looks as follows and can be investigated here:
That's the tightest encoding you can achieve with some nice benefits of being able to have arbitrary ranges.