I noticed that 8venture ( https://www.lexaloffle.com/bbs/?tid=2247 ) uses an "unpack" function to define tables from strings, which frees up the code space for like, actual code, by making all tables take up only one token.
array = unpack("1,2,3,4,ideclarea,thumbwar") table = unpack("x=1,y=2,z=-23.12,hello=there") |
This is great, but it didn't support multidimensional arrays or tables! Considering half of We Missed You is literally a huge multidimensional table, a solution to this could greatly benefit more complicated projects, or just people who like doing everything in hilariously roundabout confusing ways. Now you can do things like thiss::
mission = unpack("x=1,y=3,type=0,ents={{x=56,y=76,props={sdir=0.5,scone=0.125,swing=0.12,rate=0.00869,t=0,rad=64},n=218,tfra=8,type=0,update=pvis},{x=104,y=42,props={sdir=0.25,scone=0.125,swing=0.25,rate=0.00869,t=0.5,rad=48},n=218,tfra=8,type=0,update=pvis}}") |
Here's the modified function:
function unpack(s) local a={} local key,val,c,auto,ob local i=1 local l=0 s=s.."," auto=1 while i<=#s do c=sub(s,i,i) if c=="{" then l=i ob=1 while ob>0 do i+=1 c=sub(s,i,i) if c=="}" then ob-=1 elseif c=="{" then ob+=1 end end val=unpack(sub(s,l+1,i-1)) if not key then key=auto auto+=1 end a[key]=val key=false i+=1 --skip comma l=i elseif c=="=" then key=sub(s,l+1,i-1) l=i elseif c=="," and l~=i-1 then val=sub(s,l+1,i-1) local valc=sub(val,#val,#val) if valc>="0" and valc<="9" then val=val*1 -- cover for a bug in string conversion val=shl(shr(val,1),1) elseif val=="true" then val=true elseif val=="false" then val=false end l=i if not key then key=auto auto+=1 end a[key]=val key=false end i+=1 end return a end |
Downsides:
- Can't use spaces or newlines. You can change it to accept spaces, but it'll cost you in tokens to implement.
- Can't use calculations, variables or functions as values, duh.
- Won't be of much use unless your game is table heavy! Using this with We Missed You as a test only shaved off ~200 tokens and there were a lot of tables. Adding more missions would only cost a cool 4 tokens now, though. ;)
- Takes a while to unpack. Don't do this in realtime, it's an init step. :P
I'll post a version of We Missed You with this unpack function utilized once it's ready.
I do have a version that can handle multidimensional arrays, but 8VENTURE didn't need it. And I have another version that can handle whitespace by using a TRIM() function... There's definitely a tradeoff where if you care about tokens enough to use this UNPACK() function, you probably don't want it wasting tokens on functionality you don't use.
One problem with UNPACK is that it doesn't do numeric detection very well. PICO-8 doesn't have good builtins for string<->number conversion. Going by whether the last character is a digit is dead simple and works okay, but it has bitten me when I want string values like "READY PLAYER 1" or "83BAF83AAC2". Similarly the true/false detection when I wanted a string to have the literal value "TRUE".
[Please log in to post a comment]