Yann's Forum Posts

  • rexrainbow

    When I'll find a way to have cyclic objects and being able to export/import them as JSON.

    For that I investigated how dojo does it

    http://dojotoolkit.org/reference-guide/1.10/dojox/json/ref.html

    And I'd like to extract only that specific algorithm to use in the plugin (not having to carry the entire dojo library)

    For now I managed to have a Save/Load Reference actions so you can basically put objects inside objects by reference instead of copy. (even from different object-type of the JSON plugin)

    So you can make a cyclic linked list for example

    But if you export as JSON and import it back, you'll lose the cyclic aspect, so it breaks all the save/load features.

    Even if I manage that, I'm not sure the save/load will always work since I won't be able to save as JSON the possible link between two different objects.

    So yeah, I hit a wall and I had other things to do so the dev stopped for awhile.

    However, I already used the plugin in a few of my projects and it's behaving nicely. Though it adds some layer of complexity.

    Though bear in mind that there's two bugs hanging in the current version:

    - bug fix: array elements weren't properly deleted using the delete function
    [ul]
    	[li]bug fix: error when using clear and delete with current path.[/code:1qdildcw][/li]
    [/ul]If I used some subversion I would have just made a release with those fixes but now they are merged with my half-implemented reference thing so...
  • megatronx

    Nope.

    In canvas2D it's not possible, and in webGL it would be if I had access to uv coordinates (almost certain the SDK doesn't expose uvs), but even then, you might not like the result.

  • Dasat,

    I edited the bits about array looping since it wasn't entirely exact.

  • Dasat

    Ah yeah sorry I forgot to launch dropbox, now it's updated

    And for your foreach, first, remember to not do it every tick, that's the reason why it wasn't stopping. You can nest it under the start of layout event for it to just run once.

    Now as far as foreach goes, you're using it correctly.

    However, you have two ways to loop through arrays:

    • foreach as you did
    • "index looping", basically go from 0 to the size of the array-1 and use loopindex in the path to look up the values (that's actually what is usually done in common programming)

    The two mecanisms have a slightly different behaviour that can be seen in this capx: JSONDasat-holy-array.capx

    As far as I could test, the foreach keeps the proper array ordering.

    Looping using foreach is like considering the array as an object. And objects have no intrinsic ordering. So the foreach keeping the ordering shouldn't be expected, even if, it seems that does it, at least on chrome....

    I dunno if it's consistent throughout all the browsers, so I usually use index looping.

    Now your other problem is that you're printing the keys using JSON.CurrentKey, if you want the content, you need to use JSON.CurrentValue

    In your case, the value for "friends" is an array, so you need to detect that, you can either use the Is Array condition or check if CurrentKey = "friends".

    I would personally check for Array 'cause this way you can change the name of the field if you need it.

    Anyway, it should look something like that JSONDasat-foreach.capx

  • Dasat,

    Ah, indeed, I made a mistake, I corrected it, if you download the capx again you should see the change. I leave you, as an exercise, to find out why it was broken.

    Also it reminded me about two things:

    1/ Use the console log, on preview mode I print useful warnings when something is wrong (like trying to assign a value to an inexisting path)

    2/ The LogData action logs interesting information in the console. (See the aforementioned correction for a usage example)

    As far as the Value expression goes, it should be almost straight forward. With only one small little ugly thing: to retrieve a value, you need to specify the path of that value. And my plugin has support for absolute path (from root) or relative path (from current).

    The problem is that in an expression you can only use numbers and strings. So the only solution I found was to use the first argument of value to be 0 for root and 1 for current.

    This way, if you put something a:

    root@0,"property",1,"property_name"[/code:yniahqf4]You can retrieve it using[code:yniahqf4]JSON.Value(0, 0,"property",1,"property_name")
     means root^  ^-------absolute path--------^[/code:yniahqf4]
    with relative path:[code:yniahqf4]Set Current Path at root@0,"property",1
    current@"property_name"
    
    corresponds to
    
    JSON.Value(1,"property_name")
       current ^ ^relative path^[/code:yniahqf4]
    
    The Size expression follows the same rule, it returns the number of element in an array or the number of member in an object, for other types, if my memory is correct it will return -1
  • Problem Description

    The offline.appcache generated by Construct display relative subpath using backwslashes (\)

    like:

    images\sprite-sheet0.png
    media\bgmusic.m4a
    media\bgmusic.ogg
    [/code:pesnfix5]
    This breaks the caching on some linux based servers which require forward slashes when using Firefox.
        
    [b]Affected Browsers[/b]
    [ul]
        [li] Chrome: (NO)
        [/li][li] FireFox: (YES)
        [/li][li] Internet Explorer: (NOT TESTED)[/li][/ul]
        
    [b]Operating System and Service Pack[/b]
    Tested on window, linux and mac
        
    [b]Construct 2 Version ID[/b]
    r175
  • Dasat

    If you run the capx in the debugger (Ctrl+F5) you should see that:

    {
      "name": "dasat",
      "properties": {
        "property_name": "thermal"
      }
    }[/code:3pnpyhml]
    which is not nothing, but yeah, not what you want for now
    
    Let's comment what you did line by line so I explain a few things:
    
    [code:3pnpyhml]
    + System: on start of layout
       // in javascript it could be represented as: var root = {};
       -> JSON: new Object at root@    
       + [empty]
          // here you are redoing: root = {}; which basically discard the first object
          -> JSON: new Object at root@ 
          // here current and root are the same, so you are doing:  root["name"] = "Dasat";
          -> JSON: set "Dasat" at current@"name" 
          // you set the current path to the current path, so nothing changes
          -> JSON: Set Current Path to current@  
          // here you do root["properties"] = [];
          -> JSON: new Array at currenthfx@properties 
          // here that make sens, now current is root["properties"]
          -> JSON: Set Current Path to current@"properties" 
          // you do root["properties"] = {}; so your array gets discarded
          -> JSON: new Object at current@  
          // root["properties"]["property_name"] = "electrical"
          -> JSON: set "electrical" at current@"property_name" 
          // root["properties"]["property_unit"] = "wareva"
          -> JSON: new "wareva" at current@"property_unit" 
          // here you basically discard the object at root["properties"] and replace it by an empty one
          -> JSON: new Object at current@ 
          // root["properties"]["property_name"] = "thermal"
          -> JSON:  new "thermal" at current@"property_name" 
    [/code:3pnpyhml]
    To summurize I'll write in javascript only what you did
    [code:3pnpyhml]
    var root = {};
    root = {};     //which basically discard the first object
    root["name"] = "Dasat";
    root["properties"] = [];
    root["properties"] = {}; // discard the array
    root["properties"]["property_name"] = "electrical";
    root["properties"]["property_unit"] = "wareva";
    root["properties"] = {}; // discard the previous object
    root["properties"]["property_name"] = "thermal";
    [/code:3pnpyhml]
    So here the result showing in the debugger makes sens
    
    Let's travel from javascript back to the JSON plugin
    What you want to generate is:
    
    I'm trying to have something like let's build a basic addition and multiplication table, like what we learnt whe we were young (but yeah, 0-based for simplicity):
    [code:3pnpyhml]
    [
       {"name": "dasat,
        "properties": [
              {"property_name": "conductivity", "property_unit": "wareva" },
              {"property_name": "restitivity", "property_unit": "ohmmeter"}
          ]
       }, 
       {"name": "dasat2,
       "properties":[
             {"property_name": "conductivity2", "property_unit": "wareva2" }
             {"property_name": "restitivity2", "property_unit": "ohmmeter2"}
          ]
       }
    ]
    [/code:3pnpyhml]
    
    in javascript you can do it like that:
    [code:3pnpyhml]
    var root = [];
    root[0] = {};
    root[0]["name"] = "Dasat";
    root[0]["properties"] =  [];
    root[0]["properties"][0] =  {};
    root[0]["properties"][0]["property_name"] = "conductivity";
    root[0]["properties"][0]["property_unit"] = "wareva";
    root[0]["properties"][1] =  {};
    root[0]["properties"][1]["property_name"] = "restitivity";
    root[0]["properties"][1]["property_unit"] = "ohmmeter";
    root[0] = {};
    root[1]["name"] = "Dasat2";
    root[1]["properties"] =  [];
    root[1]["properties"][0] =  {};
    root[1]["properties"][0]["property_name"] = "conductivity2";
    root[1]["properties"][0]["property_unit"] = "wareva2";
    root[1]["properties"][1] =  {};
    root[1]["properties"][1]["property_name"] = "restitivity2";
    root[1]["properties"][1]["property_unit"] = "ohmmeter2";
    [/code:3pnpyhml]
    
    Some equivalents with the JSON plugin would be:
    [url=https://app.box.com/s/qg6tq0zdjph7z0ni0tzj]JSONDasat.capx[/url]
  • Dasat

    Thing is, I'm not really sure it's required to send json header, since you're basically passing the string in a form variable.

    If you're using php on server side, try that:

    <?php
        $data = (isset($_POST['data'])) ? $_POST['data'] : '';
        $json = json_decode(stripslashes($_POST['data']));
    
        header('Access-Control-Allow-Origin: *'); // for ajax
        header('Content-Type: text/plain');
    
        echo("raw data:\n");
        var_dump($data);
    
        echo("\n\n");
    
        echo("json data:\n");
        var_dump($json);
    ?>
    [/code:2msyoj69]
    
    using this capx [url=https://app.box.com/s/yj2xkf8jz14r7dkt7v95]JSONtoServer.capx[/url]
    (don't forget to modify your SERVER_URL)
  • Dasat

    Well to debug your problem you have a lot of options:

    - you can print the resulting string by inserting the browser plugin and in the action Browser: log(JSON.AsJson(0)) Then you can copy paste the result in http://jsonlint.com/ to see if the json is valid

    - you can check if the javascript console gives any error like the famous [quote:3naab24c]XMLHttpRequest cannot load http://scirra.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.google.com' is therefore not allowed access.

    whose solution is to either host your game on the same server you are querying, or, make the server send the Access-Control-Allow-Origin: * header

    • you can check your own server side code to se if everything is ok

    And well, if the problem persist, give more detail.

    What would you do if a friend was asking for help on an arithmetic problem without showing you the actual problem? :D (considering you have enough arithmetic knowledge, but that's not the point here)

  • Dasat

    Indeed, now that you mentionned it, your proceduraly generated robot avatar has a little touch of feminity.

    So allow me to rephrase:

    Glad to help madam.

  • Dasat

    Glad to help sir

  • Try Construct 3

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

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

    is this working for you?

    arithmeticTables.capx

  • Dasat

    hmmm

    but when i set the key or value to a defined variable in my program or content of some array, it doesnt reflect

    Not sure I understand what you are trying to say.

    + System: on start of layout
         Local text myKey = "someKey"
         Local text myValue = "someValue"
         -> JSON: new Object at root@
         -> JSON: set myValue at root@myKey[/code:2s2jgc8y]
    Doesn't work for you?
  • chrismaliszewski

    if you have an Array whose AsJson gives you

    {
        "c2array":true,
        "size":[3,2,2],
        "data":
        [
            [ 
                [10,32],[12,48]
            ],
            [
                [72,26],[80,32]
            ],
            [
                [-28,16],[-22,20]
            ]
        ]
    }[/code:3djezfzp]
    What would you want to transform it into?
    
    (or maybe use your own example, mine uses just random numbers)
  • JordanKal

    probably, but I won't make an example capx because it's a bit more complex.

    In my case the cutting was just done by choosing two vertices. With your idea you'll have the possibility to make cuts my selecting two vertices but also, three, four.... probably more.

    So not only will you have to keep track of the possible cut remaining, but also if the piece is already cut or not.

    for example, with your idea you could do:

    * * * *
         /
    * * * *
       / /
    * *-* *
    
    * * * * [/code:24wd54lm]
    but not
    [code:24wd54lm]
    * * * *
     \   /
    * * * *
       X   
    * *-* *
    
    * * * *[/code:24wd54lm]
    (at least you shouldn't be able to I think)
    On top of that you'll have to resolve vertices orders for the newly created polygons.
    So not impossible, but yeah hard.