Lately I've heard bits here and there about a new function called unpack that seems to have some significant benefits, but after searching the BBS, the Pico 8 Wiki, and Google, I've barely been able to come up with any solid infomation on it or how to use it.
If someone could point me in the right direction, I'd much appreciate it.
https://www.lua.org/pil/5.1.html and https://www.lua.org/pil/5.2.html - both near the bottom of the page - mention it; that's about all the documentation I read on it.
I've used it a few times to unpack a table of sub-tables of data for various draw commands (such as rect or spr). The following example is illustrative of such usage. I've put the pack command into this example as well just because it complements the other; although otherwise I would have used {x1,y1,x2,y2,c}. As mentioned in the recent fast sort thread - https://www.lexaloffle.com/bbs/?tid=38706 - it can also be used for copying tables.
function _init() rectangle={} for i=1,30 do local x1,x2,y1,y2,c=rnd(128),rnd(128),rnd(128),rnd(128),rnd(15)+1 add(rectangle,pack(x1,y1,x2,y2,c)) end end function _update() --nop end function _draw() cls() for i=1,#rectangle do rectfill(unpack(rectangle[i])) end end |
There's also a thread on stackoverflow https://stackoverflow.com/questions/45238831/lua-unpack-then-pack-an-array that's worth a quick read as answers there mention nil (and a field n that is added with table.pack, which is also added in pico-8 in the example I have given above where I have used pack rather than {} ).
It should perhaps be noted that:
- All the links I have provided are not recent - the online version of Programming in Lua refers to an older version of Lua, and the stackoverflow thread is 3 years old.
- The stackoverflow thread is talking about table.unpack and table.pack - I do not know if this makes a significant difference, but even so the fact it highlights nil is something worth checking to see what happens with nil values in pico-8.
- None of the links are specifically about pico-8.
- At the time I looked in pico-8.txt the information was no more than a change note: "v0.2.0i Added: pack(), unpack()"
As they are not recent nor specifically about pico-8, the information at the links may vary from how the commands work.
It appears the idea to add it was discussed by Lafolie and Felice (5th May): https://www.lexaloffle.com/bbs/?tid=37761
And zep added it shortly after (8th to 11th May): https://www.lexaloffle.com/bbs/?tid=37695
Thanks,
seems like this really isn't documented yet, but with some experimentation I got it working for me. I've been working on a system for drawing game logos in real time directly from compressed strings, bypassing the sprite sheet. With the unpack function, I can now draw an arbitrary number of specified colored rectangles from a string for less than 50 tokens! Here's a little example:
t,s={},"t░ょなou✽アに`u✽ょなdt░t░lアぬアにlv●v●oゅ●ゅ●ovとvとoゅとゅとo" function _update() cls(12) for i=0,#s-1 do t[i%5+1]=ord(s,i+1)-96 if(i%5==4)rectfill(unpack(t)) end end |
@JadeLombax Very impressive compression again, and well worked in with the unpack command.
Given what I've seen before of your code, I did wonder if what I was typing above would be of any help to you personally, or if it would just be of more help to others. Either way, I figured it would benefit someone.
Thanks for sharing what you've done with it. :)
Thanks, the main thing was just figuring out specifically what unpack did. I wasn't sure if it was used to turn strings into tables, or simplify plugging arguments into functions.
I've been working on compression stuff lately to see if some barriers with Pico 8 can be surmounted, and it's looking like they can. I'll have more to post soon, but here's the more complete version of the last snippet I posted, as a kind of teaser =).
s1,s2="t░ょなou✽アに`u✽ょなdt░t░lアぬアにlv●v●oゅ●ゅ●ovとvとoゅとゅとo","|ウツらシc``````おウツカシg``````トウツタシo``````トウツタシo``````oウやいオn``````~ウやいクf``````うウや▥クb``````オウや▥オf``````トテ}スシn``````トテ}スシn``````おチ|オシn``````|▤|らシn``````````````````````````````````````````````````````lcせoせcチスくcg`ゆせに○んgチセれせo`トエよかシoチタシエ○`トエよかシoチタシエ○`シウやいシnもいシウ}`シウやいシnもいシウ}`シウやいシnもいシウ}`シウやいシnもいシウ}`シウやいシnもいシウa`シウや{シnも▥クなg`シウやkシnも▤カno`シウや{シnも▥クn|`シウよいシnもいシウ}`シウよいシnもいシウ}`シウよいシnもいシウ}`シウよいシnもいシウ}`シウやいシoもいシエよcシウやいシoもいシエよcシウやいんgも▥んせにcシウやいせcも▤せcせc" t1,t2={},{}for n=0,3023 do if(ord(s2,n\7+1)-96&2^(n%7)>0)t2[n]=1 end function _update() cls(12) for i=0,44 do t1[i%5+1]=ord(s1,i+1)-96 if(i%5==4)rectfill(unpack(t1)) end for n=0,3023 do if(t2[n])pset(n%84+26,n\84+42,0)pset(n%84+25,n\84+40,15) end end |
[Please log in to post a comment]