A code question...

Hello all,
If I write : txt = “vec2(…), vec2(…), vec2(…)” and then mesh.vertices = {txt}, why it does not work ? I suppose the vec2 are not considered as it, but rather as simple string, but then, how can I convert it from string form to the vector I need ? Thank you.

Try this

    txt="vec2(100,200),vec2(300,400),vec2(500,600)"
    --use loadstring to create a function that will assign this table to a variable
    f=loadstring("ff={" .. txt .. "}")
    --execute the function, now the table is in ff
    f()
    mesh.vertices=ff

Ok, I ll try this, thanks

It works… Thank you. But there is some magic behind this :slight_smile:

txt is in a string format. When you put the { } around it, it’s basically just creating a table with a string inside it.

@Ignatz’ code uses the loadstring function, which can take a string and run it as Lua code. It returns a function, which when ran will execute the code as Lua. If it was invalid, it will be nil. You can use pcall(function) to run it, which also tests if it is valid.

txt = "vec2(100, 200), vec2(300, 400), vec2(500, 600)"
mesh.vertices = loadstring("return {" .. txt .. "}")()

I can’t believe it! I shortened Ignatz’ code!

Thank you for the explications Sky. And thank you for the short circuit too :wink:

@SkyTheCoder - nicely done. You are developing good skills because you are exploring and trying so many different things.

@Ignatz :smiley: