Log In  

Is there any way I can modify variables in the PICO-8 Lua VM from JS in an exported web build?

P#112180 2022-05-23 13:23

:: merwok

I don’t think so, but what is the use case?

Are you aware of the GPIO memory? A litle data can be exchanged between a PICO-8 cart and JavaScript code, see https://www.lexaloffle.com/dl/docs/pico-8_manual.html#HTML and https://www.lexaloffle.com/bbs/?tid=31084 for more info!

P#112186 2022-05-23 16:31
:: Brooke

Use case is I think being able to modify tables directly from JS would be nice for multiplayer games. I've looked into the GPIO a bit, and while it could work, it just seems a bit annoying to work with because it's all numbers.

P#112187 2022-05-23 17:39

numbers can be anything.. a string is just number indexing an alphabet (for humans to read)

P#112192 2022-05-23 19:51
:: Brooke

mhm, i feel like being able to just pass strings directly would be nice though. Don't want to have to do stuff with numbers if I don't have to.

P#112227 2022-05-24 17:07

You can reasonably easily read/write a string to memory, see strmem and memstr in https://pico-8.fandom.com/wiki/Ord and https://pico-8.fandom.com/wiki/Chr

From there, the road to being able to read/write arbitrary lua values to memory is relatively straightforward. (Write a "type" byte followed by the value's data)

Now, to deal with data larger than 128 bytes, you could create a simple protocol that allows passing arbitrarily large data.

Example simple protocol:
0x5f80..0x5ffe : 127 data bytes
0x5fff : 1 once partial data is written, 0 once data is read, 2 once complete data is written.

E.g. if JS wants to send write a big value that's encoded into 150 bytes, it would:

  • Write the first 127 bytes to 0x5f80..0x5ffe
  • Write 1 to 0x5fff (partial data available)
  • Wait for 0x5fff to become 0
  • Write the next 23 bytes to 0x5f80..0x5ffe
  • Write 2 to 0x5fff (full data available)
  • Wait for 0x5fff to become 0 before doing anything else

The P8 side, meanwhile, would read 0x5fff every frame and - if non-0 - concat the data with any previously received partial data (in memory or in a variable) and - if 2 - process it.

It could then even return data back to JS via the same protocol. (this will work as long as both sides agree on who is reading and who is writing)

P#113611 2022-06-25 08:48

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2022-08-09 19:36:20 | 0.006s | Q:13