[Plugin] JavaScript (C2 and C3)

2 favourites
From the Asset Store
A cool way for kids to write and practice English Alphabets
  • It doesn't work:

    Sample Javascript file:

    var global_variable = "Global Variable's value.";

    function global_variable_function()

    {

    console.log("global variable function called");

    }

    var global_object = new class_object();

    class class_object

    {

    constructor()

    {

    this.name = "Kenny";

    this.age = 23;

    }

    internal_class_function()

    {

    console.log("Internal class function called.");

    }

    }

  • With that sample javascript file, I tried Browser Log:

    JS.JSCodeValue("global_variable")

    and I got:

    ValerypopoffJS plugin: Error in 'JSCodeValue' expression

    ---------------------

    JS code: global_variable

    ---------------------

    global_variable is not defined

    Then I tried Browser Log:

    JS.Value("global_variable")

    And there is no error...instead...I got....0 !

  • JS: Call function:

    Expression: "global_variable_function"

    Result:

    ValerypopoffJS plugin: Error in 'Call' action

    ---------------------

    JS code: global_variable_function()

    ---------------------

    final.end is undefined

  • Toddler Well it's weird. I just tested it with javascript you provided and it works fine in both C3 and C2. Can you give me the project file that reproduces the problem?

  • Link removed.

  • Yep. You just didn't add the name of the script to "Script files" property of the plugin. You only added the script file to the project and the plugin has no way of knowing that. Your javascript is simply not added to the page. So the plugin says the truth: "global_variable is not defined" and other error messages.

    When you add the name of the script properly, everything works fine.

    Here is the file:

    http://cebion.epizy.com/test.capx

  • Updated.

  • valerypopoff

    To access Javascript Variable:

    JS.Value("global_variable")

    JS.JSCodeValue("global_variable")

    Both does the same thing, so...why ?

  • valerypopoff

    To access Javascript Variable:

    JS.Value("global_variable")

    JS.JSCodeValue("global_variable")

    Both does the same thing, so...why ?

    They work differently.

    JSCodeValue just evals the string you pass to it. So it can be any valid javascript you can think of. Using eval is not a great idea. Also eval is slow. So do it at your own risk.

    Value doesn't use eval and parses the js-expression that you pass to it. Js-expression can contain:

    • alias
    • global var-variables (not let and not const)
    • global functions
    • ["brackets"], [0]
    • .dots

    Js-expression can't contain:

    • function calls
    • operators other than brackets and a dot, like +, – and stuff
    • window object

    Using "Value" is preferable. It's super quick and it's safe.

    I'd suggest you read the manual. It's all there: valerypopoff.ru/construct/js-plugin

  • File accepted.

  • Priviet Valery!

    I am making a simple GUI project with C2 running in nw.js (I am not a programmer so C2 was the choice), the problem is that i got stuck when I need to encrypt a string (need aes encryption):

    I am over a month trying it through all plugins I found for call javascript or other means, getting mad, although I read all, and tried all can simply not do it...

    I wonder if you could help me to get out of this problem: I need an example of capx file that could pass a string (text to encode/decode) and a password and take the result back in C2.

    the javascript I am trying to run would be something like this (in my leak of knowledge of programing was the best I could find and modify to be working in nw.js):

    -------------------------------------------------------------------------------------------

    function encrypt(text,parol,aes_format){

    // covert to string to be used in Cipher

    var aes_format = "" + aes_format

    var parol = "" + parol

    var cipher = crypto.createCipher(aes_format,parol)

    var crypted = cipher.update(text,'utf8','hex')

    crypted += cipher.final('hex');

    return crypted;

    return pass;

    }

    function decrypt(text,parol,aes_format){

    // covert to string to be used in Decipher

    var aes_format = "" + aes_format

    var parol = "" + parol

    var decipher = crypto.createDecipher(aes_format,parol)

    var dec = decipher.update(text,'hex','utf8')

    dec += decipher.final('utf8');

    return dec;

    }

    // example variables

    var hw = "hello world"

    var hw2= "5876e13e22e45d9cc1397e0ac0658bf0"

    var parol = "e7wn/S4wJ2&·ns%e7wn/S43J@3Uns%e7wn/¨92J28Uns%e7+n/S()J28U"

    var aes_format = "aes-256-cbc"

    // example call functions encrypt & decrypt

    encrypt(hw,parol,"aes-256-cbc")

    // decrypt(hw2,parol,"aes-256-cbc")

    --------------------------------------------------------------------------------------------

    and I really need to survive achieve in C2: call js to "encrypt(text, pass, format) and same for decrypt function" and receive results back in any variable of C2

    could you help me?

    thanks in advance

  • sorry, at encrypt function this line have to be removed

    return pass;

    thanks!!

  • hugone I'd suggest you ask around in the Construct Discord community. People discuss stuff like that all day long there: discord.gg/HnuhDR

  • Sposivo Valery I will!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • valerypopoff, do you think there is a way to say for example, create a class on the *.js side and then make every instance of a sprite have an instance of that class ?

    Think of it as polymorphism, your "extends" so to speak.

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