Little question about Object property with array

Discussion in 'Learning Javascript' started by Jonforum, Dec 18, 2016.

  1. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,574
    Likes Received:
    1,339
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    Hi


    I am building my quest system.
    I use for Quests the Rmmv Armors Items, $dataArmors[], because I do not use this DB in my project.
    At the beginning of the game, I inject all in $dataArmors [?].meta my individually properties for Quest.
    I write them in a .js file, because the comment window in the software is too difficult to manage.


    My question is about my access to my SubQuests.
    To access it, I currently use this, 'is work good but i dont like the way to acces'


    $dataArmors[1].meta.Quest['SubQ'+0].title  // return the title from meta.Quest.SubQ0
    $dataArmors[1].meta.Quest['SubQ'+0].Desc[0]  // return the Desc LV1 from meta.Quest.SubQ0


    Here a example of the builder when the project boot,
    You can see the SubQ0: { , he define all what are the SubQ0

    $dataArmors[1].meta.Quest = {
    SubType:['recipe', ' Item', 'switch'],// type et total number of subquest in Quest1
    Status:false,
    IsFinish: function check() {},//need to make a function to check all SubQ // .meta.Quest['SubQ'+id]
    //SubQuest Custom edit for $dataArmors[1]
    SubQ0: {
    isfinish: CheckRecipe(154);
    title:'title',
    Desc:['description lv1 int ',
    'more description lv2 int ',
    'more more more description lv3 int '],
    }
    SubQ1: { } // ....
    SubQ3: { } // ....

    };



    *But I would like to know if it would be possible to access , like this way.
    I find this way a cleaner way to manage.



    $dataArmors[1].meta.Quest.SubQ[0].title   // return the title from meta.Quest.SubQ[0]
    $dataArmors[1].meta.Quest.SubQ[0].Desc[0]  // return the Desc LV1 from meta.Quest.SubQ0


    So i build my attribut like thas , but i get  Uncaught SyntaxError: Unexpected token [

    $dataArmors[1].meta.Quest = {
    SubType:['recipe', ' Item', 'switch'],// type et total number of subquest in Quest1
    Status:false,
    IsFinish: function check() {},//need to make a function to check all SubQ // .meta.Quest['SubQ'+id]
    //SubQuest Custom edit for $dataArmors[1]
    SubQ: {
    [//SubQ0
    isfinish: CheckRecipe(154);
    title:'title',
    Desc:['description lv1 int ',
    'more description lv2 int ',
    'more more more description lv3 int ']
    ]
    [],//SubQ1
    []//SubQ2....

    }

    };



    Is that I put it wrong syntax, or simply not possible to build this way.
    I'm not an expert in javascript but I'm wondering about this.
    Have you some suggestion , am do a mistake ??


    Tell me if I am not clear in my explanation, I did my best.
    Thank you
     
    Last edited by a moderator: Dec 18, 2016
    #1
  2. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    921
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    Well without seeing any information on the error, it's a bit hard to debug it, but just by looking at it, I'm 95% sure your format is wrong. SubQ is an object that you try to cram an array into, but you didn't give that array a key to access it with. This is almost definitely invalid.
     
    #2
  3. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,574
    Likes Received:
    1,339
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    @Zalerinian


    ok to be more clean.
    I want something like this.
    A array dynamic property object


    var SubQ =[]; // now work Uncaught SyntaxError: Unexpected token [
    $dataArmors[1].meta.Quest = {
    SubQ[0]: {
    title:'Title ',
    }
    SubQ[1]: {
    title:'Title ',
    }
    }




    This no work but ideally i want a easy access similar to this. :)


    var i = 0;
    $dataArmors[1].meta.Quest.SubQ.title  // return me title SubQ[0]
    var i = 1;
    $dataArmors[1].meta.Quest.SubQ.title  // return me title SubQ[1]




    is my SyntaxError or is not possible to use named array for define a property like this in a object constructor ?
     
    Last edited by a moderator: Dec 18, 2016
    #3
  4. nio kasgami

    nio kasgami VampCat Veteran

    Messages:
    8,590
    Likes Received:
    2,340
    Location:
    Canada / Quebec
    First Language:
    French
    you can't edit manually the native json
     
    #4
    Jonforum likes this.
  5. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    Believe it should be more like the following as an example:


    var Quest = {
    SubQ: [{title: 'A Title'}, {title: 'Another Title'}]
    };


    Then you can call the way you want:

    Code:
    Quest.SubQ[0].title; // 'A Title'
    
     
    #5
    Jonforum likes this.
  6. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    921
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    $dataArmors[1].meta.Quest = {
    SubQ:
    [
    {
    title: 'Title 1',
    },
    {
    title: 'Title 2',
    }
    ]
    }




    this is how you use an array in JSON, @Jonforum
     
    #6
    Jonforum likes this.
  7. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,574
    Likes Received:
    1,339
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    @Zalerinian @Sarlecc @nio kasgami


    NICE NICE NICE  :D
    Thank you very much everyone, I am very happy now and I learned something new.
    You are all super


    USE directly the JSON, This is a very good idea, but I gave up when I saw the formatting.
    I find the syntax and formatting difficult to read.


    JSON formating  :headshake:

    Captdsfsfure.JPG



    VS


    JS formating

    adfaf.JPG





    And I do not yet know the language used.


    Maybe study it soon :)
     
    #7
  8. LTN Games

    LTN Games Veteran Veteran

    Messages:
    609
    Likes Received:
    459
    Location:
    Canada
    First Language:
    English
    Primarily Uses:
    RMMV
    The formatting is not usually like that but because it's stringified before it's saved it shows up lke this. If you want to read the json, use a beautify extension in your editor or the native code formatting tool to get the original format back. Using my editor I was able to get the format to look like this.


    Screenshot_1.png


    The way you're doing things is fine but don't be scared of using a JSON file because of the way it looked, hopefully, this helps you become less worried about JSON.
     
    #8
    Jonforum likes this.
  9. Kino

    Kino EIS Game Dev Veteran

    Messages:
    515
    Likes Received:
    671
    Location:
    United States
    First Language:
    English
    Primarily Uses:
    RMMV
    You can use JSON.stringify (data, null, '\t') to format it better. I use it with my own plugins.
     
    #9
    Jonforum likes this.

Share This Page