Dynamically get id of items

Discussion in 'RPG Maker MV' started by Yuuta Kirishima, Nov 5, 2017.

  1. Yuuta Kirishima

    Yuuta Kirishima Software Engineer/Professional Mayonnaise Player Veteran

    Messages:
    548
    Likes Received:
    131
    Location:
    North Carolina
    First Language:
    Engilsh
    Primarily Uses:
    RMMV
    I have a for loop that pushes the ids of the items in the array into a new array, however Ruby is different from JavaScript and I can't seem to figure out how to accomplish this, here's how i did it in ruby:

    Code:
    for items_array.each do |i|
         new_array.push($data_items[i].id)
    end
    
    what is the equivalent in MV?
     
    #1
  2. Llareian

    Llareian Jack of All Trades, Master of None Veteran

    Messages:
    603
    Likes Received:
    1,399
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    I know JavaScript but not Ruby, so I'm having trouble reading what the function you've placed here is actually doing. It seems to be creating new_array by calling from the database, but I'm not sure what items_array is or where i comes from. Can you clarify what you need the function to do?

    EDIT: To clarify some of my confusion, in MV's JavaScript,
    Code:
    $dataItems[i].id
    returns i. So to me (without knowing Ruby) it looks like you're taking an array of item ids and making that same array again, but by filtering it through the database for some reason. Also, there are TONS of array functions you can use to pull useful information from an array of actual item objects, so I really need to know what you're trying to accomplish and what you're starting with.
     
    Last edited: Nov 5, 2017
    #2
    Yuuta Kirishima likes this.
  3. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    I believe the equivalent for what you have is a for...in loop, in JavaScript for...in loops are not recommended for arrays (they are useful for objects though).
    Code:
    //Here is a JavaScript version using a normal for loop
    //Assuming itemArray and newArray are already defined variables
    for (var i = 0; i < itemArray.length; i++) {
        newArray.push($dataItems[i].id);
    }
     
    #3
    Yuuta Kirishima likes this.
  4. Yuuta Kirishima

    Yuuta Kirishima Software Engineer/Professional Mayonnaise Player Veteran

    Messages:
    548
    Likes Received:
    131
    Location:
    North Carolina
    First Language:
    Engilsh
    Primarily Uses:
    RMMV
    @Llareian @Sarlecc Thank you both I'm pretty dumb for some reason I thought $dataItems.id wouldn't work when in fact it does.
     
    #4
  5. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    #5
    Yuuta Kirishima likes this.

Share This Page