[Plugin] JavaScript (C2 and C3)

  • The function still isn't working (normal also does not appear to be defined) but now at least I can investigate what's going on there in the js file . Thanks again!

    Well it worked, i checked. Can you share the project file again? I'll look into it.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • >

    > The function still isn't working (normal also does not appear to be defined) but now at least I can investigate what's going on there in the js file . Thanks again!

    >

    Well it worked, i checked. Can you share the project file again? I'll look into it.

    Oh wow, thank you for taking a look!!! I'm just getting to work but when I get home I'll PM you the file.

  • >

    > 1-Do you think looping over an array (defined in js script) in construct (to build a leaderboard for example) is a bad idea, since everytime the JavaScript plugin need to parse an alias expression ?

    >

    >

    This is a bad idea and wrong on so many levels. The whole point of the plugin is that you don't have to loop over arrays in Construct anymore.

    Cheers.

    Ok, then how do I build a leaderboard (the view) in C2 based on an array in my js script ?

  • Ok, then how do I build a leaderboard (the view) in C2 based on an array in my js script ?

    I can help if I understand what do you mean "how". How would you do this if the array wasn't in js-script?

  • Sorry, maybe I wasn't clear.

    The array of scores would be in a C2 array.

    I would loop the C2 array like this :

    for each item in the array

    - create a sprite ( a line of the leaderboard )

    - create a text and set its value to the current score.

    So, how would I do this if my array is in js-script ?

  • I still don't understand what you mean by "how". Is it the javascript itself that you're struggling with? Or is it JS Plugin? What is it exactly you don't know how to do?

    Anyways, the process would be absolutely the same:

    The array of scores is in a JS array:

    var ScoresArr = [222, 235, 547, 12346, 325];
    [/code:nztqujon]
    You go to Construct and loop the JS array like this: for each item in the array
     — create a sprite ( a line of the leaderboard )
     — create a text and set its value to the current score.
    
    So, what is it exactly that's not clear here? I can't help you if you don't ask a certain question. Or at least i need to understand what you're struggling with. You don't know javascript? You don't know Construct? Or is it just the plugin? 
    
    I don't want to torture you to death by the way   . I want to help you. I swear I will freaking do this for you and send you the C2 file that does what you need if I see that I can't help you otherwise. It's just... I still hope that I can.
    
    
    

    Sorry, maybe I wasn't clear.

    The array of scores would be in a C2 array.

    I would loop the C2 array like this :

    for each item in the array

    - create a sprite ( a line of the leaderboard )

    - create a text and set its value to the current score.

    So, how would I do this if my array is in js-script ?

  • This is what I want to do in C2 :

    But you said that looping over an array defined in js script in C2 is a bad idea (cf my previous questions).

    If it's a bad how can I get my scores to build a leaderboard ?

    [quote:1ciur67z]I don't want to torture you to death by the way

    lol, cool, I thought I was the one torturing you.

  • This is what I want to do in C2 :

    You're almost good to go. You should do this:

    var ScoresArr = ["111","222","333"];
    
    function GetScoresCount()
    {
    	return ScoresArr.length;
    }
    
    function GetScore( id )
    {
    	return ScoresArr[id];
    }
    [/code:2xarrmhj]
    
    [img="http://valerypopoff.ru/temp/scirra-forum/aekiro.png"]
    
    Look at the picture and note where you're mistaken, it's important. For example, you got
    [code:2xarrmhj]
    JS.AliasValue("myarray." & loopindex)
    [/code:2xarrmhj]
    That makes no sense because you don't access array items via dot like this "myarray.0". You do this via brackets like this "myarray[0]". And this has nothing to do with the plugin. It's javascript basics.
    
    
    

    But you said that looping over an array defined in js script in C2 is a bad idea (cf my previous questions).

    If it's a bad how can I get my scores to build a leaderboard ?

    I think I jumped the gun with that statement. What I was rtying to say is that this is generally a bad idea if there's a better way of doing this. In this particular case there's no other way. There's an array in JS, and there are spites in Construct. If you want to translate JS-model into visible sprites, you have to "parse" the model. In our case you have to iterate through js-array from Construct and create sprites respectively.

  • Thanks for taking the time to answer my questions.

    I wish you could have been less condescending.

  • Updated to v.0.6.2:

    — Added "Compare alias call" condition

    — Placed all eval ACEs into separate section

  • Hello valerypopoff,

    let me thank you for this great software first of all, it could be that extra programming gear Construct was missing.

    Then, a request for advice

    Let's assume i've got an array of js objects representing players in a board, and that I have to do some stuff with this data, so to handle it, I use a js file and connect it to C2 with your plugin. If I want to create a 'player' sprite on the board for each object in the array, is there a way to import the array in C2 and iterate over it? How would you suggest to do that? To split the problem into multiple get function calls?

  • Thanks!

    Let's assume that you have this kinda thing in your Javascript file:

    var PlayersArray = [];
    
    PlayersArray[0] = 
    {
     name: "Player Zero",
     score: 666
    }
    ...
    [/code:1j8tuzvw]
    Then in Construct you create sprites and assign their Y coordinate according to the player's score. Assume your Javascript Plugin object is called JS. Player sprite object is called PlayerSprite. I'd do this:
    
    Condition: JS->AllScriptsLoaded
    Action: JS->InitAlias "PA" with Javascript "PlayersArray"
    
    Condition: System->For loop
    From: 0
    To: JS.AliasValue("PA.length")
    Action: System->CreateObject of type PlayerSprite
    Action: PlayerSprite->SetY to JS.AliasValue("PA["& loopindex &"].score")
    
    
    > Hello 

    valerypopoff,[/p] > let me thank you for this great software first of all, it could be that extra programming gear Construct was missing.[/p] > [/p] > Then, a request for advice [/p] > Let's assume i've got an array of js objects representing players in a board, and that I have to do some stuff with this data, so to handle it, I use a js file and connect it to C2 with your plugin. If I want to create a 'player' sprite on the board for each object in the array, is there a way to import the array in C2 and iterate over it? How would you suggest to do that? To split the problem into multiple get function calls?[/p] > [/p]

  • First: Thanks for this useful plugin It's awesome!

    I have a problem with AliasValue: it's ok with var, not with const and let.

    But if I pass const and let to a function, and I use Call function, I can get the correct value.

    What's wrong?

  • Cheers!

    Aliases are string shortcuts for js-variables, arrays, objects and functions. You can't initialize an alias with the object declared with const or let. Only var. And I just realized I never said that explicitly in the documentation. My bad.

    If you're interested why it works like this, here's an explanation. If you initialize the alias "Alias" with the js-string "jsobj", later when you do JS.AliasValue("Alias"), it actually returns the value of javascript:

    window["jsobj"][/code:g9xwxm4j]
    Global js-variables declared with [b]var[/b] become the properties of the [b]window[/b] object. Global objects declared with [b]const[/b] or [b]let[/b] – don't.
    
    But you can get the value of a global variable declared with [b]const[/b] or [b]let[/b] using the expression [b]JSCodeValue[/b] like this: JSCodeValue("testCONST"). It works because it is translated to javascript:
    [code:g9xwxm4j]eval("testCONST")[/code:g9xwxm4j]
    
    
    

    First: Thanks for this useful plugin It's awesome!

    I have a problem with AliasValue: it's ok with var, not with const and let.

    But if I pass const and let to a function, and I use Call function, I can get the correct value.

    What's wrong?

  • > Thanks!

    >

    > Let's assume that you have this kinda thing in your Javascript file:

    >

    var PlayersArray = [];
    > 
    > PlayersArray[0] = 
    > {
    >  name: "Player Zero",
    >  score: 666
    > }
    > ...
    > [/code:38v1a19b]
    > Then in Construct you create sprites and assign their Y coordinate according to the player's score. Assume your Javascript Plugin object is called JS. Player sprite object is called PlayerSprite. I'd do this:
    > 
    > Condition: JS->AllScriptsLoaded
    > Action: JS->InitAlias "PA" with Javascript "PlayersArray"
    > 
    > Condition: System->For loop
    > From: 0
    > To: JS.AliasValue("PA.length")
    > Action: System->CreateObject of type PlayerSprite
    > Action: PlayerSprite->SetY to JS.AliasValue("PA["& loopindex &"].score")
    > 
    > 
    

    This kind of freedom (such as in "JS.AliasValue("PA["& loopindex &"].score")") is fantastic, it opens many possibilities! Thank you very much for your time!

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