Log In  

Hi all,

(I am very aware that this is almost certainly a very stupid question, but if you don't ask...)

Does anyone know if it is possible to take some string content and convert it to functional/executable code in PICO-8/Lua?

[BACKGROUND]
I have LOTS of objects (tables) that all have their own, unique functionality. So unfortunately, there's little potential for sharing data/functions.
For example, many objects like this:

object_1 = {
    name = "my object #1"
    x = 8,
    y = 4,
    states = {145, 146, 147},
    action = function()
        -- code specific to this object here...
    end
}

But as I don't need all these objects to exist at the same time (and could REALLY do with saving some tokens!), I'd like to somehow store these objects + functionality, e.g. within strings.
Then at a later time, restore these back into true objects/tables again.
Something like...

local my_stored_func = "function()  -- code specific to this object here...  end"
local my_real_func = restore_function(my_stored_func)
-- now execute the code
my_real_func()

I've seen similar potential methods of serialising/deserialising from strings - but at best it seems all I could store would be simple object properties (strings, numbers).
Whereas the real benefit for me would be if the whole object (or at least the object's functions) could be string-ified.

Again, I'm pretty sure I'm asking for the impossible, but if anyone has any suggestions that could help - I'd be very grateful to hear them!

Thanks! :D

P#38840 2017-03-29 06:12 ( Edited 2017-04-02 13:56)

There is no way to store Lua code in a string and have it interpreted directly by the Lua interpreter in Pico-8. (Full Lua has a loadstring() function but Pico-8 doesn't support it.)

If it must be a string, your only choice is to write an interpreter for the string format that executes some behavior encoded in the string. You probably don't need all of Lua to do this well when needed. For example, you could encode an enemy walk path with conditional branches using a simple sequence of characters.

You might benefit from other code organization techniques. Prototypical inheritance, closures, and coroutines are all ways of using and composing function objects for code reuse and describing behavior succinctly.

P#38849 2017-03-29 13:47 ( Edited 2017-03-29 17:47)

Thanks Dan, will certainly check out those links.

No, it doesn't have to be strings - I just figured that was the most optimal way to store it as a bulk of code would only take up one token (in theory).

Also, thanks a BUNCH for your PicoTool. It's been a complete LIFESAVER for my project.
For example, un-minified, my current code is nearly TWICE the compression limit and also near the char limit. After luamin, both of those values are almost halved.
(Would be even better, but I'm deliberately adding "exclusions" for public API functions and properties!)

P#38852 2017-03-29 14:26 ( Edited 2017-03-29 18:26)

Cool!

P#38868 2017-03-30 00:42 ( Edited 2017-03-30 04:42)

I solve a similar problem on my game for the level https://www.lexaloffle.com/bbs/?pid=37069, by creating a big state machine and put data in the map. I'm using sprite as instructions that change the state machine, and an unique function that read instructions and change the states and launch events. The main problem was to define a minimal number of instructions that can produce enough situations to create an intersting level.

P#38998 2017-04-02 03:19 ( Edited 2017-04-02 07:19)
:: jihem

Hi,
May be this could help : search for "i_core" and "i_editor" in this forum.
Kind regards,
jihem

P#39000 2017-04-02 09:56 ( Edited 2017-04-02 13:57)

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-11-18 07:41 | 0.019s | 4194k | Q:22