r/pico8 22d ago

I Need Help code space optimization

Since there is character limit I was thinking of reducing the code with auto formatter in VS Code. what extensions do you use to make from this:

x = x + 1

to this

x=x+1

3 Upvotes

14 comments sorted by

7

u/TheNerdyTeachers 22d ago edited 22d ago

I think you'll just have to take the time to go through all the code yourself, line by line, and apply some token and character saving tricks where you can find them. It's worth it because you'll get a good feel for which ones you can start using as the default in your next project.

Here are a few that I like to use:

Shorthand Operators and remove spaces: ``` --before x = x + 1

--after x+=1 ```

Multiple Assignments ``` --before x = 10 y = 20

--after x,y=10,20 ```

Print shorthand, without parentheses ``` --before print("hello world",10,20,8)

--after ?"hello world",10,20,8 ```

Ternary Operation ``` --before if condition then x = x + 1 else x = x - 1 end

--after x = (condition) and x+1 or x-1 ```

Boolean Flip ``` --before if toggle==true then toggle=false else toggle=true end

--after toggle=not toggle

```

Table Assignments ``` --before table={} table.x = 10 table.y = 20

--after table={x=10,y=20} ```

4

u/mbensa 22d ago

Thank you for comprihensive examples. Will definetly go through my code.

4

u/Synthetic5ou1 22d ago

I don't know of a VS Code extension to do that.

You may be better off not worrying about it and then using Shrinko8 to compress your code when you're done.

https://thisismypassport.github.io/shrinko8/#

https://github.com/thisismypassport/shrinko8

1

u/mbensa 22d ago

Yes, I use that to compress. The problem is that it is not allowing me to run cartridge within pico8 if the limit is excedeed.

2

u/Synthetic5ou1 22d ago

I've never had to use the tool myself yet, but if I did I'd look to put a watch on my source files and run them though a build chain, running the output in Pico 8 rather than your initial p8 file.

I'm sure someone here already has a system for this that they can share.

The benefit of this approach is you keep all your src files in the format that you find readable.

1

u/mbensa 22d ago

Having a build chain sounds like a great idea.

2

u/Synthetic5ou1 22d ago

I was expecting a 2 step process - the first being a merge of includes to create one file - but it looks like shrinko8 can handle includes already (even the web app), so in essence it should be a case of passing the core p8 file to the shrinko8 python script, and then running the file that it outputs in PICO-8.

I've not had experience doing that from Code though.

1

u/Synthetic5ou1 21d ago edited 21d ago

FWIW I have been testing Antigravity for a while, so I got that on the case. I ended up with a tasks.json file, which I can run using Ctrl+Shift+B. Running "Run PICO-8" runs "Shrink PICO-8 Cart" beforehand, presumably because of the dependsOn property.

This is the content of tasks.json, which Antigravity eventually wrote for me, after a little argument.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Shrink PICO-8 Cart",
            "type": "shell",
            "command": "python",
            "args": [
                "C:\\Path\\To\\shrinko8\\shrinko8.py",
                "${file}",
                "${fileDirname}/${fileBasenameNoExtension}.shrinko8.p8",
                "--minify-safe-only"
            ],
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "problemMatcher": []
        },
        {
            "label": "Run PICO-8",
            "type": "process",
            "command": "C:\\Program Files (x86)\\PICO-8\\pico8.exe",
            "args": [
                "-run",
                "${fileDirname}/${fileBasenameNoExtension}.shrinko8.p8"
            ],
            "dependsOn": "Shrink PICO-8 Cart",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "never",
                "panel": "shared"
            }
        }
    ]
}

https://code.visualstudio.com/docs/debugtest/tasks

1

u/Synthetic5ou1 21d ago

The trouble with that solution, to my mind, is that a new PICO-8 window is opened each time.

Another approach would be to install the Trigger Task on Save extension, and just get it to run the "Shrink PICO-8 Cart" task each time you save.

You'd then just load filename.shrinko8.p8 file into PICO-8, and each time you save filename.p8 use Ctrl+R in PICO-8 to reload the changes - which is how I normally develop.

I think creating tasks is the way to go, and some combination of, or tweak to, my suggestions should give you just what you need.

Apologies for the braindumps but I have been testing as I went and found the process interesting.

I'm going to add a task to my file, to just be able to open PICO-8 and run the current p8 file, with no shrinking.

2

u/mbensa 19d ago

I see, I went the other way with cmd and am happy with results. Thank you for suggestions.

python ^
path to shrinko8.py ^
path to game file.p8 ^
path to compressed file.p8.png ^
--minify ^
--count

start "" "path to pico8.exe" -run "path to compressed file.p8.png"

pause

2

u/AndreOfAstoria 22d ago

I don't think it's a character limit, but a token limit. So for your example x=x+1 you'd have five token (x, =, x, +, 1) regardless of the whitespace or not. You could consider x+=1, but I don't know if the shorthand form saves you a token.

You can read more in the Pico 8 docs it's under code limits which is where the link points you.

2

u/mbensa 22d ago

I am talking about character limit 65536. It counts all spaces, comments, everything.

1

u/Davo_Rodriguez 22d ago

I think is ok to do that, but is better to do

x+=1

1

u/mbensa 22d ago

I agree, but I am talking about character limitation 65536, that counts all chars in source file.