I see some big brained people like @richard @WoofWoof (w new title) use them but I wanna know how to use them in makecode and the purpose of em
Buffers in MakeCode provide a way to work with large amounts of data.
I use them in projects like What’s My Word and Countdown Solo to store the word lists. I’m also working on an implementation of the Z-Machine to play games like Zork, where I’m using buffers to manipulate the system’s main memory.
on my 3D voxel engine topic me and richard talk about them a bit, you could look there a bit
Buffers are like arrays but each element in a buffer is just one byte (8 1s/0s) so generally you just use them if you are working with a lot of data and don’t want to waste a ton of memory working with them. You cannot simply add elements to them like you can with an array, you must define it first with the amount of memory you’re going to use.
They are generally simpler and smaller amounts of memory to handle and have way fewer edge cases and opportunities for bugs when compared to a normal number array, so they are used when interacting with many built in MakeCode features, for example sound instructions, the color palette, and font data, so knowing how they work can be quite useful, though you can often find extensions that let you do those things without having to ever touch buffers yourself.
There are many ways to write numbers larger than the 1 byte by simply using 2 bytes or more, though I don’t often find myself needing to do that.
The main thing I use them for is the ability to create them from Base64 or hex strings and to generate those strings from them, which is useful if you want to be able to copy data from the console and save it for later.
I suppose you could start in Makecode by just creating a buffer with code like let b = Buffer.create(10) and then messing around with it and going down the list of suggested functions that come up after you type b.
Keep in mind you can also access them like an array with [ ] like b[] = 1 though this will assume you are writing/reading with a UInt8 format, aka “unsigned” (no negative numbers) “integer” “8” (only 8 bits per element) so only numbers 0-255 can be written/read this way. If you write, say, -2, it wraps around and reading it will result in 254. At a certain point you get into bitwise operations, which I’ll let you look up on your own… this post is long enough I think :)
can you store a image using a buffer so I can do like #asga72#gwf or like a code that stores that image
Maybe someday I’ll be able to understand this or whatever you mean by one byte, whatever a Base64 and hex strings are, and what exactly you would use them for. ![]()
Hex and base64 strings are just strings of numbers and letters that represent raw data in a way that can be easily copy and pasted, which is why they are convenient. They help with storing large amounts of data in a text based format. If you look into the explorer at the image and music files (you’ll have to create an image and a song to see these) you’ll see that the image assets and the songs you create are stored as long strings of random gibberish, which are base64 strings. Hex strings are also useful but store less information per character on account of using only 16 characters instead of the 64 used in Base64.
1 byte is 8 bits, a bit is a single 1 or 0 in your computer memory. These 1s and 0s are chunked into sections that are 8 long, and that string of 8 bits is called a byte. There are 1 million bytes in a Megabyte, for example, which means 8 million 1s and 0s.
Memory is usually handled in bytes, meaning numbers stored in a single byte are fairly space efficient whilst not having to be unpacked once retrieved like numbers stored using different amounts of bits would have to be.
somebody should make a guide on buffers
img`…` is a buffer, aka making an image in blocks is already a buffer
base64 isn’t a thing, it’s a type of number
a buffer contains hexadecimal, so 0123456789ABCDEF
That’s a hex (aka hexadecimal) string. Yes, writing hex`(hex data)` does automatically turn your hex string into a buffer, but that is still a hex string and not a buffer.
Yes Base64 is a number type but a Base64 encoded string is what we are referring to here.
well I wanted to make a buffer of a image make it into a small code say 65374 and if you type 65374 it gives you that image
Images are already stored as hex-strings in MakeCode Arcade. You want a data structure known as a map or a dictionary, which are not native to TypeScript but are easy enough to create using interfaces.
interface KvPair {
key: string
value: Image
}
let dictionary: KvPair[] = []
dictionary.push({key: 'abcde', value: sprites.duck.duck3})
dictionary.push({key: '65374', value: sprites.castle.heroWalkFront1})
let theSprite: Sprite = sprites.create(img`.`, SpriteKind.Player)
let key: string = game.askForString("Enter a key.")
let index: number = -1
for (let i: number = 0; i < dictionary.length; i++) {
let kv: KvPair = dictionary[i]
if (kv.key == key) {
index = i
break
}
}
if (index > -1) {
theSprite.setImage(dictionary[index].value)
} else {
theSprite.setImage(sprites.food.smallApple)
}