valerypopoff's Forum Posts

  • Thanks for your answer.

    It would be really handy if the expression JS.AliasValue(alias) returns a json string if the value is an object or an array.

    Imagine AliasValue returns a json string of a js-object. How would you use this string in Construct?

  • Thank you!

    1-would it be possible to add trigger support ?

    I didn't add trigger support because:

    — You can come up with your own triggers (boolean variables) and use Compare

    — If you're operating with Promises in your js functions, triggers won't help you. Like: ok, the function is executed, here we are, shooting the trigger, but what the function really does is not finished yet. So you will have to manually do some «this.ShootMe()» in your js code when the Promise resolved. Sounds like API to me. I want any js code just work without it knowing that it's being executed in some special environment.

    — I believe, this is a broken architecture if you want to make Construct do something right after asynchronous js-algorithm is finished. If you need something to be done right after something else, do it in javascript. Don't bother Construct with that. If you really need Construct to react on a result of your js-algorithm, use states. This is a more consistent way of doing that. Pseudocode:

    js:
    Promise resolved ? Player.state = "walking".
    
    construct:
    Compare alias [Player.state] == "walking" ? player_sprite.SetAnimation("walking_anim")
    [/code:5yui0vlz]
    What do you think of it? Don't hesitate to share your ideas and tell me more about your architecture. 
    
    
    

    2-In your landing page you said that "Javascript Plugin is slower than built-in Construct instructions and functions"

    Do you mean that a chunk of code written in JS is slower that code written with construct's ACEs ?

    No. A chunk of code written in JS is always faster that code written with construct's ACEs. Because pure js is always faster.

    What I meant is that the whole process of calling the action "Call JS function" that executes js function "Foo" that does "i=0" is slower than Construct instruction "Set variable i to 0". The js code itself is faster. What is slower is calling a plugin action. The more your JS does (not just "i=0"), the smaller the difference. Iterating through large arrays are much faster in js than in Construct, for example.

    3-Does your plugin uses any ecmascript 6 features that might not be supported in older browsers or mobile ?

    Thanks.

    Oh! Good question, thanks! I forgot to mention it in a FAQ. No, it doesn't use any ecmascript 6 features.

  • I think i kinda see what you're trying to do. You can do this, yes:

    EDIT: I think I found a way to do this... ignore this post for now

    function Compare(a,attacker,target) {
    	var stat = {armor:0, crit:1, critDmg:2, dmg:3, mhp:4, hp:5, shotcount:6};
    	
    	var aArmor = attacker.split(",")[stat.armor];
    	var aCrit = attacker.split(",")[stat.crit];
    	var aCritDmg = attacker.split(",")[stat.critDmg];
    	var aDmg = attacker.split(",")[stat.dmg];
    	var aMHp = attacker.split(",")[stat.mhp];
    	var aHp = attacker.split(",")[stat.hp];
    	var aShotCount = attacker.split(",")[stat.shotcount];
    	
    	var tArmor = target.split(",")[stat.armor];
    	var tCrit = target.split(",")[stat.crit];
    	var tCritDmg = target.split(",")[stat.critDmg];
    	var tDmg = target.split(",")[stat.dmg];
    	var tMHp = target.split(",")[stat.mhp];
    	var tHp = target.split(",")[stat.hp];
    	var tShotCount = target.split(",")[stat.shotcount];
    
    	return eval(a);
    }[/code:10uk0rlf]
    
    

    It's just you wouldn't have to do that if you had Player and Enemy objects in javascript from the very beginning. The plugin allows you to keep the whole model part in javascript and never store object data in Construct. So you never have to pass data to javascript. You just call certain javascript functions with certain parameters.

    Imagine that you only have player's and multiple enemie's sprites in Construct. And these sprites only represent the objects. They are not the objects. They don't store any data except their own x and y sprite coordinates. These sprites have only to have one instance variable — id. This is how you know that this certain sprite represents this certain object in javascript.

    Also imagine that you have "Player_sprite on collision with Enemy_sprite condition". And you do "Call JS function" with parameters:

    Name: CalculateDamage

    Param 0: Player_sprite.id

    Param 1: Enemy_sprite.id

    And also imagine that you have something like this in javascript:

    var players = [];
    
    var Player = 
    {
    	armor: 100, 
    	crit: 50, 
    	critDmg: 20, 
    	dmg: 10, 
    	mhp: 10, 
    	hp: 75, 
    	shotcount: 0
    }
    
    var Enemy = 
    {
    	armor: 100, 
    	crit: 50, 
    	critDmg: 20, 
    	dmg: 10, 
    	mhp: 10, 
    	hp: 75, 
    	shotcount: 0
    }
    
    function InitGame()
    {
    	players.push( Player );
    
    	return players.length-1;
    }
    
    function CreateEnemy()
    {
    	players.push( Object.assign({}, Enemy) );	
    
    	return players.length-1;
    }
    
    function CalculateDamage( attacker_id, attacked_id )
    {
    	var attacker = players[attacker_id];
    	var attacked = players[attacked_id];
    
    	attacked.health -= attacker.damage;
    	...
    }
    [/code:10uk0rlf]
    
    This is how you're really supposed to use the plugin.
  • Did I get this right? You have a global Construct variable called ShotCount. You have a js function called Compare (Sic!) that takes a string argument, evals it and returns the result. Then you call js function "Compare" from Construct with the action "Call js function" and pass the parameter which is basically "shotCount % 3 == 0".

    If I got this right, you get a fair response "shotCount is not defined". Because there's no such variable in global scope of your js-script. Construct variables are not real. They don't exist in your js-script. Construct variables are virtual and only make sense to the engine itself and to the js-plugin ACEs. So if you want your js script to operate with construct variables (which is totally upside-down), you should either pass them as arguments to js functions, or create js variables in your script, set their values to Construct variables values (use aliases) and then operate with those js variables.

    If you're just playing around with the plugin, it's alright. But if you're really trying to achieve something here, this something is not clear to me. Maybe if you tell me what you're trying to do, we'll find a suitable solution for that.

    Also can you send me a project file please?

    Amazing plugin! I ran into a little problem I was hoping you could help me with:

    I'm trying to use this script to eval a string. This string could contain any number of global variables and even call functions

    A test string, called var1 is this: "shotCount % 3 == 0"

    The event is JS Call Function "Compare" with parameter 0 being var1

    The .js file has this:

    function Compare(a) {
    	return eval(a);
    }[/code:2t75r8km]
    
    But it doesn't work, it says "shotCount is not defined" in the log. If I change var1 to something like "2+2" it evals correctly.
    I don't want to pass the global variable because I need this to be flexible and allow multiples and any variable. Do I need to somehow put the variables into the scope of the JS plugin?
    
  • The new version of a plugin is out! Check it on plugin's promo page:

    https://readymag.com/valerypopoff/valer ... js-plugin/

    What's new:

    — Aliases: an easy mechanism to work with js-objects, its properties and methods

    — Plugin is now "Single global" which means you can only have one instance of a plugin in a project

    — Everything works faster because functions and aliases aren't implemented with eval( ) anymore

  • CS2 Plugin version is down dude.

    Freaking dropbox! Fixed it, thanks

  • Ashley Thanks! That's a good advice. Will try to avoid eval if it's possible.

  • This isn't a meaningful test. The JS engine can see that the executed JavaScript will have no effect at all, and just ignore it. However in practice nobody uses JavaScript that has no effect at all, so it's not important if this case is optimised or not. Different browsers will have different approaches.

    I checked. JS engine doesn't ignore this code and properly executes it. It's easy to check in a console. Just declare a variable that is later altered with the code we're talking about and see in console whether the variable was altered.

    And still 100x difference between Chrome and Safari. Can it be not the JS issue then? Maybe it has something to do with the plugin action call itself?

  • Ashley any ideas?

  • I was experimenting with executing javascript code from Construct 3 and noticed something rather strange. A simple JS code is executed 100 times slower than the simplest native Construct instruction. But only in Chrome. In Safari it's the same.

    Here's the example project: test.c3p

    Safari 11.0.2

    — 3,000,000 empty loops: 4.5 sec

    — 3,000,000 loops with "System: Set VARIABLE to 666": 8.9 sec

    — 3,000,000 loops with "Browser: Execute javascript 'var variable=666'": 8.5 sec

    Chrome 63.0.3239.132

    — 3,000,000 empty loops: 0.88 sec

    — 3,000,000 loops with "System: Set VARIABLE to 666": 1.2 sec

    — 30,000 loops with "Browser: Execute javascript 'var variable=666'": 1.65 sec

    See? I tested javascript execution in Chrome on 30,000 loops instead of 3,000,000 because otherwise it hangs the browser dead and seems to never execute it.

    Same thing when I use my own Javascript plugin that simply does "eval" of a string you pass to it. Is it normal or what?

  • I may be wrong but it really looks like you're not so good at javascript. If so, I would suggest you not to use this plugin. Can't think of a single scenario how you can benefit from using it if you don't know how to properly use javascript.

    Now I'm gonna explain why it doesn't work as you expected. But I strongly recommend you to read about javascript functions.

    It's not obvious from what you said but I'm guessing you're using Execute JS code action. If so and the Code parameter is

    "
    
    function myFunction(#0,#1) {
    return #0+#1;
    }
    
    "[/code:1z4qpjwb]
    it will return [b]undefined[/b]. StoredReturnValue works. It just returns [b]undefined[/b]. And in Construct you see it as 0.
    
    The reason it returns undefined is that in your code you only define the function and never call it. A function definition doesn't return anything. Truth to be told this is not even a proper definition you've got there. The action will simply substitute #0 and #1 with the values of the parameters you pass. So the code you're trying to execute is something like:
    [code:1z4qpjwb]"
    
    function myFunction(7,12) {
    return 7+12;
    }
    
    "[/code:1z4qpjwb]
    and if you preview the project and look in the console (always look in the console) you'll see that you're getting a syntax error which in this case is "Unexpected number". Because you can't put numbers in parenthesis in a function definition. It must be names of parameters.
    
    So what you should have done is to define a function first and then call it:
    [code:1z4qpjwb]"
    
    function myFunction(a, b) {
    return a+b;
    }
    
    myFunction(#0,#1);
    
    "[/code:1z4qpjwb]
    
    
    

    Can you explain why the StoredReturnValue don't work?

    I test alot of JS codes, even simple ones and don't return any value, even if i specified.

    Example:

    "
    
    function myFunction(#0,#1) {
    return #0+#1;
    }
    
    "[/code:1z4qpjwb]
    
    Its just two Global Variables in construct to sum and return the result.
    As you said the only value who return its "0".
    
  • Not sure if I now what you mean. The file structure of what?

    If you mean the file structure of included javascript, then yes. You can include any valid script. Any valid scriptS, actually.

    Just put them into the "Script files" field like this:

    script1.js;script2.js (Only for Construct 2. In Construct 3 you put every script in a separate line)

    There's no multiline text fields in Construct 2 plugin properties, so you have to separate scripts with ;

    After the scripts are loaded, they are executed the same way it works when you add scripts to any webpage.

    valerypopoff O man this looks amazing, I'm guessing that the file structure has to stay the same?And it automatically just includes all files instead of just one?

  • newt Phacanu totoe irina

    The Construct 2 plugin version is released. See the top post to get the download link.

    In Construct 2 version conditions "Compare Function's Return value" and "Compare JS code Completion value" have reversed parameters order. I had to do that due to the differences between how Construct 2 and Construct 3 work.

    So instead of this:

    You'll have this:

  • Javascript Plugin v.0.7.0 is out!

    +C3 runtime support

    Use 3rd party js libraries (including JQuery), call javascript functions, access object properties and methods. Implement game objects, and algorithms in javascript.

    This plugin also enables you to organize the model and algorithmic parts of your game in javascript. Organize objects, classes, functions and arrays in a javascript file. Do loops, algorithms and other stuff – which Construct event sheets are not designed for – in a javascript. Only pass algorithm results and object values to Construct when needed.

    Include js script into your project and access objects, variables and functions from anywhere in a game.

    More about the plugin, downloads and examples — on plugin's promo page

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I guess in my case I can not.

    I made a plugin with the condition that works like this:

    It works perfectly in Construct 3 but this is a problem for Construct 2. What would you recommend?

    Variadic parameters were implemented solely for the Function plugin. Anything the Function plugin doesn't need may be missing or buggy. I'd advise against using them if you can.