loadstring and graphics?

I have my ‘ants’ program that runs fine, but when i start it with loadstring it doesnt animate.
It uses coroutines, so i wonder if it would be the reason?
Has anyone some insight in this?
thanks
[EDIT] actually, the project animates, but the graphics seem corrupted. SetContext? Or beta 2.1?

@Jmv38, it might be the coroutines not playing nice with load string.

setContext and beta 2.1 work fine in this little test I did:

function setup()
    local test = [[img = image(200, 200) setContext(img)
    fill(255) ellipse(100, 100, 200, 200)
    setContext()]]

    loadstring(test)()
end

function draw()
    background(40)
    
    sprite(img, WIDTH / 2, HEIGHT / 2)
end

thank you @jakattak.
Try this:
1/ run this code; tap on load button. see the result is strange.
2/ paste into a new project (the code is in the pastebord). Run, this is how the result should look like… so what is going on?
Thanks.

-- test httprequest url

function load(data,status,headers)
    pasteboard.text = data
    print("ready")
    resetMatrix()
    resetStyle()
   loadstring( data )()
   setup()
end
function fail(data,status,headers)
    print("load failed")
    print(data)
    print(status)
end

function setup()
    url = "http://jmv38.comze.com/CODEA_4/ants-v2-2.txt"
    parameter.action("load", function()
        print("starting request")
        http.request(url,load,fail)
    end)
end

function draw()
    background(40, 40, 50)
    strokeWidth(5)
end

@Jmv38, I did a little testing (i saved all the code in a tab and instead of loading it from website i load from the tab) and i found that the problem is not load string - it only occurs when you load it from the website, then use load string. I will investigate further and let you know if I find anything.

@JakAttak wow! thank you so much for your help! I really need it to work correctly from the website. I though i could be beta 2.1, but apparently i cant revert back to an older version, so i cant check if it is the cause of the problem.
Thank you again.

@Jmv38, I found a solution - here is the code:

function store(data,status,headers)
    pasteboard.text = data
    saveProjectData("antscode", data)
    print("stored and placed on pasteboard")
    setup()
end

function load(data)
    print("ready")
    resetMatrix()
    resetStyle()
    loadstring( data )()
    setup()
end
function fail(data,status,headers)
    print("load failed")
    print(data)
    print(status)
end

function setup()
    if readProjectData("antscode") == nil then
        url = "http://jmv38.comze.com/CODEA_4/ants-v2-2.txt"
        parameter.action("load and store", function()
            print("starting request")
            http.request(url,store,fail)
       end)
    else
        parameter.action("run", function()
            load( readProjectData("antscode") )
        end)
    end
end

function draw()
    background(40, 40, 50)
    strokeWidth(5)
end

@JakAttak thank you so much!
From your example, i have done this variant. It seems that the only thing needed is the delay.

-- test httprequest url

function load(data,status,headers)
    pasteboard.text = data
    tween.delay(0.1,function()
        print("ready")
        resetMatrix()
        resetStyle()
        loadstring( data )()
        setup()
    end)
end
function fail(data,status,headers)
    print("load failed")
    print(data)
    print(status)
end

function setup()
    url = "http://jmv38.comze.com/CODEA_4/ants-v2-2.txt"
    parameter.action("load", function()
        print("starting request")
        http.request(url,load,fail)
    end)
end

function draw()
    background(40, 40, 50)
    strokeWidth(5)
end

@Jmv38, very nice I did not think of that. Glad I could help.

I still like my version a bit because it does not need to load from internet more than once.

On my side, what is important is:

  • i dont want to create local memory cluttering that users may have difficulties to remove.
  • i want the last version to be always there.
  • and the user who wants to save the download can still do it (via the pasteboard).
    This is why i prefer this slightly different implementation.
    Thanks for your help!

@Jmv38, this makes sense.

@Jmv38, you don’t actually need the delay. see here:

function load(data,status,headers)
    pasteboard.text = data
    tween.delay(0,function()
        print("ready")
        resetMatrix()
        resetStyle()
        loadstring( data )()
        setup()
    end)
end

this is really weird. @Simeon any comment on this?