Log In  
Follow
pancelor
Follow

Cart #fitahinigu-3 | 2021-02-23 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

There's something weird going on here; using ..= along with a single-line if-then-end statement causes a syntax error that makes no sense:

syntax error line 16 (tab 0)
if x<2 then bad..="1" end
')' expected near 'end'

I've included 4 similar test cases that don't trigger this bug

My pico-8 version is 0.2.2 (stat(5)==30)

P#88031 2021-02-23 00:17

Cart #remains-0 | 2021-02-19 | Code ▽ | Embed ▽ | No License

controls:

  • arrow keys: move

A short little game. I'm quite proud of how it turned out

P#87870 2021-02-19 01:17

Cart #typefight-1 | 2021-02-19 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
2

a standard normal typing game

controls:

  • a: a
  • arrow keys: move/menu select/etc
  • b: b
  • backspace
  • c: c
  • d: d
  • dash: -
  • e: e
  • enter: confirm/begin/etc
  • f: f
  • g: g
  • h: h
  • i: i
  • j: j
  • k: k
  • l: l
  • m: m
  • n: n
  • o: o
  • p: p
  • q: q
  • r: r
  • s: s
  • space:
  • t: t
  • u: u
  • v: v
  • w: w
  • x: x
  • y: y
  • z: z
P#87864 2021-02-19 00:14 ( Edited 2021-02-19 21:56)

A question came up yesterday in the pico-8 discord: are there any nice ways of doing default arguments? For example,

function foo(a,b,c)
  a=a or default_for_a
  b=b or default_for_b
  c=c or default_for_c
  -- do stuff
end

That's five tokens spent per argument; can we do better?

yes

My first thought was doing something like this:

function new_actor(fields)
 return merge(parse"x=64,y=64,w=8,h=8",fields)
end

This is a common pattern I use in my games; merge(A,B) is a custom thing I have that merges tables A and B together by creating a new table, copying all of A's contents into it, and then copying all of B's contents into it. Note that if A and B share any keys, the value from table B will be used for the result, which makes this handy for setting defaults in a table. (and parse is another custom thing that does what you'd expect, turning a string into a table)

However, this wasn't quite what the questioner was asking for; I can set default values for table members with this method, but I can't set local variables. This method wouldn't work to save tokens if you had to say locals.myvar throughout your function instead of just myvar!

Time for some brain thinking... hmm, I recently found out that lua's _ENV table is accessible through pico-8 (although you have to spell ENV with pico-8's puny text mode, funnily enough). This is a bit of a special table that all variables are implicitly accessed through, so typing tonum(myvar) actually looks up the values of tonum and myvar in the _ENV table. To give a concrete example, you can get/set the value of a variable A by using _ENV.A (or _ENV["A"]) instead

One stroke of madness inspiration later, I came up with this positively arcane incantation:
(this is a working pico-8 cart; paste it into an empty cart to see it in action!)

-- defaultargs, by pancelor
env_backup=_𝘦𝘯𝘷
function defaultargs(str,args)
 local locals=setmetatable({},{__index=env_backup})
 for i,str2 in ipairs(split(str)) do
  local parts=split(str2,"=")
  assert(#parts==2)
  local k,v=unpack(parts)
  locals[k]=args[i] or tonum(v)
 end
 return locals
end

function myrect(...)
 local _𝘦𝘯𝘷=defaultargs(
 "x=64,y=64,w=32,h=32",{...})
 rectfill(x,y,x+w-1,y+h-1,5)
end

function _init()
 poke(0x5f2d,1) --enable mouse
end
function _draw()
 cls(0)
 local mousex,mousey=stat(32),stat(33)
 myrect(mousex,mousey)
 assert(x==nil) -- test to make sure the function's local args didn't leak out into global scope
end

It's certainly Something but hey it gets the job done! And it does it with ~60 tokens for defaultargs plus 6 tokens per function that uses it (which very quickly becomes better than the 5~6 tokens needed per argument in the 'a=a or default' way)

So... how does it work? Well, calling 'myrect(mousex,mousey)' ends up calling 'defaultargs(
"x=64,y=64,w=32,h=32",{mousex,mousey})'. The defaultargs function creates a new table called locals that myrect will later set to it's _ENV. So, if we can somehow make locals equal to {x=mousex,y=mousey,w=32,h32}, then when myrect sets it to _ENV, the variables x, y, w, and h will be available in the scope of myrect. However, we also want to have other global variables in scope too, like the rectfill function, so we do some metatable/__index shenanigans to make that work.

Understanding that is the most complicated bit; the rest of defaultargs does the natural thing that sets locals to have the members we want inside of it. (if 'unpack' confuses you, just pretend that line says 'local k,v=parts[1],parts[2]' instead; it does the same thing)

This one code snippet uses 'unpack', '...', metatables and fallback indexing, required puny text, and lua's _ENV table... wow. I'm not exactly sure why making a backup reference to _ENV is necessary; I assume it's something to do with how _ENV is a bit special in lua?

Time for a nap.

Happy token saving!

-pancelor

P#85041 2020-12-04 21:27

Who's the biggest fish?

Cart #fishy-0 | 2020-11-11 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

Controls:

  • arrow keys: move
  • x: restart

This was my entry to TweetTweetJam 5; check it out on the jam page here if you like. Also, I wrote a short blog post about it here!

And clearly, a tweetcart needs to be tweeted: here it is

P#84105 2020-11-11 05:23

you've received a mysterious invitation to a party. but it's not just any party...

it's the 💀monster mash💀

better find somebody to go with!

Cart #monstermash-1 | 2020-10-30 | Code ▽ | Embed ▽ | No License
13

Controls:

  • arrow keys to move
  • x to interact
  • z to jump

(made by @princryss and me! she did the design + art + sound, I did the programming)

P#83514 2020-10-30 19:45

Cart #escalatorworld-0 | 2020-08-31 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
3

Welcome to Escalator World! You can only go up from here!

Controls:

  • up/down/X to menu. (the rest is explained in the tutorial)

A tribute to one of my favorite games from the old yoyogames forums.

P#81385 2020-08-31 01:03

Cart #woruyudutu-0 | 2020-08-28 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

I think I've found a bug with how music and sfx channels are used; I've attached a cartridge that shows off the issue. Basically, if you call sfx() and then call music() afterward, the music won't be heard until the sfx() finishes, even though there are 3 available unused sound channels for the music to play on.

Am I misunderstanding how this is supposed to work? it seems like the music() command should figure out that it has 3 free channel slots to play in... I do see the channelmask parameter (https://pico-8.fandom.com/wiki/Music) but calling music(4,0,0b1111) instead of music(4) doesn't fix the issue. Even if it did, sometimes you want to start playing music after sound is already playing, so how are you supposed to set up in advance which channels the music has priority to play on?

I can see workarounds for this (e.g. call sfx(8,-2) to stop the sound, or shim in a custom version of sfx() that only ever plays sound effects on channels 2 and 3, leaving the other channels open for music... but these workarounds seem weird/awkward enough that I'm thinking this is just a bug in pico-8

P#81315 2020-08-28 22:29 ( Edited 2020-08-29 00:17)

When I press shift+9 in the music editor (but not the sound editor) on any pattern EXCEPT for pattern 0, pico8 crashes.

My OS is windows 7, and my pico8 version is 29, as shown by print(stat(5))

P#81224 2020-08-26 19:00

I wish I knew exactly how to reproduce this; it doesn't happen all the time. But decently often (~4 times in the last ~40 hours of runtime? idk just a ballpark guess) when I open a new tab in the code editor and press ctrl+v to paste in code, pico8.exe immediately exits.

The last 3 or 4 times this has happened, I've relaunched pico8.exe and tried to do exactly the same keypresses etc (ctrl+a to copy all text from one tab, ctrl+t out of muscle memory to create a new tab (does not work in pico8), mouse click to create a new tab for real, ctrl+v to paste code), and it works the second time without crashing. Maybe it only happens when the console has been on for long enough? (>4 hours, generally). Maybe I'm not reproducing my steps exactly? idk

My pico8 version is 29, as shown by print(stat(5))

My OS is windows 7

(Is there some sort of crash logs folder or anything like that that I should attach to this post?)

P#81222 2020-08-26 18:46 ( Edited 2020-08-26 18:48)

This cartridge fails the assertion, which normally I would write off as a floating point bug, but pico-8 uses fixed point representation so this seems like an actual bug in pico-8 to me?

function _init()
 local x=0.6
 local y=0.3
 local sum=x+y
 assert_equal(sum,0.9)
end

function assert_equal(a,b)
 local s=a.." ("..tostr(a,true)..") does not equal "..b.." ("..tostr(b,true)..")"
 printh(s,"@clip")
 assert(a==b,s)
end

function _draw()
 cls(3)
end

The assertion fails, saying:
0.9 (0x0000.e665) does not equal 0.9 (0x0000.e666)

This is just one example, but this happens to me more often than not when trying to compare decimal numbers :(

P#80489 2020-08-08 04:46

I'm having problems with the del function; its behavior doesn't seem to match what the documentation says it should do. From the docs: (pico-8.txt)

del  t [v]

    Delete the first instance of value v in table t
    [...]
    When v is not given, the last element in the table is removed.
    del returns the deleted item, or returns no value when nothing was deleted.

So, calling del(arr) should be equivalent to calling deli(arr,#arr), right? Both should pop the last element off of arr as far as I understand. But when you run the cartridge attached to this post, the O button works but the X button doesn't

P#80038 2020-07-28 17:35

Follow Lexaloffle:        
Generated 2021-02-28 07:31 | 0.174s | 2097k | Q:123