[REQUEST] Template strings

  • [REQUEST] Template strings in the editor

    Ashley

    This :

    Browser.ExecJS(`alert(${MY_VARIABLE})`)[/code:2tgjv3oy]
    instead of : [code:2tgjv3oy]Browser.ExecJS("alert('"&MY_VARIABLE&"')")[/code:2tgjv3oy]
    
    // CODE
    [code:2tgjv3oy]
    var interpolate = new Function("str","scope","tagFn",`
      /* The function constructor ignores strict mode
         allowing the 'with' statement for scoping
      */
      if(!scope){ if(tagFn) return (eval("tagFn " + str)); 
        else return (eval(str));
      } else with(scope){ if(tagFn) return eval("tagFn " + str); 
        else return eval(str);
      };
    `);[/code:2tgjv3oy]
    
    // TESTS
    [code:2tgjv3oy]
    var scope = { two:"2" };
    console.log( interpolate("`${ parseInt(two) + 2 }`",scope) ); 
    // [4]
    
    console.log(
      interpolate("`${two}`",scope, 
      function(strs,...vals){ // optional template function
          return strs.map(function(val,ind,arr) {
             if(vals[ind]){ return val + vals[ind];} 
             else return val;
          }).join('');   
      }) 
    ); 
    // [2][/code:2tgjv3oy]
  • I wouldn't use this for Javascript code, that is always better in a custom plugin. What else would you use this for? Feature requests should have a comprehensive set of use cases to justify the possibly large amount of work that the feature would need.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Its worth noting template strings have access to eval `token:${ extJS() }`

    Besides the js awesomeness ( which I hope you will consider );

    • String concats `${myprop} : ${myvar}` vs ""&myprop&" : "&myvar
    • Loading JSON

      *

      `{"prop":"val"}` vs "{""prop"":""val""}"

    * I know you'll frown upon not loading assets via ajax request

    but it adds an extra layer of asynchronous logic and it's not always convenient for small stuff you plan on storing in a variable to begin with.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)