Peek and Poke are very "low level" commands.
They let you edit memory.
Normally you store data in variables. With names and everything. Like this :
MyVar = 42 |
You could do that, but that would be crazy.
Instead they're mostly used for accessing things you're "not supposed" to touch. Things in the system that there isn't a lua variable for, but the Pico8 operating system stores in its own memory.
For example, Pico8 has eight different video modes, but there's no lua code that switches from one mode to another. If you want to change video modes, you have to use "poke" to edit the memory where the OS keeps track of what video mode it's supposed to be in. I happen to know that's at address 0x5f2c. And I happen to know that the "Horizontal Mirror" mode is mode 5.
poke(0x5f2c, 5) |
See. Easy as pie. .ɘiq ƨɒ yƨɒƎ .ɘɘƧ
> You could do that, but that would be crazy.
:p
They also let you do things like use a screen buffer (draw some things to a part of memory and then show it on the screen later). There's a big ol chunk of memory starting at 0x4300 just for these purposes.
You can also compress graphics more efficiently if you store values in sprite memory as data.. I wrote some things about this and some memory stuff in general including understanding binary and hex here: https://www.lexaloffle.com/bbs/?tid=29966 . And you can also update the display manually by poking addresses starting at 0x6000.
Not everything can be saved though. A lot only exists during runtime.
There isn't bright line between "crazy" and "advanced technique'.
I forget who coined it, But there's a variation of that I heard quoted once that goes:
"The fine line between insanity and genius is measured only by success."
:D
Feedback and edits to the wiki are welcome. I recommend the Memory article, to which the Poke and Peek articles refer.
http://pico-8.wikia.com/wiki/Memory
The addressable memory feature of Pico-8 is intended to resemble old-fashioned computers. Abstract functions such as spr() or print() do fancy things to values in memory that the hardware (or in this case, fantasy hardware) converts into a graphical image. As with the old computers, Pico-8 gives you direct access to this memory to make advanced techniques possible.
With the old computers, you usually access memory directly because it is faster than using the abstract functions for a certain task. One oddity with Pico-8 is that it isn't always faster to, say, manipulate the screen data with peek and poke than it is to use the built-in functions. Some of Pico-8's graphics functions actually have faster access to the memory than a program does with peek and poke.
In other cases, peek and poke are the only way Pico-8 gives you to accomplish certain things. For example, there is currently no way for a program to generate or modify sound and music (such as changing notes in a song during gameplay) using a built-in function other than poking new values into the sound and music region of addressable memory.
One oddity with Pico-8 is that it isn't always faster to, say, manipulate the screen data with peek and poke than it is to use the built-in functions. Some of Pico-8's graphics functions actually have faster access to the memory than a program does with peek and poke. |
I believe this comes from its console heritage. It's pretending it has special hardware for drawing sprites and tiles.
@apLundell
Is there some documentation of the different video modes you can change by altering the memory at 0x5f2c? Never seen it mentioned and it could be really cool for things I'm doing.
@capnmarcy I had found this before https://www.lexaloffle.com/bbs/?tid=2547
@apLundell I'd say it gets this for "free" by nature of memory accesses just naturally being much slower going through one by one peeks/pokes via an interpreted language. But it is a very "realistic" byproduct; so it could definitely make that claim. :)
[Please log in to post a comment]