Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

A code question...

edited August 2013 in Questions Posts: 35

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.



  • IgnatzIgnatz Mod
    Posts: 5,396

    Try this

        --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
  • Posts: 35

    Ok, I ll try this, thanks

  • Posts: 35

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

  • Posts: 1,976

    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!

  • Posts: 35

    Thank you for the explications Sky. And thank you for the short circuit too ;)

  • IgnatzIgnatz Mod
    Posts: 5,396

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

Sign In or Register to comment.